Changeset View
Changeset View
Standalone View
Standalone View
swh/auth/django/backends.py
Show First 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | ) -> Optional[OIDCUser]: | ||||
except Exception as e: | except Exception as e: | ||||
sentry_sdk.capture_exception(e) | sentry_sdk.capture_exception(e) | ||||
return user | return user | ||||
def get_user(self, user_id: int) -> Optional[OIDCUser]: | def get_user(self, user_id: int) -> Optional[OIDCUser]: | ||||
# get oidc profile from cache | # get oidc profile from cache | ||||
oidc_client = keycloak_oidc_client() | oidc_client = keycloak_oidc_client() | ||||
oidc_profile = cache.get(oidc_profile_cache_key(oidc_client, user_id)) | cache_key = oidc_profile_cache_key(oidc_client, user_id) | ||||
oidc_profile = cache.get(cache_key) | |||||
if oidc_profile: | if oidc_profile: | ||||
try: | try: | ||||
user = oidc_user_from_profile(oidc_client, oidc_profile) | user = oidc_user_from_profile(oidc_client, oidc_profile) | ||||
# update cached oidc profile if needed | # update cached oidc profile if needed | ||||
_update_cached_oidc_profile(oidc_client, oidc_profile, user) | _update_cached_oidc_profile(oidc_client, oidc_profile, user) | ||||
# restore auth backend | # restore auth backend | ||||
setattr(user, "backend", f"{__name__}.{self.__class__.__name__}") | setattr(user, "backend", f"{__name__}.{self.__class__.__name__}") | ||||
return user | return user | ||||
except KeycloakError as ke: | |||||
error_msg = keycloak_error_message(ke) | |||||
if error_msg == "invalid_grant: Session not active": | |||||
# user session no longer active, remove oidc profile from cache | |||||
cache.delete(cache_key) | |||||
return None | |||||
except Exception as e: | except Exception as e: | ||||
sentry_sdk.capture_exception(e) | sentry_sdk.capture_exception(e) | ||||
return None | return None | ||||
else: | else: | ||||
return None | return None | ||||
class OIDCBearerTokenAuthentication(BaseAuthentication): | class OIDCBearerTokenAuthentication(BaseAuthentication): | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |