rest: Simplify authentication error handling.

This pure refactor removes a now unnecessarily nested else clause,
helping keep this key piece of code readable.
This commit is contained in:
Tim Abbott 2020-08-25 16:55:06 -07:00
parent c355f6b8d8
commit 1f90a31fa3
1 changed files with 9 additions and 12 deletions

View File

@ -140,19 +140,16 @@ def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:
target_function = authenticated_rest_api_view(
is_webhook='allow_incoming_webhooks' in view_flags,
)(target_function)
# Pick a way to tell user they're not authed based on how the request was made
else:
# Logged out user accessing an endpoint with anonymous user access on JSON; proceed.
# `allow_anonymous_user_web` calls are only restricted to /json calls used
# by our webapp.
elif request.path.startswith("/json") and 'allow_anonymous_user_web' in view_flags:
# For endpoints that support anonymous web access, we do that.
# TODO: Allow /api calls when this is stable enough.
if request.path.startswith("/json") and 'allow_anonymous_user_web' in view_flags:
auth_kwargs = dict(allow_unauthenticated=True)
target_function = csrf_protect(authenticated_json_view(
target_function, **auth_kwargs))
else:
# Don't allow anonymous queries to endpoints witout `allow_anonymous_user_web` flag.
raise MissingAuthenticationError()
auth_kwargs = dict(allow_unauthenticated=True)
target_function = csrf_protect(authenticated_json_view(
target_function, **auth_kwargs))
else:
# Otherwise, throw an authentication error; our middleware
# will generate the appropriate HTTP response.
raise MissingAuthenticationError()
if request.method not in ["GET", "POST"]:
# process_as_post needs to be the outer decorator, because