Skip to content

Commit 55728b3

Browse files
committed
Changes to speed up unit testing.
1 parent 03820d8 commit 55728b3

File tree

183 files changed

+7486
-6258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+7486
-6258
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def run_tests(self):
7979
"Programming Language :: Python :: 3.10",
8080
"Topic :: Software Development :: Libraries :: Python Modules"],
8181
install_requires=[
82-
"cryptojwt==1.8.0",
82+
"cryptojwt>=1.8.0",
8383
"pyOpenSSL",
8484
"filelock>=3.0.12",
8585
'pyyaml>=5.1.2',

src/idpyoidc/actor/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88

99
class CIBAClient(ImpExp):
10-
parameter = {
11-
"context": {}
12-
}
10+
parameter = {"context": {}}
1311

1412
def __init__(
15-
self,
16-
keyjar: Optional[KeyJar] = None,
13+
self,
14+
keyjar: Optional[KeyJar] = None,
1715
):
1816
ImpExp.__init__(self)
1917
self.keyjar = keyjar
@@ -30,14 +28,15 @@ def create_authentication_request(self, scope, binding_message, login_hint):
3028
"scope": scope,
3129
"client_notification_token": client_notification_token,
3230
"binding_message": binding_message,
33-
"login_hint": login_hint
31+
"login_hint": login_hint,
3432
}
35-
request = _service.get_request_parameters(request_args=request_args,
36-
authn_method="private_key_jwt")
33+
request = _service.get_request_parameters(
34+
request_args=request_args, authn_method="private_key_jwt"
35+
)
3736

3837
self.context[client_notification_token] = {
3938
"authentication_request": request,
40-
"client_id": _service.client_get("service_context").issuer
39+
"client_id": _service.client_get("service_context").issuer,
4140
}
4241
return request
4342

@@ -48,18 +47,17 @@ def get_client_id_from_token(self, token):
4847
def do_client_notification(self, msg, http_info):
4948
_notification_endpoint = self.server.server_get("endpoint", "client_notification")
5049
_nreq = _notification_endpoint.parse_request(
51-
msg, http_info, get_client_id_from_token=self.get_client_id_from_token)
50+
msg, http_info, get_client_id_from_token=self.get_client_id_from_token
51+
)
5252
_ninfo = _notification_endpoint.process_request(_nreq)
5353

5454

5555
class CIBAServer(ImpExp):
56-
parameter = {
57-
"context": {}
58-
}
56+
parameter = {"context": {}}
5957

6058
def __init__(
61-
self,
62-
keyjar: Optional[KeyJar] = None,
59+
self,
60+
keyjar: Optional[KeyJar] = None,
6361
):
6462
ImpExp.__init__(self)
6563
self.keyjar = keyjar

src/idpyoidc/client/client_auth.py

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
from idpyoidc.message.oidc import AuthnToken
1717
from idpyoidc.time_util import utc_time_sans_frac
1818
from idpyoidc.util import rndstr
19-
# from idpyoidc.oidc.backchannel_authentication import ClientNotificationAuthn
2019

2120
from ..message import VREQUIRED
2221
from .util import sanitize
2322

23+
# from idpyoidc.oidc.backchannel_authentication import ClientNotificationAuthn
24+
25+
2426
LOGGER = logging.getLogger(__name__)
2527

26-
__author__ = 'roland hedberg'
28+
__author__ = "roland hedberg"
2729

2830

2931
class AuthnFailure(Exception):
@@ -48,10 +50,10 @@ def assertion_jwt(client_id, keys, audience, algorithm, lifetime=600):
4850
"""
4951
_now = utc_time_sans_frac()
5052

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())
5557
return _token.to_jwt(key=keys, algorithm=algorithm)
5658

5759

@@ -62,7 +64,7 @@ class ClientAuthnMethod:
6264
"""
6365

6466
def construct(self, request, service=None, http_args=None, **kwargs):
65-
""" Add authentication information to a request"""
67+
"""Add authentication information to a request"""
6668
raise NotImplementedError()
6769

6870
def modify_request(self, request, service, **kwargs):
@@ -125,18 +127,20 @@ def _get_authentication_token(self, request, service, **kwargs):
125127

