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

Commit 503cf86

Browse files
committed
Deal with signing algorithm == None
1 parent bee31d2 commit 503cf86

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

src/oidcmsg/oidc/__init__.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# encoding: utf-8
2-
from urllib.parse import urlencode
3-
from urllib.parse import urlparse
4-
52
import inspect
63
import json
74
import logging
85
import sys
96
import time
7+
from urllib.parse import urlencode
8+
from urllib.parse import urlparse
109

1110
from cryptojwt import as_unicode
1211
from cryptojwt.exception import UnsupportedAlgorithm
@@ -29,7 +28,6 @@
2928
from oidcmsg.exception import SchemeError
3029
from oidcmsg.exception import VerificationError
3130
from oidcmsg.message import Message
32-
from oidcmsg.message import msg_ser
3331
from oidcmsg.message import OPTIONAL_LIST_OF_SP_SEP_STRINGS
3432
from oidcmsg.message import OPTIONAL_LIST_OF_STRINGS
3533
from oidcmsg.message import OPTIONAL_MESSAGE
@@ -39,6 +37,7 @@
3937
from oidcmsg.message import SINGLE_OPTIONAL_JSON
4038
from oidcmsg.message import SINGLE_OPTIONAL_STRING
4139
from oidcmsg.message import SINGLE_REQUIRED_STRING
40+
from oidcmsg.message import msg_ser
4241
from oidcmsg.oauth2 import ResponseMessage
4342
from oidcmsg.time_util import utc_time_sans_frac
4443

@@ -279,16 +278,16 @@ def verify_id_token(msg, check_hash=False, claim='id_token', **kwargs):
279278
raise ValueError('{} not a signed JWT'.format(claim))
280279

281280
if _jws.jwt.headers['alg'] == 'none':
282-
try:
283-
_allow_none = kwargs['allow_sign_alg_none']
284-
except KeyError:
285-
logger.info('Signing algorithm None not allowed')
286-
raise UnsupportedAlgorithm('Signing algorithm None not allowed')
287-
else:
288-
if not _allow_none:
281+
_signed = False
282+
_sign_alg = kwargs.get("sigalg")
283+
if _sign_alg == "none":
284+
_allowed = True
285+
else: # There might or might not be a specified signing alg
286+
if kwargs.get('allow_sign_alg_none', False) is False:
289287
logger.info('Signing algorithm None not allowed')
290288
raise UnsupportedAlgorithm('Signing algorithm None not allowed')
291289
else:
290+
_signed = True
292291
if "allowed_sign_alg" in kwargs:
293292
if _jws.jwt.headers['alg'] != kwargs["allowed_sign_alg"]:
294293
_msg = "Wrong token signing algorithm, {} != {}".format(
@@ -297,7 +296,7 @@ def verify_id_token(msg, check_hash=False, claim='id_token', **kwargs):
297296
raise UnsupportedAlgorithm(_msg)
298297

299298
_body = _jws.jwt.payload()
300-
if 'keyjar' in kwargs:
299+
if _signed and 'keyjar' in kwargs:
301300
try:
302301
if _body['iss'] not in kwargs['keyjar']:
303302
raise ValueError('Unknown issuer')
@@ -308,7 +307,7 @@ def verify_id_token(msg, check_hash=False, claim='id_token', **kwargs):
308307
if not idt.verify(**kwargs):
309308
return False
310309

311-
if check_hash:
310+
if _signed and check_hash:
312311
_alg = idt.jws_header["alg"]
313312
hfunc = "HS" + _alg[-3:]
314313

@@ -353,7 +352,7 @@ def verify(self, **kwargs):
353352
class UserInfoRequest(Message):
354353
c_param = {
355354
"access_token": SINGLE_OPTIONAL_STRING,
356-
}
355+
}
357356

358357

359358
class AuthorizationResponse(oauth2.AuthorizationResponse,
@@ -368,7 +367,7 @@ class AuthorizationResponse(oauth2.AuthorizationResponse,
368367
"id_token": SINGLE_OPTIONAL_IDTOKEN,
369368
# Below is REQUIRED if doing session management
370369
"session_state": SINGLE_OPTIONAL_STRING
371-
})
370+
})
372371

