Skip to content

Commit 2dfb25d

Browse files
authored
Merge pull request #97 from lionick/add_claims_oauth_resource
Add claims for OAuth 2.0 Protected Resource
2 parents dddbc05 + 9020549 commit 2dfb25d

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Optional
2+
3+
from idpyoidc.client import claims
4+
from idpyoidc.message.oauth2 import OAuthProtectedResourceRequest
5+
from idpyoidc.client.claims.transform import array_or_singleton
6+
7+
class Claims(claims.Claims):
8+
_supports = {
9+
"resource": None,
10+
"grant_types_supported": ["authorization_code", "implicit", "refresh_token"],
11+
"scopes_supported": [],
12+
"authorization_servers": [],
13+
"bearer_methods_supported": [],
14+
"resource_documentation": None,
15+
"resource_signing_alg_values_supported": [],
16+
"resource_encryption_alg_values_supported": [],
17+
"resource_encryption_enc_values_supported": [],
18+
"client_registration_types": [],
19+
"organization_name": None,
20+
"resource_policy_uri": None,
21+
"resource_tos_uri": None
22+
}
23+
24+
callback_path = {}
25+
26+
callback_uris = ["redirect_uris"]
27+
28+
def __init__(self, prefer: Optional[dict] = None, callback_path: Optional[dict] = None):
29+
claims.Claims.__init__(self, prefer=prefer, callback_path=callback_path)
30+
31+
def create_registration_request(self):
32+
_request = {}
33+
for key, spec in OAuthProtectedResourceRequest.c_param.items():
34+
_pref_key = key
35+
if _pref_key in self.prefer:
36+
value = self.prefer[_pref_key]
37+
elif _pref_key in self.supports():
38+
value = self.supports()[_pref_key]
39+
else:
40+
continue
41+
42+
if not value:
43+
continue
44+
45+
_request[key] = array_or_singleton(spec, value)
46+
return _request

src/idpyoidc/client/service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,16 @@ def construct(self, request_args: Optional[dict] = None, **kwargs):
262262
_args = self.gather_request_args(**request_args)
263263

264264
# logger.debug("kwargs: %s" % sanitize(kwargs))
265+
266+
# we must check if claims module is idpyoidc.client.claims.oauth2recource as
267+
# in that case we don't want to set_defaults like application_type etc.
268+
obj = self.upstream_get("context").claims
265269
# initiate the request as in an instance of the self.msg_type
266270
# message type
267-
request = self.msg_type(**_args)
271+
if(obj.__class__.__module__ == "idpyoidc.client.claims.oauth2resource"):
272+
request = self.msg_type(**_args, set_defaults=False)
273+
else:
274+
request = self.msg_type(**_args)
268275

269276
_behaviour_args = kwargs.get("behaviour_args")
270277
if _behaviour_args:

src/idpyoidc/client/service_context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from idpyoidc.claims import claims_dump
1919
from idpyoidc.claims import claims_load
2020
from idpyoidc.client.claims.oauth2 import Claims as OAUTH2_Specs
21+
from idpyoidc.client.claims.oauth2resource import Claims as OAUTH2RESOURCE_Specs
2122
from idpyoidc.client.claims.oidc import Claims as OIDC_Specs
2223
from idpyoidc.client.configure import Configuration
2324
from idpyoidc.util import rndstr
@@ -133,6 +134,8 @@ def __init__(
133134
self.claims = OIDC_Specs()
134135
elif client_type == "oauth2":
135136
self.claims = OAUTH2_Specs()
137+
elif client_type == "oauth2resource":
138+
self.claims = OAUTH2RESOURCE_Specs()
136139
else:
137140
raise ValueError(f"Unknown client type: {client_type}")
138141

src/idpyoidc/message/oauth2/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,22 @@ class TokenRevocationErrorResponse(ResponseMessage):
636636
c_allowed_values = ResponseMessage.c_allowed_values.copy()
637637
c_allowed_values.update({"error": ["unsupported_token_type"]})
638638

639+
class OAuthProtectedResourceRequest(Message):
640+
c_param = {
641+
"resource": SINGLE_REQUIRED_STRING,
642+
"authorization_servers": OPTIONAL_LIST_OF_STRINGS,
643+
"jwks_uri": SINGLE_OPTIONAL_STRING,
644+
"resource_documentation": SINGLE_OPTIONAL_STRING,
645+
"scopes_supported": OPTIONAL_LIST_OF_STRINGS,
646+
"bearer_methods_supported": OPTIONAL_LIST_OF_STRINGS,
647+
"resource_signing_alg_values_supported": OPTIONAL_LIST_OF_STRINGS,
648+
"resource_encryption_alg_values_supported": OPTIONAL_LIST_OF_STRINGS,
649+
"resource_encryption_enc_values_supported": OPTIONAL_LIST_OF_STRINGS,
650+
"client_registration_types": OPTIONAL_LIST_OF_STRINGS,
651+
"organization_name": SINGLE_OPTIONAL_STRING,
652+
"resource_policy_uri": SINGLE_OPTIONAL_STRING,
653+
"resource_tos_uri": SINGLE_OPTIONAL_STRING
654+
}
639655

640656
def factory(msgtype, **kwargs):
641657
"""

0 commit comments

Comments
 (0)