16
16
from idpyoidc .message .oidc import AuthnToken
17
17
from idpyoidc .time_util import utc_time_sans_frac
18
18
from idpyoidc .util import rndstr
19
- # from idpyoidc.oidc.backchannel_authentication import ClientNotificationAuthn
20
19
21
20
from ..message import VREQUIRED
22
21
from .util import sanitize
23
22
23
+ # from idpyoidc.oidc.backchannel_authentication import ClientNotificationAuthn
24
+
25
+
24
26
LOGGER = logging .getLogger (__name__ )
25
27
26
- __author__ = ' roland hedberg'
28
+ __author__ = " roland hedberg"
27
29
28
30
29
31
class AuthnFailure (Exception ):
@@ -48,10 +50,10 @@ def assertion_jwt(client_id, keys, audience, algorithm, lifetime=600):
48
50
"""
49
51
_now = utc_time_sans_frac ()
50
52
51
- _token = AuthnToken (iss = client_id , sub = client_id ,
52
- aud = audience , jti = rndstr (32 ),
53
- exp = _now + lifetime , iat = _now )
54
- LOGGER .debug (' AuthnToken: %s' , _token .to_dict ())
53
+ _token = AuthnToken (
54
+ iss = client_id , sub = client_id , aud = audience , jti = rndstr (32 ), exp = _now + lifetime , iat = _now
55
+ )
56
+ LOGGER .debug (" AuthnToken: %s" , _token .to_dict ())
55
57
return _token .to_jwt (key = keys , algorithm = algorithm )
56
58
57
59
@@ -62,7 +64,7 @@ class ClientAuthnMethod:
62
64
"""
63
65
64
66
def construct (self , request , service = None , http_args = None , ** kwargs ):
65
- """ Add authentication information to a request"""
67
+ """Add authentication information to a request"""
66
68
raise NotImplementedError ()
67
69
68
70
def modify_request (self , request , service , ** kwargs ):
@@ -125,18 +127,20 @@ def _get_authentication_token(self, request, service, **kwargs):
125
127
126
128
@staticmethod
127
129
def _with_or_without_client_id (request , service ):
128
- """ Add or delete client_id from request.
130
+ """Add or delete client_id from request.
129
131
130
132
If we're doing an access token request with an authorization code
131
133
then we should add client_id to the request if it's not already there.
132
134
:param request: A request
133
135
:param service: A :py:class:`idpyoidc.client.service.Service` instance
134
136
"""
135
- if isinstance (request , AccessTokenRequest ) and request [
136
- 'grant_type' ] == 'authorization_code' :
137
- if 'client_id' not in request :
137
+ if (
138
+ isinstance (request , AccessTokenRequest )
139
+ and request ["grant_type" ] == "authorization_code"
140
+ ):
141
+ if "client_id" not in request :
138
142
try :
139
- request [' client_id' ] = service .client_get ("service_context" ).client_id
143
+ request [" client_id" ] = service .client_get ("service_context" ).client_id
140
144
except AttributeError :
141
145
pass
142
146
else :
@@ -272,16 +276,18 @@ def find_token(request, token_type, service, **kwargs):
272
276
# I should pick the latest acquired token, this should be the right
273
277
# order for that.
274
278
_arg = service .client_get ("service_context" ).state .multiple_extend_request_args (
275
- {}, kwargs ['key' ], ['access_token' ],
276
- ['auth_response' , 'token_response' , 'refresh_token_response' ])
277
- return _arg .get ('access_token' )
279
+ {},
280
+ kwargs ["key" ],
281
+ ["access_token" ],
282
+ ["auth_response" , "token_response" , "refresh_token_response" ],
283
+ )
284
+ return _arg .get ("access_token" )
278
285
279
286
280
287
class BearerHeader (ClientAuthnMethod ):
281
288
"""The bearer header authentication method."""
282
289
283
- def construct (self , request = None , service = None , http_args = None ,
284
- ** kwargs ):
290
+ def construct (self , request = None , service = None , http_args = None , ** kwargs ):
285
291
"""
286
292
Constructing the Authorization header. The value of
287
293
the Authorization header is "Bearer <access_token>".
@@ -293,13 +299,13 @@ def construct(self, request=None, service=None, http_args=None,
293
299
:return:
294
300
"""
295
301
296
- if service .service_name == ' refresh_token' :
297
- _acc_token = find_token (request , ' refresh_token' , service , ** kwargs )
302
+ if service .service_name == " refresh_token" :
303
+ _acc_token = find_token (request , " refresh_token" , service , ** kwargs )
298
304
else :
299
- _acc_token = find_token (request , ' access_token' , service , ** kwargs )
305
+ _acc_token = find_token (request , " access_token" , service , ** kwargs )
300
306
301
307
if not _acc_token :
302
- raise KeyError (' No access or refresh token available' )
308
+ raise KeyError (" No access or refresh token available" )
303
309
304
310
# The authorization value starts with 'Bearer' when bearer tokens
305
311
# are used
@@ -329,14 +335,14 @@ def modify_request(self, request, service, **kwargs):
329
335
:param service: The service using this authentication method.
330
336
:param kwargs: Extra keyword arguments
331
337
"""
332
- _acc_token = ''
333
- for _token_type in [' access_token' , ' refresh_token' ]:
338
+ _acc_token = ""
339
+ for _token_type in [" access_token" , " refresh_token" ]:
334
340
_acc_token = find_token (request , _token_type , service , ** kwargs )
335
341
if _acc_token :
336
342
break
337
343
338
344
if not _acc_token :
339
- raise KeyError (' No access or refresh token available' )
345
+ raise KeyError (" No access or refresh token available" )
340
346
341
347
request ["access_token" ] = _acc_token
342
348
@@ -370,7 +376,7 @@ def bearer_auth(request, authn):
370
376
return request ["access_token" ]
371
377
except KeyError :
372
378
if not authn .startswith ("Bearer " ):
373
- raise ValueError (' Not a bearer token' )
379
+ raise ValueError (" Not a bearer token" )
374
380
return authn [7 :]
375
381
376
382
@@ -407,8 +413,7 @@ def get_signing_key_from_keyjar(algorithm, service_context):
407
413
:param service_context: A :py:class:`idpyoidc.client.service_context.ServiceContext` instance
408
414
:return: A key
409
415
"""
410
- return service_context .keyjar .get_signing_key (
411
- alg2keytype (algorithm ), alg = algorithm )
416
+ return service_context .keyjar .get_signing_key (alg2keytype (algorithm ), alg = algorithm )
412
417
413
418
@staticmethod
414
419
def _get_key_by_kid (kid , algorithm , service_context ):
@@ -439,8 +444,9 @@ def _get_signing_key(self, algorithm, context, kid=None):
439
444
signing_key = [self ._get_key_by_kid (kid , algorithm , context )]
440
445
elif ktype in context .kid ["sig" ]:
441
446
try :
442
- signing_key = [self ._get_key_by_kid (
443
- context .kid ["sig" ][ktype ], algorithm , context )]
447
+ signing_key = [
448
+ self ._get_key_by_kid (context .kid ["sig" ][ktype ], algorithm , context )
449
+ ]
444
450
except KeyError :
445
451
signing_key = self .get_signing_key_from_keyjar (algorithm , context )
446
452
else :
@@ -456,7 +462,7 @@ def _get_audience_and_algorithm(self, context, **kwargs):
456
462
457
463
# audience for the signed JWT depends on which endpoint
458
464
# we're talking to.
459
- if ' authn_endpoint' in kwargs and kwargs [' authn_endpoint' ] in [' token_endpoint' ]:
465
+ if " authn_endpoint" in kwargs and kwargs [" authn_endpoint" ] in [" token_endpoint" ]:
460
466
reg_resp = context .registration_response
461
467
if reg_resp :
462
468
algorithm = reg_resp ["token_endpoint_auth_signing_alg" ]
@@ -470,14 +476,15 @@ def _get_audience_and_algorithm(self, context, **kwargs):
470
476
algorithm = "RS256" # default
471
477
else :
472
478
for alg in algs : # pick the first one I support and have keys for
473
- if alg in SIGNER_ALGS and self .get_signing_key_from_keyjar (alg ,
474
- context ):
479
+ if alg in SIGNER_ALGS and self .get_signing_key_from_keyjar (
480
+ alg , context
481
+ ):
475
482
algorithm = alg
476
483
break
477
484
478
- audience = context .provider_info [' token_endpoint' ]
485
+ audience = context .provider_info [" token_endpoint" ]
479
486
else :
480
- audience = context .provider_info [' issuer' ]
487
+ audience = context .provider_info [" issuer" ]
481
488
482
489
if not algorithm :
483
490
algorithm = self .choose_algorithm (** kwargs )
@@ -488,16 +495,16 @@ def _construct_client_assertion(self, service, **kwargs):
488
495
_entity = service .client_get ("entity" )
489
496
audience , algorithm = self ._get_audience_and_algorithm (_context , ** kwargs )
490
497
491
- if ' kid' in kwargs :
492
- signing_key = self ._get_signing_key (algorithm , _context , kid = kwargs [' kid' ])
498
+ if " kid" in kwargs :
499
+ signing_key = self ._get_signing_key (algorithm , _context , kid = kwargs [" kid" ])
493
500
else :
494
501
signing_key = self ._get_signing_key (algorithm , _context )
495
502
496
503
if not signing_key :
497
504
raise UnsupportedAlgorithm (algorithm )
498
505
499
506
try :
500
- _args = {' lifetime' : kwargs [' lifetime' ]}
507
+ _args = {" lifetime" : kwargs [" lifetime" ]}
501
508
except KeyError :
502
509
_args = {}
503
510
@@ -513,15 +520,14 @@ def modify_request(self, request, service, **kwargs):
513
520
:param service: The service using this authentication method.
514
521
:param kwargs: Extra keyword arguments
515
522
"""
516
- if 'client_assertion' in kwargs :
517
- request ["client_assertion" ] = kwargs ['client_assertion' ]
518
- if 'client_assertion_type' in kwargs :
519
- request [
520
- 'client_assertion_type' ] = kwargs ['client_assertion_type' ]
523
+ if "client_assertion" in kwargs :
524
+ request ["client_assertion" ] = kwargs ["client_assertion" ]
525
+ if "client_assertion_type" in kwargs :
526
+ request ["client_assertion_type" ] = kwargs ["client_assertion_type" ]
521
527
else :
522
528
request ["client_assertion_type" ] = JWT_BEARER
523
- elif ' client_assertion' in request :
524
- if ' client_assertion_type' not in request :
529
+ elif " client_assertion" in request :
530
+ if " client_assertion_type" not in request :
525
531
request ["client_assertion_type" ] = JWT_BEARER
526
532
else :
527
533
request ["client_assertion" ] = self ._construct_client_assertion (service , ** kwargs )
@@ -591,7 +597,7 @@ def get_signing_key_from_keyjar(self, algorithm, service_context=None):
591
597
"bearer_body" : BearerBody ,
592
598
"client_secret_jwt" : ClientSecretJWT ,
593
599
"private_key_jwt" : PrivateKeyJWT ,
594
- # "client_notification_authn": ClientNotificationAuthn
600
+ # "client_notification_authn": ClientNotificationAuthn
595
601
}
596
602
597
603
TYPE_METHOD = [(JWT_BEARER , JWSAuthnMethod )]
0 commit comments