373372
def verify(self, **kwargs):
374373
super(AuthorizationResponse, self).verify(**kwargs)
@@ -422,13 +421,13 @@ class AuthorizationRequest(oauth2.AuthorizationRequest):
422421
"request_uri": SINGLE_OPTIONAL_STRING,
423422
# "session_state": SINGLE_OPTIONAL_STRING,
424423
"response_mode": SINGLE_OPTIONAL_STRING,
425-
}
426-
)
424+
}
425+
)
427426
c_allowed_values = oauth2.AuthorizationRequest.c_allowed_values.copy()
428427
c_allowed_values.update({
429428
"display": ["page", "popup", "touch", "wap"],
430429
"prompt": ["none", "login", "consent", "select_account"]
431-
})
430+
})
432431

433432
def verify(self, **kwargs):
434433
"""Authorization Request parameters that are OPTIONAL in the OAuth 2.0
@@ -518,12 +517,12 @@ class AccessTokenRequest(oauth2.AccessTokenRequest):
518517
c_param.update({
519518
"client_assertion_type": SINGLE_OPTIONAL_STRING,
520519
"client_assertion": SINGLE_OPTIONAL_STRING
521-
})
520+
})
522521
c_default = {"grant_type": "authorization_code"}
523522
c_allowed_values = {
524523
"client_assertion_type": [
525524
"urn:ietf:params:oauth:client-assertion-type:jwt-bearer"],
526-
}
525+
}
527526

528527

529528
class AddressClaim(Message):
@@ -534,7 +533,7 @@ class AddressClaim(Message):
534533
"region": SINGLE_OPTIONAL_STRING,
535534
"postal_code": SINGLE_OPTIONAL_STRING,
536535
"country": SINGLE_OPTIONAL_STRING
537-
}
536+
}
538537

539538

540539
class OpenIDSchema(ResponseMessage):
@@ -563,7 +562,7 @@ class OpenIDSchema(ResponseMessage):
563562
"updated_at": SINGLE_OPTIONAL_INT,
564563
"_claim_names": OPTIONAL_MESSAGE,
565564
"_claim_sources": OPTIONAL_MESSAGE
566-
})
565+
})
567566

568567
def verify(self, **kwargs):
569568
super(OpenIDSchema, self).verify(**kwargs)
@@ -627,12 +626,12 @@ class RegistrationRequest(Message):
627626
"frontchannel_logout_session_required": SINGLE_OPTIONAL_BOOLEAN,
628627
"backchannel_logout_uri": SINGLE_OPTIONAL_STRING,
629628
"backchannel_logout_session_supported": SINGLE_OPTIONAL_BOOLEAN
630-
}
629+
}
631630
c_default = {"application_type": "web", "response_types": ["code"]}
632631
c_allowed_values = {
633632
"application_type": ["native", "web"],
634633
"subject_type": ["public", "pairwise"]
635-
}
634+
}
636635

637636
def verify(self, **kwargs):
638637
super(RegistrationRequest, self).verify(**kwargs)
@@ -675,7 +674,7 @@ class RegistrationResponse(ResponseMessage):
675674
"registration_client_uri": SINGLE_OPTIONAL_STRING,
676675
"client_id_issued_at": SINGLE_OPTIONAL_INT,
677676
"client_secret_expires_at": SINGLE_OPTIONAL_INT
678-
})
677+
})
679678
c_param.update(RegistrationRequest.c_param)
680679

681680
def verify(self, **kwargs):
@@ -702,7 +701,7 @@ class ClientRegistrationErrorResponse(oauth2.ResponseMessage):
702701
"error": ["invalid_redirect_uri",
703702
"invalid_client_metadata",
704703
"invalid_configuration_parameter"]
705-
}
704+
}
706705

707706

708707
class IdToken(OpenIDSchema):
@@ -722,7 +721,7 @@ class IdToken(OpenIDSchema):
722721
"azp": SINGLE_OPTIONAL_STRING,
723722
"sub_jwk": SINGLE_OPTIONAL_STRING,
724723
"sid": SINGLE_OPTIONAL_STRING
725-
})
724+
})
726725
hashable = {'access_token': 'at_hash', 'code': 'c_hash'}
727726

728727
def val_hash(self, alg):
@@ -896,7 +895,7 @@ class ProviderConfigurationResponse(ResponseMessage):
896895
# "jwk_encryption_url": SINGLE_OPTIONAL_STRING,
897896
# "x509_url": SINGLE_REQUIRED_STRING,
898897
# "x509_encryption_url": SINGLE_OPTIONAL_STRING,
899-
})
898+
})
900899
c_default = {
901900
"version": "3.0",
902901
"token_endpoint_auth_methods_supported": [
@@ -906,7 +905,7 @@ class ProviderConfigurationResponse(ResponseMessage):
906905
"request_uri_parameter_supported": True,
907906
"require_request_uri_registration": True,
908907
"grant_types_supported": ["authorization_code", "implicit"]
909-
}
908+
}
910909

911910
def verify(self, **kwargs):
912911
super(ProviderConfigurationResponse, self).verify(**kwargs)
@@ -932,7 +931,7 @@ def verify(self, **kwargs):
932931
raise ValueError('Issuer ID invalid')
933932

934933
if any("code" in rt for rt in self[
935-
"response_types_supported"]) and "token_endpoint" not in self:
934+
"response_types_supported"]) and "token_endpoint" not in self:
936935
raise MissingRequiredAttribute("token_endpoint")
937936

938937
return True
@@ -948,7 +947,7 @@ class JsonWebToken(Message):
948947
"nbf": SINGLE_OPTIONAL_INT,
949948
"iat": SINGLE_OPTIONAL_INT,
950949
"jti": SINGLE_OPTIONAL_STRING,
951-
}
950+
}
952951

953952
def verify(self, **kwargs):
954953
super(JsonWebToken, self).verify(**kwargs)
@@ -1010,7 +1009,7 @@ class AuthnToken(JsonWebToken):
10101009
"jti": SINGLE_REQUIRED_STRING,
10111010
"exp": SINGLE_REQUIRED_INT,
10121011
"iat": SINGLE_OPTIONAL_INT,
1013-
}
1012+
}
10141013

10151014

10161015
def jwt_deser(val, sformat="json"):
@@ -1030,14 +1029,14 @@ class UserInfoErrorResponse(oauth2.ResponseMessage):
10301029
c_allowed_values = {
10311030
"error": ["invalid_schema", "invalid_request",
10321031
"invalid_token", "insufficient_scope"]
1033-
}
1032+
}
10341033

10351034

10361035
class DiscoveryRequest(Message):
10371036
c_param = {
10381037
"resource": SINGLE_REQUIRED_STRING,
10391038
"rel": SINGLE_REQUIRED_STRING
1040-
}
1039+
}
10411040

10421041

10431042
class Link(Message):
@@ -1050,7 +1049,7 @@ class Link(Message):
10501049
"href": SINGLE_OPTIONAL_STRING,
10511050
"titles": SINGLE_OPTIONAL_DICT,
10521051
"properties": SINGLE_OPTIONAL_DICT
1053-
}
1052+
}
10541053

10551054

10561055
def _l_deser(val, sformat):
@@ -1115,14 +1114,14 @@ class JRD(ResponseMessage):
11151114
"aliases": OPTIONAL_LIST_OF_STRINGS,
11161115
"properties": SINGLE_OPTIONAL_DICT,
11171116
"links": REQUIRED_LINKS
1118-
}
1117+
}
11191118

11201119

11211120
class WebFingerRequest(Message):
11221121
c_param = {
11231122
"resource": SINGLE_REQUIRED_STRING,
11241123
"rel": SINGLE_REQUIRED_STRING
1125-
}
1124+
}
11261125
c_default = {"rel": "http://openid.net/specs/connect/1.0/issuer"}
11271126

11281127

@@ -1139,7 +1138,7 @@ class ClaimsRequest(Message):
11391138
c_param = {
11401139
"userinfo": OPTIONAL_MULTIPLE_Claims,
11411140
"id_token": OPTIONAL_MULTIPLE_Claims
1142-
}
1141+
}
11431142

11441143

11451144
def factory(msgtype, **kwargs):

0 commit comments

Comments
 (0)