2013-11-04 23:16:46 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-08-01 13:06:35 +02:00
|
|
|
import logging
|
2016-08-08 09:38:50 +02:00
|
|
|
from typing import Any, Set, Tuple, Optional
|
|
|
|
from six import text_type
|
2016-08-01 13:06:35 +02:00
|
|
|
|
2013-11-04 23:16:46 +01:00
|
|
|
from django.contrib.auth.backends import RemoteUserBackend
|
2013-11-21 01:30:20 +01:00
|
|
|
from django.conf import settings
|
2016-08-08 09:38:50 +02:00
|
|
|
from django.http import HttpResponse
|
2013-11-04 23:42:31 +01:00
|
|
|
import django.contrib.auth
|
|
|
|
|
2016-08-08 09:38:50 +02:00
|
|
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
2015-10-13 23:08:05 +02:00
|
|
|
from zerver.lib.actions import do_create_user
|
2013-11-21 01:30:20 +01:00
|
|
|
|
2015-10-13 23:08:05 +02:00
|
|
|
from zerver.models import UserProfile, Realm, get_user_profile_by_id, \
|
|
|
|
get_user_profile_by_email, remote_user_to_email, email_to_username, \
|
|
|
|
resolve_email_to_domain, get_realm
|
2013-08-06 22:51:47 +02:00
|
|
|
|
2014-01-10 23:48:05 +01:00
|
|
|
from apiclient.sample_tools import client as googleapiclient
|
|
|
|
from oauth2client.crypt import AppIdentityError
|
2016-08-01 13:06:35 +02:00
|
|
|
from social.backends.github import GithubOAuth2, GithubOrganizationOAuth2, \
|
|
|
|
GithubTeamOAuth2
|
|
|
|
from social.exceptions import AuthFailed
|
2016-07-20 13:33:27 +02:00
|
|
|
from django.contrib.auth import authenticate
|
2013-08-06 22:51:47 +02:00
|
|
|
|
2014-03-28 00:45:03 +01:00
|
|
|
def password_auth_enabled(realm):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (Realm) -> bool
|
2015-08-19 03:42:38 +02:00
|
|
|
if realm is not None:
|
2015-09-20 06:16:18 +02:00
|
|
|
if realm.domain == 'zulip.com' and settings.PRODUCTION:
|
2015-08-19 03:42:38 +02:00
|
|
|
# the dropbox realm is SSO only, but the unit tests still need to be
|
|
|
|
# able to login
|
|
|
|
return False
|
2015-02-06 03:00:28 +01:00
|
|
|
|
2013-11-04 23:42:31 +01:00
|
|
|
for backend in django.contrib.auth.get_backends():
|
|
|
|
if isinstance(backend, EmailAuthBackend):
|
|
|
|
return True
|
2015-09-30 08:14:17 +02:00
|
|
|
if isinstance(backend, ZulipLDAPAuthBackend):
|
|
|
|
return True
|
2013-11-04 23:42:31 +01:00
|
|
|
return False
|
|
|
|
|
2015-08-19 02:58:20 +02:00
|
|
|
def dev_auth_enabled():
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: () -> bool
|
2015-08-19 02:58:20 +02:00
|
|
|
for backend in django.contrib.auth.get_backends():
|
|
|
|
if isinstance(backend, DevAuthBackend):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def google_auth_enabled():
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: () -> bool
|
2015-08-19 02:58:20 +02:00
|
|
|
for backend in django.contrib.auth.get_backends():
|
|
|
|
if isinstance(backend, GoogleMobileOauth2Backend):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-04-21 21:07:43 +02:00
|
|
|
def common_get_active_user_by_email(email, return_data=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (text_type, Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
2016-04-21 07:19:08 +02:00
|
|
|
try:
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return None
|
2016-04-21 21:07:43 +02:00
|
|
|
if not user_profile.is_active:
|
|
|
|
if return_data is not None:
|
|
|
|
return_data['inactive_user'] = True
|
|
|
|
return None
|
|
|
|
if user_profile.realm.deactivated:
|
|
|
|
if return_data is not None:
|
|
|
|
return_data['inactive_realm'] = True
|
2016-04-21 07:19:08 +02:00
|
|
|
return None
|
|
|
|
return user_profile
|
|
|
|
|
2016-07-20 13:33:27 +02:00
|
|
|
def github_auth_enabled():
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: () -> bool
|
2016-07-20 13:33:27 +02:00
|
|
|
for backend in django.contrib.auth.get_backends():
|
2016-07-29 21:47:14 +02:00
|
|
|
if isinstance(backend, GitHubAuthBackend):
|
2016-07-20 13:33:27 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-11-01 20:22:12 +01:00
|
|
|
class ZulipAuthMixin(object):
|
|
|
|
def get_user(self, user_profile_id):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (int) -> Optional[UserProfile]
|
2013-11-01 20:22:12 +01:00
|
|
|
""" Get a UserProfile object from the user_profile_id. """
|
|
|
|
try:
|
|
|
|
return get_user_profile_by_id(user_profile_id)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2016-07-26 12:12:01 +02:00
|
|
|
class SocialAuthMixin(ZulipAuthMixin):
|
2016-08-08 09:45:52 +02:00
|
|
|
def get_email_address(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> text_type
|
2016-07-26 12:12:01 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2016-08-08 09:45:52 +02:00
|
|
|
def get_full_name(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> text_type
|
2016-07-26 12:12:01 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def authenticate(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> Optional[UserProfile]
|
2016-07-26 12:12:01 +02:00
|
|
|
return_data = kwargs.get('return_data', {})
|
|
|
|
|
|
|
|
email_address = self.get_email_address(*args, **kwargs)
|
|
|
|
if not email_address:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
user_profile = get_user_profile_by_email(email_address)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return_data["valid_attestation"] = True
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not user_profile.is_active:
|
|
|
|
return_data["inactive_user"] = True
|
|
|
|
return None
|
|
|
|
|
|
|
|
if user_profile.realm.deactivated:
|
|
|
|
return_data["inactive_realm"] = True
|
|
|
|
return None
|
|
|
|
|
|
|
|
return user_profile
|
|
|
|
|
|
|
|
def process_do_auth(self, user_profile, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (UserProfile, *Any, **Any) -> Optional[HttpResponse]
|
2016-07-26 12:12:01 +02:00
|
|
|
# This function needs to be imported from here due to the cyclic
|
|
|
|
# dependency.
|
|
|
|
from zerver.views import login_or_register_remote_user
|
|
|
|
|
|
|
|
return_data = kwargs.get('return_data', {})
|
|
|
|
|
|
|
|
inactive_user = return_data.get('inactive_user')
|
|
|
|
inactive_realm = return_data.get('inactive_realm')
|
|
|
|
|
|
|
|
if inactive_user or inactive_realm:
|
|
|
|
return None
|
|
|
|
|
2016-08-08 09:38:50 +02:00
|
|
|
request = self.strategy.request # type: ignore # This comes from Python Social Auth.
|
2016-07-26 12:12:01 +02:00
|
|
|
email_address = self.get_email_address(*args, **kwargs)
|
|
|
|
full_name = self.get_full_name(*args, **kwargs)
|
|
|
|
|
|
|
|
return login_or_register_remote_user(request, email_address,
|
|
|
|
user_profile, full_name)
|
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
class ZulipDummyBackend(ZulipAuthMixin):
|
|
|
|
"""
|
|
|
|
Used when we want to log you in but we don't know which backend to use.
|
|
|
|
"""
|
|
|
|
def authenticate(self, username=None, use_dummy_backend=False):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (Optional[str], bool) -> Optional[UserProfile]
|
2013-11-21 04:57:23 +01:00
|
|
|
if use_dummy_backend:
|
2016-04-21 07:19:08 +02:00
|
|
|
return common_get_active_user_by_email(username)
|
2013-11-21 04:57:23 +01:00
|
|
|
return None
|
|
|
|
|
2013-11-01 20:22:12 +01:00
|
|
|
class EmailAuthBackend(ZulipAuthMixin):
|
2013-08-06 22:51:47 +02:00
|
|
|
"""
|
|
|
|
Email Authentication Backend
|
|
|
|
|
|
|
|
Allows a user to sign in using an email/password pair rather than
|
|
|
|
a username/password pair.
|
|
|
|
"""
|
|
|
|
|
2016-04-21 21:07:43 +02:00
|
|
|
def authenticate(self, username=None, password=None, return_data=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (Optional[text_type], Optional[str], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
2013-08-06 22:51:47 +02:00
|
|
|
""" Authenticate a user based on email address as the user name. """
|
|
|
|
if username is None or password is None:
|
|
|
|
# Return immediately. Otherwise we will look for a SQL row with
|
|
|
|
# NULL username. While that's probably harmless, it's needless
|
|
|
|
# exposure.
|
|
|
|
return None
|
|
|
|
|
2016-04-21 21:07:43 +02:00
|
|
|
user_profile = common_get_active_user_by_email(username, return_data=return_data)
|
2016-04-21 07:19:08 +02:00
|
|
|
if user_profile is None:
|
2013-08-06 22:51:47 +02:00
|
|
|
return None
|
2016-04-21 07:19:08 +02:00
|
|
|
if not password_auth_enabled(user_profile.realm):
|
2016-04-21 21:07:43 +02:00
|
|
|
if return_data is not None:
|
|
|
|
return_data['password_auth_disabled'] = True
|
2016-04-21 07:19:08 +02:00
|
|
|
return None
|
|
|
|
if user_profile.check_password(password):
|
|
|
|
return user_profile
|
2013-08-06 22:51:47 +02:00
|
|
|
|
2014-01-10 23:48:05 +01:00
|
|
|
class GoogleMobileOauth2Backend(ZulipAuthMixin):
|
|
|
|
"""
|
|
|
|
Google Apps authentication for mobile devices
|
|
|
|
|
|
|
|
Allows a user to sign in using a Google-issued OAuth2 token.
|
|
|
|
|
|
|
|
Ref:
|
|
|
|
https://developers.google.com/+/mobile/android/sign-in#server-side_access_for_your_app
|
|
|
|
https://developers.google.com/accounts/docs/CrossClientAuth#offlineAccess
|
|
|
|
|
|
|
|
"""
|
2016-08-08 09:38:50 +02:00
|
|
|
def authenticate(self, google_oauth2_token=None, return_data=dict()):
|
|
|
|
# type: (Optional[str], Dict[str, Any]) -> Optional[UserProfile]
|
2014-01-10 23:48:05 +01:00
|
|
|
try:
|
|
|
|
token_payload = googleapiclient.verify_id_token(google_oauth2_token, settings.GOOGLE_CLIENT_ID)
|
|
|
|
except AppIdentityError:
|
|
|
|
return None
|
2014-05-14 18:57:09 +02:00
|
|
|
if token_payload["email_verified"] in (True, "true"):
|
2014-01-10 23:48:05 +01:00
|
|
|
try:
|
2016-04-21 07:19:08 +02:00
|
|
|
user_profile = get_user_profile_by_email(token_payload["email"])
|
2014-01-10 23:48:05 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return_data["valid_attestation"] = True
|
|
|
|
return None
|
2016-04-21 21:07:43 +02:00
|
|
|
if not user_profile.is_active:
|
|
|
|
return_data["inactive_user"] = True
|
|
|
|
return None
|
|
|
|
if user_profile.realm.deactivated:
|
|
|
|
return_data["inactive_realm"] = True
|
2016-04-21 07:19:08 +02:00
|
|
|
return None
|
|
|
|
return user_profile
|
2014-01-10 23:48:05 +01:00
|
|
|
else:
|
|
|
|
return_data["valid_attestation"] = False
|
|
|
|
|
2013-11-04 23:16:46 +01:00
|
|
|
class ZulipRemoteUserBackend(RemoteUserBackend):
|
|
|
|
create_unknown_user = False
|
|
|
|
|
|
|
|
def authenticate(self, remote_user):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (str) -> Optional[UserProfile]
|
2013-11-04 23:16:46 +01:00
|
|
|
if not remote_user:
|
2016-01-26 03:11:31 +01:00
|
|
|
return None
|
2013-11-04 23:16:46 +01:00
|
|
|
|
|
|
|
email = remote_user_to_email(remote_user)
|
2016-04-21 07:19:08 +02:00
|
|
|
return common_get_active_user_by_email(email)
|
2015-02-06 18:30:28 +01:00
|
|
|
|
2015-10-13 23:08:05 +02:00
|
|
|
class ZulipLDAPException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
|
|
|
|
# Don't use Django LDAP's permissions functions
|
|
|
|
def has_perm(self, user, perm, obj=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (UserProfile, Any, Any) -> bool
|
|
|
|
# Using Any type is safe because we are not doing anything with
|
|
|
|
# the arguments.
|
2015-10-13 23:08:05 +02:00
|
|
|
return False
|
|
|
|
def has_module_perms(self, user, app_label):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (UserProfile, str) -> bool
|
2015-10-13 23:08:05 +02:00
|
|
|
return False
|
|
|
|
def get_all_permissions(self, user, obj=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (UserProfile, Any) -> Set
|
|
|
|
# Using Any type is safe because we are not doing anything with
|
|
|
|
# the arguments.
|
2015-10-13 23:08:05 +02:00
|
|
|
return set()
|
|
|
|
def get_group_permissions(self, user, obj=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (UserProfile, Any) -> Set
|
|
|
|
# Using Any type is safe because we are not doing anything with
|
|
|
|
# the arguments.
|
2015-10-13 23:08:05 +02:00
|
|
|
return set()
|
|
|
|
|
2013-11-21 01:30:20 +01:00
|
|
|
def django_to_ldap_username(self, username):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (text_type) -> text_type
|
2015-10-13 22:57:38 +02:00
|
|
|
if settings.LDAP_APPEND_DOMAIN:
|
2015-10-13 23:08:05 +02:00
|
|
|
if not username.endswith("@" + settings.LDAP_APPEND_DOMAIN):
|
|
|
|
raise ZulipLDAPException("Username does not match LDAP domain.")
|
2013-11-21 01:30:20 +01:00
|
|
|
return email_to_username(username)
|
|
|
|
return username
|
|
|
|
def ldap_to_django_username(self, username):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (str) -> str
|
2015-10-13 22:57:38 +02:00
|
|
|
if settings.LDAP_APPEND_DOMAIN:
|
2013-11-25 17:57:30 +01:00
|
|
|
return "@".join((username, settings.LDAP_APPEND_DOMAIN))
|
2013-11-21 01:30:20 +01:00
|
|
|
return username
|
|
|
|
|
2015-10-13 23:08:05 +02:00
|
|
|
class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
|
2016-05-18 23:55:34 +02:00
|
|
|
def authenticate(self, username, password, return_data=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (text_type, str, Optional[Dict[str, Any]]) -> Optional[str]
|
2015-10-13 23:08:05 +02:00
|
|
|
try:
|
|
|
|
username = self.django_to_ldap_username(username)
|
|
|
|
return ZulipLDAPAuthBackendBase.authenticate(self, username, password)
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
return None
|
|
|
|
except ZulipLDAPException:
|
|
|
|
return None
|
|
|
|
|
2013-11-21 01:30:20 +01:00
|
|
|
def get_or_create_user(self, username, ldap_user):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (str, _LDAPUser) -> Tuple[UserProfile, bool]
|
2013-11-21 01:30:20 +01:00
|
|
|
try:
|
2016-04-21 07:19:08 +02:00
|
|
|
user_profile = get_user_profile_by_email(username)
|
|
|
|
if not user_profile.is_active or user_profile.realm.deactivated:
|
|
|
|
raise ZulipLDAPException("Realm has been deactivated")
|
|
|
|
return user_profile, False
|
2013-11-21 01:30:20 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
2015-10-13 23:08:05 +02:00
|
|
|
domain = resolve_email_to_domain(username)
|
|
|
|
realm = get_realm(domain)
|
2016-04-21 07:19:08 +02:00
|
|
|
# No need to check for an inactive user since they don't exist yet
|
|
|
|
if realm.deactivated:
|
|
|
|
raise ZulipLDAPException("Realm has been deactivated")
|
2015-10-13 23:08:05 +02:00
|
|
|
|
|
|
|
full_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["full_name"]
|
2015-10-26 17:17:51 +01:00
|
|
|
short_name = full_name = ldap_user.attrs[full_name_attr][0]
|
2015-10-13 23:08:05 +02:00
|
|
|
if "short_name" in settings.AUTH_LDAP_USER_ATTR_MAP:
|
|
|
|
short_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["short_name"]
|
2015-10-26 17:17:51 +01:00
|
|
|
short_name = ldap_user.attrs[short_name_attr][0]
|
2013-11-21 01:30:20 +01:00
|
|
|
|
2015-10-13 23:08:05 +02:00
|
|
|
user_profile = do_create_user(username, None, realm, full_name, short_name)
|
|
|
|
return user_profile, False
|
2013-11-21 01:30:20 +01:00
|
|
|
|
2015-10-13 23:08:05 +02:00
|
|
|
# Just like ZulipLDAPAuthBackend, but doesn't let you log in.
|
|
|
|
class ZulipLDAPUserPopulator(ZulipLDAPAuthBackendBase):
|
2013-11-21 01:30:20 +01:00
|
|
|
def authenticate(self, username, password):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (text_type, str) -> None
|
2013-11-21 01:30:20 +01:00
|
|
|
return None
|
2015-08-19 02:58:20 +02:00
|
|
|
|
|
|
|
class DevAuthBackend(ZulipAuthMixin):
|
|
|
|
# Allow logging in as any user without a password.
|
|
|
|
# This is used for convenience when developing Zulip.
|
|
|
|
|
2016-06-01 02:28:27 +02:00
|
|
|
def authenticate(self, username, return_data=None):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (text_type, Optional[Dict[str, Any]]) -> UserProfile
|
2016-06-01 02:28:27 +02:00
|
|
|
return common_get_active_user_by_email(username, return_data=return_data)
|
2016-07-20 13:33:27 +02:00
|
|
|
|
2016-07-29 21:47:14 +02:00
|
|
|
class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
|
2016-07-26 12:12:01 +02:00
|
|
|
def get_email_address(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> Optional[text_type]
|
2016-07-20 13:33:27 +02:00
|
|
|
try:
|
2016-07-26 12:12:01 +02:00
|
|
|
return kwargs['response']['email']
|
2016-07-20 13:33:27 +02:00
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2016-07-26 12:12:01 +02:00
|
|
|
def get_full_name(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> text_type
|
2016-07-20 13:33:27 +02:00
|
|
|
try:
|
2016-07-26 12:12:01 +02:00
|
|
|
return kwargs['response']['name']
|
|
|
|
except KeyError:
|
|
|
|
return ''
|
2016-07-20 13:33:27 +02:00
|
|
|
|
|
|
|
def do_auth(self, *args, **kwargs):
|
2016-08-08 09:38:50 +02:00
|
|
|
# type: (*Any, **Any) -> Optional[UserProfile]
|
2016-07-20 13:33:27 +02:00
|
|
|
kwargs['return_data'] = {}
|
2016-08-01 13:06:35 +02:00
|
|
|
user_profile = None
|
|
|
|
|
|
|
|
team_id = settings.SOCIAL_AUTH_GITHUB_TEAM_ID
|
|
|
|
org_name = settings.SOCIAL_AUTH_GITHUB_ORG_NAME
|
|
|
|
|
|
|
|
if (team_id is None and org_name is None):
|
|
|
|
user_profile = GithubOAuth2.do_auth(self, *args, **kwargs)
|
|
|
|
|
|
|
|
elif (team_id):
|
|
|
|
backend = GithubTeamOAuth2(self.strategy, self.redirect_uri)
|
|
|
|
try:
|
|
|
|
user_profile = backend.do_auth(*args, **kwargs)
|
|
|
|
except AuthFailed:
|
|
|
|
logging.info("User profile not member of team.")
|
|
|
|
user_profile = None
|
|
|
|
|
|
|
|
elif (org_name):
|
|
|
|
backend = GithubOrganizationOAuth2(self.strategy, self.redirect_uri)
|
|
|
|
try:
|
|
|
|
user_profile = backend.do_auth(*args, **kwargs)
|
|
|
|
except AuthFailed:
|
|
|
|
logging.info("User profile not member of organisation.")
|
|
|
|
user_profile = None
|
|
|
|
|
2016-07-26 12:12:01 +02:00
|
|
|
return self.process_do_auth(user_profile, *args, **kwargs)
|