decorator: Reorganize remote Zulip server code into one block.

This commit is contained in:
Tim Abbott 2017-08-14 15:39:36 -07:00
parent 052e173aae
commit a6647f335f
1 changed files with 7 additions and 7 deletions

View File

@ -183,11 +183,17 @@ def validate_api_key(request, role, api_key, is_webhook=False):
# Remove whitespace to protect users from trivial errors. # Remove whitespace to protect users from trivial errors.
role, api_key = role.strip(), api_key.strip() role, api_key = role.strip(), api_key.strip()
if is_remote_server(role): if settings.ZILENCER_ENABLED and is_remote_server(role):
try: try:
profile = get_remote_server_by_uuid(role) # type: Union[UserProfile, RemoteZulipServer] profile = get_remote_server_by_uuid(role) # type: Union[UserProfile, RemoteZulipServer]
except RemoteZulipServer.DoesNotExist: except RemoteZulipServer.DoesNotExist:
raise JsonableError(_("Invalid Zulip server: %s") % (role,)) raise JsonableError(_("Invalid Zulip server: %s") % (role,))
if api_key != profile.api_key:
raise JsonableError(_("Invalid API key"))
if not check_subdomain(get_subdomain(request), ""):
raise JsonableError(_("This API key only works on the root subdomain"))
return profile
else: else:
try: try:
profile = get_user_profile_by_email(role) profile = get_user_profile_by_email(role)
@ -197,12 +203,6 @@ def validate_api_key(request, role, api_key, is_webhook=False):
if api_key != profile.api_key: if api_key != profile.api_key:
raise JsonableError(_("Invalid API key")) raise JsonableError(_("Invalid API key"))
# early exit for RemoteZulipServer instances
if settings.ZILENCER_ENABLED and isinstance(profile, RemoteZulipServer):
if not check_subdomain(get_subdomain(request), ""):
raise JsonableError(_("This API key only works on the root subdomain"))
return profile
profile = cast(UserProfile, profile) # is UserProfile profile = cast(UserProfile, profile) # is UserProfile
if not profile.is_active: if not profile.is_active:
raise JsonableError(_("Account not active")) raise JsonableError(_("Account not active"))