126128
@staticmethod
127129
def _with_or_without_client_id(request, service):
128-
""" Add or delete client_id from request.
130+
"""Add or delete client_id from request.
129131
130132
If we're doing an access token request with an authorization code
131133
then we should add client_id to the request if it's not already there.
132134
:param request: A request
133135
:param service: A :py:class:`idpyoidc.client.service.Service` instance
134136
"""
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:
138142
try:
139-
request['client_id'] = service.client_get("service_context").client_id
143+
request["client_id"] = service.client_get("service_context").client_id
140144
except AttributeError:
141145
pass
142146
else:
@@ -272,16 +276,18 @@ def find_token(request, token_type, service, **kwargs):
272276
# I should pick the latest acquired token, this should be the right
273277
# order for that.
274278
_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")
278285

279286

280287
class BearerHeader(ClientAuthnMethod):
281288
"""The bearer header authentication method."""
282289

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):
285291
"""
286292
Constructing the Authorization header. The value of
287293
the Authorization header is "Bearer <access_token>".
@@ -293,13 +299,13 @@ def construct(self, request=None, service=None, http_args=None,
293299
:return:
294300
"""
295301

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)
298304
else:
299-
_acc_token = find_token(request, 'access_token', service, **kwargs)
305+
_acc_token = find_token(request, "access_token", service, **kwargs)
300306

301307
if not _acc_token:
302-
raise KeyError('No access or refresh token available')
308+
raise KeyError("No access or refresh token available")
303309

304310
# The authorization value starts with 'Bearer' when bearer tokens
305311
# are used
@@ -329,14 +335,14 @@ def modify_request(self, request, service, **kwargs):
329335
:param service: The service using this authentication method.
330336
:param kwargs: Extra keyword arguments
331337
"""
332-
_acc_token = ''
333-
for _token_type in ['access_token', 'refresh_token']:
338+
_acc_token = ""
339+
for _token_type in ["access_token", "refresh_token"]:
334340
_acc_token = find_token(request, _token_type, service, **kwargs)
335341
if _acc_token:
336342
break
337343

338344
if not _acc_token:
339-
raise KeyError('No access or refresh token available')
345+
raise KeyError("No access or refresh token available")
340346

341347
request["access_token"] = _acc_token
342348

@@ -370,7 +376,7 @@ def bearer_auth(request, authn):
370376
return request["access_token"]
371377
except KeyError:
372378
if not authn.startswith("Bearer "):
373-
raise ValueError('Not a bearer token')
379+
raise ValueError("Not a bearer token")
374380
return authn[7:]
375381

376382

@@ -407,8 +413,7 @@ def get_signing_key_from_keyjar(algorithm, service_context):
407413
:param service_context: A :py:class:`idpyoidc.client.service_context.ServiceContext` instance
408414
:return: A key
409415
"""
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)
412417

413418
@staticmethod
414419
def _get_key_by_kid(kid, algorithm, service_context):
@@ -439,8 +444,9 @@ def _get_signing_key(self, algorithm, context, kid=None):
439444
signing_key = [self._get_key_by_kid(kid, algorithm, context)]
440445
elif ktype in context.kid["sig"]:
441446
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+
]
444450
except KeyError:
445451
signing_key = self.get_signing_key_from_keyjar(algorithm, context)
446452
else:
@@ -456,7 +462,7 @@ def _get_audience_and_algorithm(self, context, **kwargs):
456462

457463
# audience for the signed JWT depends on which endpoint
458464
# 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"]:
460466
reg_resp = context.registration_response
461467
if reg_resp:
462468
algorithm = reg_resp["token_endpoint_auth_signing_alg"]
@@ -470,14 +476,15 @@ def _get_audience_and_algorithm(self, context, **kwargs):
470476
algorithm = "RS256" # default
471477
else:
472478
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+
):
475482
algorithm = alg
476483
break
477484

478-
audience = context.provider_info['token_endpoint']
485+
audience = context.provider_info["token_endpoint"]
479486
else:
480-
audience = context.provider_info['issuer']
487+
audience = context.provider_info["issuer"]
481488

482489
if not algorithm:
483490
algorithm = self.choose_algorithm(**kwargs)
@@ -488,16 +495,16 @@ def _construct_client_assertion(self, service, **kwargs):
488495
_entity = service.client_get("entity")
489496
audience, algorithm = self._get_audience_and_algorithm(_context, **kwargs)
490497

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"])
493500
else:
494501
signing_key = self._get_signing_key(algorithm, _context)
495502

496503
if not signing_key:
497504
raise UnsupportedAlgorithm(algorithm)
498505

499506
try:
500-
_args = {'lifetime': kwargs['lifetime']}
507+
_args = {"lifetime": kwargs["lifetime"]}
501508
except KeyError:
502509
_args = {}
503510

@@ -513,15 +520,14 @@ def modify_request(self, request, service, **kwargs):
513520
:param service: The service using this authentication method.
514521
:param kwargs: Extra keyword arguments
515522
"""
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"]
521527
else:
522528
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:
525531
request["client_assertion_type"] = JWT_BEARER
526532
else:
527533
request["client_assertion"] = self._construct_client_assertion(service, **kwargs)
@@ -591,7 +597,7 @@ def get_signing_key_from_keyjar(self, algorithm, service_context=None):
591597
"bearer_body": BearerBody,
592598
"client_secret_jwt": ClientSecretJWT,
593599
"private_key_jwt": PrivateKeyJWT,
594-
# "client_notification_authn": ClientNotificationAuthn
600+
# "client_notification_authn": ClientNotificationAuthn
595601
}
596602

597603
TYPE_METHOD = [(JWT_BEARER, JWSAuthnMethod)]

0 commit comments

Comments
 (0)