Skip to content

Commit

Permalink
Additional logging for auth errors on custom backends (#1271)
Browse files Browse the repository at this point in the history
* additional logging for auth errors on custom backends

Signed-off-by: Paul S. Schweigert <[email protected]>

* pylint warning

Signed-off-by: Paul S. Schweigert <[email protected]>

* pylint 2

Signed-off-by: Paul S. Schweigert <[email protected]>

* switch to if-elf-else blocks

Signed-off-by: Paul S. Schweigert <[email protected]>

* update warning messages

Signed-off-by: Paul S. Schweigert <[email protected]>

* lint

Signed-off-by: Paul S. Schweigert <[email protected]>

---------

Signed-off-by: Paul S. Schweigert <[email protected]>
  • Loading branch information
psschwei authored Apr 7, 2024
1 parent 6394d1c commit 07a82a8
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion gateway/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ def safe_request(request: Callable) -> Optional[Dict[str, Any]]:
result = json.loads(response.text)
except Exception as json_exception: # pylint: disable=broad-exception-caught
logger.error(json_exception)
if response is not None and not response.ok:
logger.error("%d : %s", response.status_code, response.text)

return result


class CustomTokenBackend(authentication.BaseAuthentication):
"""Custom token backend for authentication against 3rd party auth service."""

def authenticate(self, request):
def authenticate(self, request): # pylint: disable=too-many-branches
auth_url = settings.SETTINGS_TOKEN_AUTH_URL
verification_url = settings.SETTINGS_TOKEN_AUTH_VERIFICATION_URL
auth_header = request.META.get("HTTP_AUTHORIZATION")
Expand Down Expand Up @@ -88,6 +90,29 @@ def authenticate(self, request):
except User.DoesNotExist:
user = User(username=user_id)
user.save()
elif user_id is None:
logger.warning("Problems authenticating: No user id.")
else: # not verified
logger.warning("Problems authenticating: User is not verified.")

else: # verification_data is None
logger.warning(
"Problems authenticating: No verification data returned from request."
)

else: # auth_data is None
logger.warning(
"Problems authenticating: No authorization data returned from auth url."
)

elif auth_header is None:
logger.warning(
"Problems authenticating: User did not provide authorization token."
)
else: # auth_url is None
logger.warning(
"Problems authenticating: No auth url: something is broken in our settings."
)

return user, CustomToken(token.encode()) if token else None

Expand Down

0 comments on commit 07a82a8

Please sign in to comment.