Skip to content
Snippets Groups Projects
Commit 7fddad7e authored by Antoine Lambert's avatar Antoine Lambert
Browse files

backends: Report only relevant exceptions to sentry

When catching KeycloakError exceptions, other exception types
will be then raised like ValueError or AuthenticationFailed.

Those will be processed by django applications using these auth
middlewares so there is no need to duplicate error reports in
sentry here.
parent 882f35e4
No related branches found
Tags v0.5.3
No related merge requests found
# Copyright (C) 2020-2021 The Software Heritage developers
# Copyright (C) 2020-2022 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -121,7 +121,8 @@ class OIDCAuthorizationCodePKCEBackend:
if error_msg == "invalid_grant: Session not active":
# user session no longer active, remove oidc profile from cache
cache.delete(cache_key)
sentry_sdk.capture_exception(ke)
else:
sentry_sdk.capture_exception(ke)
return None
except Exception as e:
sentry_sdk.capture_exception(e)
......@@ -204,8 +205,7 @@ class OIDCBearerTokenAuthentication(BaseAuthentication):
# create Django user
user = oidc_user_from_decoded_token(decoded_token, oidc_client.client_id)
except UnicodeEncodeError as e:
sentry_sdk.capture_exception(e)
except UnicodeEncodeError:
raise ValidationError("Invalid bearer token")
except KeycloakError as ke:
error_msg = keycloak_error_message(ke)
......@@ -217,10 +217,8 @@ class OIDCBearerTokenAuthentication(BaseAuthentication):
"Bearer token expired after a long period of inactivity; "
"please generate a new one."
)
sentry_sdk.capture_exception(ke)
raise AuthenticationFailed(error_msg)
except Exception as e:
sentry_sdk.capture_exception(e)
raise AuthenticationFailed(str(e))
return user, None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment