Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit a08823d

Browse files
committed
Allowing sign alg 'none' should not be by default.
Made verify_id_token work both for id_token and id_token_hint.
1 parent f015a18 commit a08823d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/oidcmsg/oidc/__init__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ def check_char_set(string, allowed):
239239

240240
ID_TOKEN_VERIFY_ARGS = ['keyjar', 'verify', 'encalg', 'encenc', 'sigalg',
241241
'issuer', 'allow_missing_kid', 'no_kid_issuer',
242-
'trusting', 'skew', 'nonce_storage_time', 'client_id']
242+
'trusting', 'skew', 'nonce_storage_time', 'client_id',
243+
'allow_sign_alg_none']
243244

244245
CLAIMS_WITH_VERIFIED = ['id_token', 'id_token_hint', 'request']
245246
VERIFIED_CLAIM_PREFIX = '__verified'
@@ -267,7 +268,7 @@ class TokenErrorResponse(oauth2.TokenErrorResponse):
267268
pass
268269

269270

270-
def verify_id_token(msg, check_hash=False, **kwargs):
271+
def verify_id_token(msg, check_hash=False, claim='id_token', **kwargs):
271272
# Try to decode the JWT, checks the signature
272273
args = {}
273274
for arg in ID_TOKEN_VERIFY_ARGS:
@@ -276,9 +277,20 @@ def verify_id_token(msg, check_hash=False, **kwargs):
276277
except KeyError:
277278
pass
278279

279-
_jws = jws_factory(msg["id_token"])
280+
_jws = jws_factory(msg[claim])
280281
if not _jws:
281-
raise ValueError('id_token not a signed JWT')
282+
raise ValueError('{} not a signed JWT'.format(claim))
283+
284+
if _jws.jwt.headers['alg'] == 'none':
285+
try:
286+
_allow_none = kwargs['allow_sign_alg_none']
287+
except KeyError:
288+
logger.info('Signing algorithm None not allowed')
289+
return False
290+
else:
291+
if not _allow_none:
292+
logger.info('Signing algorithm None not allowed')
293+
return False
282294

283295
_body = _jws.jwt.payload()
284296
if 'keyjar' in kwargs:
@@ -288,7 +300,7 @@ def verify_id_token(msg, check_hash=False, **kwargs):
288300
except KeyError:
289301
raise MissingRequiredAttribute('iss')
290302

291-
idt = IdToken().from_jwt(str(msg["id_token"]), **args)
303+
idt = IdToken().from_jwt(str(msg[claim]), **args)
292304
if not idt.verify(**kwargs):
293305
return False
294306

@@ -312,8 +324,8 @@ def verify_id_token(msg, check_hash=False, **kwargs):
312324
if idt["c_hash"] != left_hash(msg["code"], hfunc):
313325
raise CHashError("Failed to verify code hash", idt)
314326

315-
msg[verified_claim_name("id_token")] = idt
316-
logger.info('Verified ID Token: {}'.format(idt.to_dict()))
327+
msg[verified_claim_name(claim)] = idt
328+
logger.info('Verified {}: {}'.format(claim, idt.to_dict()))
317329

318330
return True
319331

0 commit comments

Comments
 (0)