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

Commit bdc96e8

Browse files
committed
Updated to work with the 0.4.2 cryptojwt version.
Improving test coverage.
1 parent 0f166e9 commit bdc96e8

File tree

4 files changed

+280
-113
lines changed

4 files changed

+280
-113
lines changed

src/oidcmsg/message.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from cryptojwt.jwe.jwe import JWE
1111
from cryptojwt.jwe.jwe import factory as jwe_factory
1212
from cryptojwt.jws.jws import JWS
13-
from cryptojwt.jws.jws import SimpleJWT
1413
from cryptojwt.jws.jws import factory as jws_factory
1514
from cryptojwt.key_jar import update_keyjar
1615
from cryptojwt.utils import as_unicode

src/oidcmsg/oidc/__init__.py

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from cryptojwt import as_unicode
1414
from cryptojwt.jws.utils import left_hash
15+
from cryptojwt.jwt import JWT
16+
from cryptojwt.key_jar import KeyJar
1517

1618
from oidcmsg import oauth2
1719
from oidcmsg import time_util
@@ -183,14 +185,15 @@ def claims_request_deser(val, sformat="json"):
183185

184186

185187
def dict_deser(val, sformat="json"):
186-
# never 'urlencoded'
188+
# never 'urlencoded', silently correct
187189
if sformat == "urlencoded":
188190
sformat = "json"
191+
189192
if sformat in ["dict", "json"]:
190193
if not isinstance(val, str):
191194
val = json.dumps(val)
192-
elif isinstance(val, dict):
193-
return val
195+
196+
return val
194197
else:
195198
raise ValueError('sformat can not be "{}"'.format(sformat))
196199

@@ -426,11 +429,20 @@ def verify(self, **kwargs):
426429
# Try to decode the JWT, checks the signature
427430
oidr = OpenIDRequest().from_jwt(str(self["request"]), **args)
428431

429-
# verify that nothing is change in the original message
432+
# check if something is change in the original message
430433
for key, val in oidr.items():
431434
if key in self:
432435
if self[key] != val:
433-
raise ValueError('{} != {}'.format(self[key], val))
436+
# log but otherwise ignore
437+
logger.warning('{} != {}'.format(self[key], val))
438+
439+
# remove all claims
440+
_keys = list(self.keys())
441+
for key in _keys:
442+
if key not in oidr:
443+
del self[key]
444+
445+
self.update(oidr)
434446

435447
# replace the JWT with the parsed and verified instance
436448
self[verified_claim_name("request")] = oidr
@@ -1178,48 +1190,25 @@ def factory(msgtype, **kwargs):
11781190
return oauth2.factory(msgtype, **kwargs)
11791191

11801192

1181-
def make_openid_request(arq, keys=None, userinfo_claims=None,
1182-
idtoken_claims=None, request_object_signing_alg=None,
1183-
**kwargs):
1193+
def make_openid_request(arq, keys, issuer, request_object_signing_alg, recv):
11841194
"""
11851195
Construct the JWT to be passed by value (the request parameter) or by
11861196
reference (request_uri).
11871197
The request will be signed
11881198
11891199
:param arq: The Authorization request
11901200
:param keys: Keys to use for signing/encrypting
1191-
:param userinfo_claims: UserInfo claims
1192-
:param idtoken_claims: IdToken claims
1201+
:param issuer: Who is signing this JSON Web Token
11931202
:param request_object_signing_alg: Which signing algorithm to use
1203+
:param recv: The intended receiver of the request
11941204
:return: JWT encoded OpenID request
11951205
"""
11961206

1197-
oir_args = {}
1198-
for prop in OpenIDRequest.c_param.keys():
1199-
try:
1200-
oir_args[prop] = arq[prop]
1201-
except KeyError:
1202-
pass
1203-
1204-
for attr in ["scope", "response_type"]:
1205-
if attr in oir_args:
1206-
oir_args[attr] = " ".join(oir_args[attr])
1207-
1208-
c_args = {}
1209-
if userinfo_claims is not None:
1210-
# UserInfoClaims
1211-
c_args["userinfo"] = Claims(**userinfo_claims)
1212-
1213-
if idtoken_claims is not None:
1214-
# IdTokenClaims
1215-
c_args["id_token"] = Claims(**idtoken_claims)
1207+
if isinstance(keys, KeyJar):
1208+
keys = keys.get_signing_key()
12161209

1217-
if c_args:
1218-
oir_args["claims"] = ClaimsRequest(**c_args)
1219-
1220-
oir = OpenIDRequest(**oir_args)
1221-
1222-
return oir.to_jwt(key=keys, algorithm=request_object_signing_alg)
1210+
_jwt = JWT(own_keys=keys, iss=issuer, sign_alg=request_object_signing_alg)
1211+
return _jwt.pack(arq.to_dict(), owner=issuer, recv=recv)
12231212

12241213

12251214
def claims_match(value, claimspec):
@@ -1230,7 +1219,7 @@ def claims_match(value, claimspec):
12301219
Also the text doesn't prohibit claims specification having both 'value'
12311220
and 'values'.
12321221
1233-
:param value: single value or list of values
1222+
:param value: single value
12341223
:param claimspec: None or a dictionary with 'essential', 'value' or 'values'
12351224
as keys
12361225
:return: Boolean
@@ -1253,8 +1242,8 @@ def claims_match(value, claimspec):
12531242
if matched:
12541243
break
12551244

1256-
if matched is False:
1257-
if list(claimspec.keys()) == ['essential']:
1258-
return True
1245+
# No values to test against so it's just about being there or not
1246+
if list(claimspec.keys()) == ['essential']:
1247+
return True
12591248

12601249
return matched

tests/test_4_message.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from cryptojwt.jwk.hmac import SYMKey
88
from cryptojwt.key_jar import build_keyjar
99
from cryptojwt.key_jar import KeyJar
10-
from cryptojwt.key_jar import public_keys_keyjar
1110

1211
from oidcmsg.message import json_deserializer
1312
from oidcmsg.message import json_serializer
@@ -39,7 +38,6 @@
3938
]
4039

4140
KEYJAR = build_keyjar(keys)
42-
PUBLIC_KEYJAR = public_keys_keyjar(KEYJAR, '')
4341

4442
IKEYJAR = build_keyjar(keys)
4543
IKEYJAR.issuer_keys['issuer'] = IKEYJAR.issuer_keys['']
@@ -275,7 +273,7 @@ def test_int_instead_of_string(self):
275273
def test_to_jwt(keytype, alg):
276274
msg = Message(a='foo', b='bar', c='tjoho')
277275
_jwt = msg.to_jwt(KEYJAR.get_signing_key(keytype, ''), alg)
278-
msg1 = Message().from_jwt(_jwt, PUBLIC_KEYJAR)
276+
msg1 = Message().from_jwt(_jwt, KEYJAR)
279277
assert msg1 == msg
280278

281279

@@ -285,8 +283,7 @@ def test_to_jwt(keytype, alg):
285283
])
286284
def test_to_jwe(keytype, alg, enc):
287285
msg = Message(a='foo', b='bar', c='tjoho')
288-
_jwe = msg.to_jwe(PUBLIC_KEYJAR.get_encrypt_key(keytype, ''), alg=alg,
289-
enc=enc)
286+
_jwe = msg.to_jwe(KEYJAR.get_encrypt_key(keytype, ''), alg=alg, enc=enc)
290287
msg1 = Message().from_jwe(_jwe, KEYJAR.get_encrypt_key(keytype, ''))
291288
assert msg1 == msg
292289

0 commit comments

Comments
 (0)