Skip to content

Commit 5c1ead1

Browse files
committed
Added more logging.
Depending on web framework the dpop header may come under different names. Improved the dump method to allow dictionary representation beside a Message instance.
1 parent 75187c2 commit 5c1ead1

12 files changed

Lines changed: 111 additions & 40 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"keys": [{"kty": "oct", "use": "enc", "kid": "enc", "k": "GEmhZ9UKLSq60zECQRyAtmMLG5smRpCl"}, {"kty": "oct", "use": "sig", "kid": "sig", "k": "Px8EGB-oWk-DfMlYWXBHTjED372mvtBt"}]}
1+
{"keys": [{"kty": "oct", "use": "enc", "kid": "enc", "k": "HqZu6WO7HyvyCfAwfCdzwSLUuEeVPiIv"}, {"kty": "oct", "use": "sig", "kid": "sig", "k": "Y3sPFaO2qJuG-Q2O-UzpRIYk-I1KLPZo"}]}

src/idpyoidc/claims.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from typing import Callable
23
from typing import Optional
34

@@ -10,6 +11,7 @@
1011
from idpyoidc.util import add_path
1112
from idpyoidc.util import qualified_name
1213

14+
logger = logging.getLogger(__name__)
1315

1416
def claims_dump(info, exclude_attributes):
1517
return {qualified_name(info.__class__): info.dump(exclude_attributes=exclude_attributes)}
@@ -138,6 +140,7 @@ def handle_keys(self,
138140
configuration: dict,
139141
keyjar: Optional[KeyJar] = None,
140142
entity_id: Optional[str] = ""):
143+
logger.debug(f"configuration: {configuration}")
141144
_jwks = _jwks_uri = None
142145
_id = self.get_id(configuration)
143146
keyjar, uri_path = self._keyjar(keyjar, configuration, entity_id=_id)

src/idpyoidc/client/oauth2/add_on/par.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ def push_authorization(request_args, service, **kwargs):
7676
_req[param] = request_args.get(param)
7777
request_args = _req
7878
else:
79-
raise ConnectionError(
80-
f"Could not connect to "
81-
f'{_context.provider_info["pushed_authorization_request_endpoint"]}'
82-
)
79+
raise ConnectionError(f"Could not connect to {_par_endpoint}")
8380

8481
return request_args
8582

src/idpyoidc/impexp.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import base64
2+
import inspect
3+
import logging
24
from typing import Any
35
from typing import List
46
from typing import Optional
@@ -10,6 +12,7 @@
1012
from idpyoidc.message import Message
1113
from idpyoidc.storage import DictType
1214

15+
logger = logging.getLogger(__name__)
1316

1417
def fully_qualified_name(cls):
1518
return cls.__module__ + "." + cls.__class__.__name__
@@ -41,6 +44,7 @@ def __init__(self):
4144
pass
4245

4346
def dump_attr(self, cls, item, exclude_attributes: Optional[List[str]] = None) -> dict:
47+
logger.debug(f"dump_attr:: cls: {cls}, item: {item}")
4448
if cls in [None, 0, "", bool]:
4549
val = item
4650
elif cls == b"":
@@ -74,9 +78,29 @@ def dump_attr(self, cls, item, exclude_attributes: Optional[List[str]] = None) -
7478
val = qualified_name(item)
7579
elif isinstance(cls, list):
7680
val = [self.dump_attr(cls[0], v, exclude_attributes) for v in item]
81+
elif inspect.isclass(cls):
82+
logger.debug(f"class instance: {cls}")
83+
_dump = getattr(cls, "dump", None)
84+
if _dump:
85+
val = _dump(item, exclude_attributes=exclude_attributes)
86+
else:
87+
if isinstance(item, cls):
88+
val = {qualified_name(cls): item.to_dict()}
89+
elif isinstance(item, dict):
90+
val = {qualified_name(cls): item}
91+
else:
92+
logger.error(f"Can't dump {item} as {cls}")
7793
else:
78-
val = item.dump(exclude_attributes=exclude_attributes)
94+
_dump = getattr(item, "dump", None)
95+
if _dump:
96+
val = _dump(exclude_attributes=exclude_attributes)
97+
elif isinstance(item, dict):
98+
val = item
99+
else:
100+
logger.error(f"Do not know how to dump: {item}")
101+
raise AttributeError()
79102

103+
logger.debug(f"-> {val}")
80104
return val
81105

