mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
auth: Convert RemoteUserBackend to accept a realm object.
This commit is contained in:
@@ -124,6 +124,14 @@ class AuthBackendTest(ZulipTestCase):
|
|||||||
index = getattr(user_profile.realm.authentication_methods, backend_name).number
|
index = getattr(user_profile.realm.authentication_methods, backend_name).number
|
||||||
user_profile.realm.authentication_methods.set_bit(index, False)
|
user_profile.realm.authentication_methods.set_bit(index, False)
|
||||||
user_profile.realm.save()
|
user_profile.realm.save()
|
||||||
|
if 'realm' in good_kwargs:
|
||||||
|
# Because this test is a little unfaithful to the ordering
|
||||||
|
# (i.e. we fetched the realm object before this function
|
||||||
|
# was called, when in fact it should be fetched after we
|
||||||
|
# changed the allowed authentication methods), we need to
|
||||||
|
# propagate the changes we just made to the actual realm
|
||||||
|
# object in good_kwargs.
|
||||||
|
good_kwargs['realm'] = user_profile.realm
|
||||||
self.assertIsNone(backend.authenticate(**good_kwargs))
|
self.assertIsNone(backend.authenticate(**good_kwargs))
|
||||||
user_profile.realm.authentication_methods.set_bit(index, True)
|
user_profile.realm.authentication_methods.set_bit(index, True)
|
||||||
user_profile.realm.save()
|
user_profile.realm.save()
|
||||||
@@ -282,9 +290,19 @@ class AuthBackendTest(ZulipTestCase):
|
|||||||
username = self.get_username()
|
username = self.get_username()
|
||||||
self.verify_backend(ZulipRemoteUserBackend(),
|
self.verify_backend(ZulipRemoteUserBackend(),
|
||||||
good_kwargs=dict(remote_user=username,
|
good_kwargs=dict(remote_user=username,
|
||||||
realm_subdomain='zulip'),
|
realm=get_realm('zulip')),
|
||||||
bad_kwargs=dict(remote_user=username,
|
bad_kwargs=dict(remote_user=username,
|
||||||
realm_subdomain='acme'))
|
realm=get_realm('zephyr')))
|
||||||
|
|
||||||
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',))
|
||||||
|
def test_remote_user_backend_invalid_realm(self):
|
||||||
|
# type: () -> None
|
||||||
|
username = self.get_username()
|
||||||
|
self.verify_backend(ZulipRemoteUserBackend(),
|
||||||
|
good_kwargs=dict(remote_user=username,
|
||||||
|
realm=get_realm('zulip')),
|
||||||
|
bad_kwargs=dict(remote_user=username,
|
||||||
|
realm=None))
|
||||||
|
|
||||||
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',))
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipRemoteUserBackend',))
|
||||||
@override_settings(SSO_APPEND_DOMAIN='zulip.com')
|
@override_settings(SSO_APPEND_DOMAIN='zulip.com')
|
||||||
@@ -293,9 +311,9 @@ class AuthBackendTest(ZulipTestCase):
|
|||||||
username = self.get_username(email_to_username)
|
username = self.get_username(email_to_username)
|
||||||
self.verify_backend(ZulipRemoteUserBackend(),
|
self.verify_backend(ZulipRemoteUserBackend(),
|
||||||
good_kwargs=dict(remote_user=username,
|
good_kwargs=dict(remote_user=username,
|
||||||
realm_subdomain='zulip'),
|
realm=get_realm("zulip")),
|
||||||
bad_kwargs=dict(remote_user=username,
|
bad_kwargs=dict(remote_user=username,
|
||||||
realm_subdomain='acme'))
|
realm=get_realm('zephyr')))
|
||||||
|
|
||||||
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.GitHubAuthBackend',))
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.GitHubAuthBackend',))
|
||||||
def test_github_backend(self):
|
def test_github_backend(self):
|
||||||
|
|||||||
@@ -187,7 +187,11 @@ def remote_user_sso(request):
|
|||||||
# enabled.
|
# enabled.
|
||||||
validate_login_email(remote_user_to_email(remote_user))
|
validate_login_email(remote_user_to_email(remote_user))
|
||||||
|
|
||||||
user_profile = authenticate(remote_user=remote_user, realm_subdomain=get_subdomain(request))
|
subdomain = get_subdomain(request)
|
||||||
|
realm = get_realm(subdomain)
|
||||||
|
# Since RemoteUserBackend will return None if Realm is None, we
|
||||||
|
# don't need to check whether `get_realm` returned None.
|
||||||
|
user_profile = authenticate(remote_user=remote_user, realm=realm)
|
||||||
return login_or_register_remote_user(request, remote_user, user_profile)
|
return login_or_register_remote_user(request, remote_user, user_profile)
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
|
|||||||
@@ -411,15 +411,17 @@ class ZulipRemoteUserBackend(RemoteUserBackend):
|
|||||||
create_unknown_user = False
|
create_unknown_user = False
|
||||||
|
|
||||||
def authenticate(self, remote_user: Optional[str],
|
def authenticate(self, remote_user: Optional[str],
|
||||||
realm_subdomain: Optional[str]=None) -> Optional[UserProfile]:
|
realm: Optional[Realm]=None) -> Optional[UserProfile]:
|
||||||
if not remote_user:
|
if realm is None:
|
||||||
|
return None
|
||||||
|
if remote_user is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
email = remote_user_to_email(remote_user)
|
email = remote_user_to_email(remote_user)
|
||||||
user_profile = common_get_active_user_by_email(email)
|
user_profile = common_get_active_user_by_email(email)
|
||||||
if user_profile is None:
|
if user_profile is None:
|
||||||
return None
|
return None
|
||||||
if not user_matches_subdomain(realm_subdomain, user_profile):
|
if not user_matches_subdomain(realm.subdomain, user_profile):
|
||||||
return None
|
return None
|
||||||
if not auth_enabled_helper(["RemoteUser"], user_profile.realm):
|
if not auth_enabled_helper(["RemoteUser"], user_profile.realm):
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user