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

Commit 5532cd5

Browse files
committed
Configuration args should be collected under kwargs header.
Make both possible for a while. Deprecation warning if you don't have kwargs.
1 parent 49d51a5 commit 5532cd5

File tree

4 files changed

+61
-42
lines changed

4 files changed

+61
-42
lines changed

src/oidcendpoint/token_handler.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import hashlib
33
import logging
4+
import warnings
45

56
from cryptography.fernet import Fernet
67
from cryptography.fernet import InvalidToken
@@ -258,7 +259,28 @@ def init_token_handler(ec, spec, typ):
258259
else:
259260
cls = importer(_cls)
260261

261-
return cls(typ=typ, ec=ec, **spec)
262+
_kwargs = spec.get('kwargs')
263+
if _kwargs is None:
264+
if cls != DefaultToken:
265+
warnings.warn(
266+
"Token initialisation arguments should be grouped under 'kwargs'.",
267+
DeprecationWarning,
268+
stacklevel=2,
269+
)
270+
_kwargs = spec
271+
272+
return cls(typ=typ, ec=ec, **_kwargs)
273+
274+
275+
def _add_passwd(keyjar, conf, kid):
276+
if keyjar:
277+
_keys = keyjar.get_encrypt_key(key_type="oct", kid=kid)
278+
if _keys:
279+
pw = as_unicode(_keys[0].k)
280+
if "kwargs" in conf:
281+
conf["kwargs"]["password"] = pw
282+
else:
283+
conf["password"] = pw
262284

263285

264286
def factory(ec, code=None, token=None, refresh=None, jwks_def=None, **kwargs):
@@ -282,26 +304,15 @@ def factory(ec, code=None, token=None, refresh=None, jwks_def=None, **kwargs):
282304
args = {}
283305

284306
if code:
285-
if kj:
286-
_keys = kj.get_encrypt_key(key_type="oct", kid="code")
287-
if _keys:
288-
code["password"] = as_unicode(_keys[0].k)
307+
_add_passwd(kj, code, "code")
289308
args["code_handler"] = init_token_handler(ec, code, TTYPE["code"])
290309

291310
if token:
292-
if kj:
293-
_keys = kj.get_encrypt_key(key_type="oct", kid="token")
294-
if _keys:
295-
token["password"] = as_unicode(_keys[0].k)
311+
_add_passwd(kj, token, "token")
296312
args["access_token_handler"] = init_token_handler(ec, token, TTYPE["token"])
297313

298314
if refresh:
299-
if kj:
300-
_keys = kj.get_encrypt_key(key_type="oct", kid="refresh")
301-
if _keys:
302-
refresh["password"] = as_unicode(_keys[0].k)
303-
args["refresh_token_handler"] = init_token_handler(
304-
ec, refresh, TTYPE["refresh"]
305-
)
315+
_add_passwd(kj, refresh, "refresh")
316+
args["refresh_token_handler"] = init_token_handler(ec, refresh, TTYPE["refresh"])
306317

307318
return TokenHandler(**args)

tests/test_27_jwt_token.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from cryptojwt.jwt import JWT
55
from cryptojwt.jwt import utc_time_sans_frac
66
from cryptojwt.key_jar import init_key_jar
7+
from oidcmsg.oidc import AccessTokenRequest
8+
from oidcmsg.oidc import AuthorizationRequest
9+
710
from oidcendpoint import user_info
811
from oidcendpoint.client_authn import verify_client
912
from oidcendpoint.endpoint_context import EndpointContext
@@ -15,8 +18,6 @@
1518
from oidcendpoint.oidc.token import AccessToken
1619
from oidcendpoint.session import setup_session
1720
from oidcendpoint.user_authn.authn_context import INTERNETPROTOCOLPASSWORD
18-
from oidcmsg.oidc import AccessTokenRequest
19-
from oidcmsg.oidc import AuthorizationRequest
2021

2122
KEYDEFS = [
2223
{"type": "RSA", "key": "", "use": ["sig"]},
@@ -109,15 +110,17 @@ def create_endpoint(self):
109110
"code": {"lifetime": 600},
110111
"token": {
111112
"class": "oidcendpoint.jwt_token.JWTToken",
112-
"lifetime": 3600,
113-
"add_claims": [
114-
"email",
115-
"email_verified",
116-
"phone_number",
117-
"phone_number_verified",
118-
],
119-
"add_claim_by_scope": True,
120-
"aud": ["https://example.org/appl"],
113+
"kwargs": {
114+
"lifetime": 3600,
115+
"add_claims": [
116+
"email",
117+
"email_verified",
118+
"phone_number",
119+
"phone_number_verified",
120+
],
121+
"add_claim_by_scope": True,
122+
"aud": ["https://example.org/appl"],
123+
}
121124
},
122125
},
123126
"endpoint": {
@@ -217,6 +220,6 @@ def test_is_expired(self):
217220
assert handler.is_expired(_dic["access_token"]) is False
218221

219222
assert (
220-
handler.is_expired(_dic["access_token"], utc_time_sans_frac() + 4000)
221-
is True
223+
handler.is_expired(_dic["access_token"], utc_time_sans_frac() + 4000)
224+
is True
222225
)

tests/test_31_introspection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ def create_endpoint(self, jwt_token):
165165
"template_dir": "template",
166166
}
167167
if jwt_token:
168-
conf["token_handler_args"]["token"]["class"] = \
169-
"oidcendpoint.jwt_token.JWTToken"
168+
conf["token_handler_args"]["token"] = {
169+
"class": "oidcendpoint.jwt_token.JWTToken",
170+
"kwargs": {}
171+
}
170172
endpoint_context = EndpointContext(conf)
171173
endpoint_context.cdb["client_1"] = {
172174
"client_secret": "hemligt",

tests/test_40_oauth2_pushed_authorization.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
from cryptojwt import JWT
66
from cryptojwt.jwt import remove_jwt_parameters
77
from cryptojwt.key_jar import init_key_jar
8+
from oidcmsg.message import Message
9+
from oidcmsg.oauth2 import AuthorizationRequest
10+
811
from oidcendpoint.cookie import CookieDealer
912
from oidcendpoint.endpoint_context import EndpointContext
1013
from oidcendpoint.id_token import IDToken
1114
from oidcendpoint.oauth2.authorization import Authorization
1215
from oidcendpoint.oauth2.pushed_authorization import PushedAuthorization
1316
from oidcendpoint.oidc.provider_config import ProviderConfiguration
1417
from oidcendpoint.oidc.registration import Registration
15-
from oidcmsg.message import Message
16-
from oidcmsg.oauth2 import AuthorizationRequest
1718

1819
CAPABILITIES = {
1920
"subject_types_supported": ["public", "pairwise"],
@@ -83,15 +84,17 @@ def create_endpoint(self):
8384
"code": {"lifetime": 600},
8485
"token": {
8586
"class": "oidcendpoint.jwt_token.JWTToken",
86-
"lifetime": 3600,
87-
"add_claims": [
88-
"email",
89-
"email_verified",
90-
"phone_number",
91-
"phone_number_verified",
92-
],
93-
"add_claim_by_scope": True,
94-
"aud": ["https://example.org/appl"],
87+
"kwargs":{
88+
"lifetime": 3600,
89+
"add_claims": [
90+
"email",
91+
"email_verified",
92+
"phone_number",
93+
"phone_number_verified",
94+
],
95+
"add_claim_by_scope": True,
96+
"aud": ["https://example.org/appl"]
97+
},
9598
},
9699
"refresh": {"lifetime": 86400},
97100
},

0 commit comments

Comments
 (0)