Changeset View
Changeset View
Standalone View
Standalone View
swh/web/auth/backends.py
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | def authenticate(self, request): | ||||
if userinfo: | if userinfo: | ||||
user = _oidc_user_from_info(userinfo) | user = _oidc_user_from_info(userinfo) | ||||
else: | else: | ||||
# get OIDC userinfo | # get OIDC userinfo | ||||
userinfo = _oidc_client.userinfo(token) | userinfo = _oidc_client.userinfo(token) | ||||
# create Django user | # create Django user | ||||
user = _oidc_user_from_info(userinfo) | user = _oidc_user_from_info(userinfo) | ||||
# cache userinfo until token expires | # cache userinfo until token expires | ||||
max_ttl = decoded['exp'] - decoded['auth_time'] - 1 | max_ttl = decoded['exp'] - decoded['auth_time'] | ||||
ttl = decoded['exp'] - int(timezone.now().timestamp()) - 1 | ttl = decoded['exp'] - int(timezone.now().timestamp()) | ||||
ttl = max(0, min(ttl, max_ttl)) | ttl = max(0, min(ttl, max_ttl)) | ||||
cache.set(decoded['sub'], userinfo, timeout=ttl) | cache.set(decoded['sub'], userinfo, timeout=ttl) | ||||
except Exception as e: | except Exception as e: | ||||
sentry_sdk.capture_exception(e) | sentry_sdk.capture_exception(e) | ||||
raise AuthenticationFailed(str(e)) | raise AuthenticationFailed(str(e)) | ||||
return user, None | return user, None |