82106
def dump(self, exclude_attributes: Optional[List[str]] = None) -> dict:
@@ -154,7 +178,11 @@ def load_attr(
154178
val = [_cls(**_args).load(v, **_kwargs) for v in item]
155179
elif issubclass(cls, Message):
156180
_cls_name = list(item.keys())[0]
157-
_cls = importer(_cls_name)
181+
try:
182+
_cls = importer(_cls_name)
183+
except Exception as err:
184+
logger.error(f"Could not import {item}: {err}")
185+
raise
158186
val = _cls().from_dict(item[_cls_name])
159187
else:
160188
if issubclass(cls, ImpExp) and init_args:

src/idpyoidc/server/claims/oidc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
from idpyoidc import metadata
4+
from idpyoidc.message import Message
45
from idpyoidc.message.oidc import ProviderConfigurationResponse
56
from idpyoidc.message.oidc import RegistrationRequest
67
from idpyoidc.message.oidc import RegistrationResponse
@@ -91,7 +92,7 @@ def verify_rules(self, supports):
9192
self.set_preference("id_token_encryption_alg_values_supported", [])
9293
self.set_preference("id_token_encryption_enc_values_supported", [])
9394

94-
def provider_info(self, supports):
95+
def provider_info(self, supports, schema: Optional[Message] = None):
9596
_info = {}
9697
for key in ProviderConfigurationResponse.c_param.keys():
9798
_val = self.get_preference(key, supports.get(key, None))

src/idpyoidc/server/client_authn.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ def verify_client(
488488
if not allowed_methods:
489489
allowed_methods = list(methods.keys()) # If not specific for this endpoint then all
490490

491+
logger.info(f"Allowed client authentication methods: {allowed_methods}")
492+
491493
_method = None
492494
_cdb = _cinfo = None
493495
_tested = []
@@ -504,8 +506,8 @@ def verify_client(
504506
endpoint=endpoint,
505507
get_client_id_from_token=get_client_id_from_token,
506508
)
507-
except (BearerTokenAuthenticationError, ClientAuthenticationError):
508-
raise
509+
# except (BearerTokenAuthenticationError, ClientAuthenticationError):
510+
# raise
509511
except Exception as err:
510512
logger.info("Verifying auth using {} failed: {}".format(_method.tag, err))
511513
continue
@@ -534,7 +536,7 @@ def verify_client(
534536
_auto_reg = getattr(endpoint, "automatic_registration", None)
535537
if _auto_reg:
536538
_cinfo = {"client_id": client_id}
537-
_auto_reg.set(client_id, _cinfo)
539+
_cdb[client_id] = _cinfo
538540
else:
539541
raise UnknownClient("Unknown Client ID")
540542

src/idpyoidc/server/endpoint.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ def parse_request(
196196
:return:
197197
"""
198198
LOGGER.debug("- {} -".format(self.endpoint_name))
199-
LOGGER.info("Request: %s" % sanitize(request))
199+
LOGGER.info(f"Request: {sanitize(request)}")
200+
if http_info:
201+
LOGGER.info(f"HTTP info: {http_info}")
200202

201203
_context = self.upstream_get("context")
202204
_keyjar = self.upstream_get("attribute", "keyjar")

src/idpyoidc/server/endpoint_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ def __init__(
241241
conf = conf.conf
242242
_supports = self.supports()
243243
self.keyjar = self.claims.load_conf(conf, supports=_supports, keyjar=keyjar)
244-
metadata_schema = conf.conf.get("metadata_schema", None)
244+
if isinstance(conf, dict):
245+
metadata_schema = conf.get("metadata_schema", None)
246+
else:
247+
metadata_schema = conf.conf.get("metadata_schema", None)
245248
if metadata_schema:
246249
metadata_schema = importer(metadata_schema)
247250
self.provider_info = self.get_provider_info(_supports, schema=metadata_schema)

src/idpyoidc/server/oauth2/add_on/dpop.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ def token_post_parse_request(request, client_id, context, **kwargs):
107107
if not _http_info:
108108
return request
109109

110-
_dpop = DPoPProof().verify_header(_http_info["headers"]["dpop"])
110+
_headers = _http_info['headers']
111+
logger.debug(f"http headers: {_headers}")
112+
113+
_dpop_header = _headers.get("dpop", _headers.get("http_dpop", None))
114+
if not _dpop_header:
115+
raise ValueError("Missing DPoP header")
116+
117+
_dpop = DPoPProof().verify_header(_dpop_header)
111118

112119
# The signature of the JWS is verified, now for checking the
113120
# content

src/idpyoidc/server/session/grant.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ def mint_token(
377377
)
378378

379379
logger.debug(f"token_payload: {token_payload}")
380-
381380
item.value = token_handler(
382381
session_id=session_id, usage_rules=usage_rules, **token_payload
383382
)

0 commit comments

Comments
 (0)