11import base64
22import hashlib
33import logging
4+ import warnings
45
56from cryptography .fernet import Fernet
67from 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
264286def 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 )
0 commit comments