Skip to content

Commit 6897f2d

Browse files
committed
fix: use auth_time in Python CLI
1 parent 224ba7b commit 6897f2d

File tree

1 file changed

+11
-33
lines changed

1 file changed

+11
-33
lines changed

perun-cli-python/perun/oidc/__init__.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, perun_instance: PerunInstance, encryption_password: str, mfa:
5454
PerunInstance.einfra: {
5555
'metadata_url': 'https://login.e-infra.cz/oidc/.well-known/openid-configuration',
5656
'client_id': '363b656e-d139-4290-99cd-ee64eeb830d5',
57-
'scopes': 'openid perun_api perun_admin offline_access authn_details',
57+
'scopes': 'openid perun_api perun_admin offline_access',
5858
'perun_api_url': 'https://perun-api.e-infra.cz/oauth/rpc',
5959
'mfa': True
6060
},
@@ -75,21 +75,21 @@ def __init__(self, perun_instance: PerunInstance, encryption_password: str, mfa:
7575
PerunInstance.idm_test: {
7676
'metadata_url': 'https://oidc.muni.cz/oidc/.well-known/openid-configuration',
7777
'client_id': '5a730abc-6553-4fc4-af9a-21c75c46e0c2',
78-
'scopes': 'openid perun_api perun_admin offline_access profile authn_details',
78+
'scopes': 'openid perun_api perun_admin offline_access profile',
7979
'perun_api_url': 'https://idm-test.ics.muni.cz/oauth/rpc',
8080
'mfa': True
8181
},
8282
PerunInstance.idm: {
8383
'metadata_url': 'https://oidc.muni.cz/oidc/.well-known/openid-configuration',
8484
'client_id': '5a730abc-6553-4fc4-af9a-21c75c46e0c2',
85-
'scopes': 'openid perun_api perun_admin offline_access profile authn_details',
85+
'scopes': 'openid perun_api perun_admin offline_access profile',
8686
'perun_api_url': 'https://idm.ics.muni.cz/oauth/rpc',
8787
'mfa': True
8888
},
8989
PerunInstance.elixir: {
9090
'metadata_url': 'https://login.elixir-czech.org/oidc/.well-known/openid-configuration',
9191
'client_id': 'da97db9f-b511-4c72-b71f-daab24b86884',
92-
'scopes': 'openid perun_api perun_admin offline_access profile authn_details',
92+
'scopes': 'openid perun_api perun_admin offline_access profile',
9393
'perun_api_url': 'https://elixir-api.aai.lifescience-ri.eu/oauth/rpc',
9494
'mfa': True
9595
},
@@ -229,8 +229,8 @@ def __verify_token(self, token: str, token_type: str) -> bool:
229229
print(' name:', decoded_token['name'])
230230
if 'acr' in decoded_token:
231231
print(' acr:', decoded_token['acr'])
232-
if 'authn_instant' in decoded_token:
233-
print(' authn_instant:', isoparse(decoded_token['authn_instant']).astimezone())
232+
if 'auth_time' in decoded_token:
233+
print(' auth_time:', datetime.fromtimestamp(decoded_token['auth_time']).astimezone())
234234
if self.mfa and token_type == 'id':
235235
acr = decoded_token.get('acr')
236236
if acr is None or acr != 'https://refeds.org/profile/mfa':
@@ -271,35 +271,13 @@ def __verify_mfa(self) -> bool:
271271
print('MFA not detected, id_token has acr:', acr)
272272
return False
273273
# get time of authentication
274-
authn_instant = decoded_id_token.get('authn_instant')
275-
if authn_instant is not None:
276-
authn_instant = isoparse(authn_instant).astimezone()
277-
if self.debug:
278-
print('got authn_instant from id_token:', authn_instant)
279-
else:
280-
# try to get it from userInfo
281-
access_token = self.tokens.get('access_token')
282-
decoded_access_token = jwt.decode(access_token,
283-
self.pyJWKClient.get_signing_key_from_jwt(access_token).key,
284-
algorithms=['RS256', 'ES256'],
285-
audience=self.CLIENT_ID)
286-
if 'authn_details' not in decoded_access_token['scope']:
287-
print('WARNING: cannot get time of MFA', file=sys.stderr)
288-
return False
289-
# call userInfo endpoint to get authn_instant
290-
userinfo_response = requests.get(self.USERINFO_ENDPOINT_URL,
291-
headers={'Authorization': 'Bearer ' + access_token})
292-
if userinfo_response.status_code != 200:
293-
print('Error calling userInfo endpoint')
294-
print(userinfo_response)
295-
raise typer.Exit(code=1)
296-
authn_instant = isoparse(userinfo_response.json().get('authn_instant')).astimezone()
297-
if self.debug:
298-
print('got authn_instant from userInfo:', authn_instant)
274+
auth_time = decoded_id_token.get('auth_time')
275+
if auth_time is not None and self.debug:
276+
print('got auth_time from id_token:', datetime.fromtimestamp(decoded_token['auth_time']).astimezone())
299277
# check that time of MFA is not older than required
300-
if time.time() - authn_instant.timestamp() > self.mfa_valid_seconds:
278+
if time.time() - auth_time > self.mfa_valid_seconds:
301279
if self.debug:
302-
print('MFA is too old: ', authn_instant, 'max is', self.mfa_valid_seconds, 'seconds')
280+
print('MFA is too old: ', auth_time, 'max is', self.mfa_valid_seconds, 'seconds')
303281
return False
304282
if self.debug:
305283
print("MFA verified")

0 commit comments

Comments
 (0)