Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 184fad7

Browse files
committed
There might be instances where a based_on token is not connected to the grant under which the new token is minted. This can happen during token exchange.
1 parent 11914df commit 184fad7

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/oidcop/oauth2/introspection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def _introspect(self, token, client_id, grant):
3434

3535
scope = token.scope
3636
if not scope:
37-
scope = grant.scope
37+
if token.based_on:
38+
scope = grant.find_scope(token.based_on)
39+
else:
40+
scope = grant.scope
3841
aud = token.resources
3942
if not aud:
4043
aud = grant.resources

src/oidcop/session/grant.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,17 @@ def get(self) -> object:
176176
)
177177

178178
def find_scope(self, based_on):
179-
if based_on.scope:
180-
return based_on.scope
179+
if isinstance(based_on, str):
180+
based_on = self.get_token(based_on)
181181

182-
if based_on.based_on:
183-
# Don't expect there to be that many tokens based on one grant so a linear search
184-
# should be OK.
185-
for token in self.issued_token:
186-
if token.value == based_on.based_on:
187-
return self.find_scope(token)
188-
return []
182+
if based_on:
183+
if based_on.scope:
184+
return based_on.scope
185+
186+
if based_on.based_on:
187+
return self.find_scope(based_on.based_on)
188+
189+
return self.scope
189190

190191
def payload_arguments(
191192
self,
@@ -199,7 +200,7 @@ def payload_arguments(
199200
200201
:return: dictionary containing information to place in a token value
201202
"""
202-
if not scope:
203+
if scope is None:
203204
scope = self.scope
204205

205206
payload = {"scope": scope, "aud": self.resources, "jti": uuid1().hex}
@@ -272,9 +273,11 @@ def mint_token(
272273
handler_args = {}
273274

274275
if token_class:
275-
if not scope:
276+
if scope is None:
276277
if based_on:
277278
scope = self.find_scope(based_on)
279+
else:
280+
scope = self.scope
278281

279282
item = token_class(
280283
type=token_type,

tests/test_36_oauth2_token_exchange.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,15 @@ def _mint_code(self, grant, session_id):
197197
token_handler=self.session_manager.token_handler["code"],
198198
)
199199

200-
def _mint_access_token(self, grant, session_id, token_ref=None, resources=None):
200+
def _mint_access_token(self, grant, session_id, token_ref=None, resources=None, scope=None):
201201
return grant.mint_token(
202202
session_id=session_id,
203203
endpoint_context=self.endpoint.server_get("endpoint_context"),
204204
token_type="access_token",
205205
token_handler=self.session_manager.token_handler["access_token"],
206206
based_on=token_ref,
207207
resources=resources,
208+
scope=scope
208209
)
209210

210211
def exchange_grant(self, session_id, users, targets, scope):
@@ -257,15 +258,19 @@ def test_do_response(self):
257258
assert exch_grants
258259
exch_grant = exch_grants[0]
259260

260-
session_info = self.session_manager.get_session_info_by_token(ter["subject_token"])
261+
session_info = self.session_manager.get_session_info_by_token(ter["subject_token"],
262+
grant=True)
261263
_token = self.session_manager.find_token(session_info["session_id"], ter["subject_token"])
262264

263265
session_id = self.session_manager.encrypted_session_id(
264266
session_info["user_id"], session_info["client_id"], exch_grant.id
265267
)
266268

269+
_scope = session_info["grant"].find_scope(ter["subject_token"])
270+
267271
_token = self._mint_access_token(
268272
exch_grant, session_id, token_ref=_token, resources=["https://backend.example.com"],
273+
scope=_scope
269274
)
270275

271276
print(_token.value)
@@ -274,8 +279,7 @@ def test_do_response(self):
274279
"token": _token.value,
275280
"client_id": "client_1",
276281
"client_secret": self.introspection_endpoint.server_get("endpoint_context").cdb[
277-
"client_1"
278-
]["client_secret"],
282+
"client_1"]["client_secret"],
279283
}
280284
)
281285
_resp = self.introspection_endpoint.process_request(_req)

0 commit comments

Comments
 (0)