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

Commit 10ed5ac

Browse files
committed
Refactor token exchange configuration
1 parent b40c5bc commit 10ed5ac

File tree

5 files changed

+464
-267
lines changed

5 files changed

+464
-267
lines changed

docs/source/contents/conf.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,120 @@ the following::
663663
}
664664
}
665665

666+
==============
667+
Token exchange
668+
==============
669+
There are two possible ways to configure Token Exchange in OIDC-OP, globally and per-client.
670+
For the first case the configuration is passed in the Token Exchange handler throught the
671+
`urn:ietf:params:oauth:grant-type:token-exchange` dictionary in token's `grant_types_supported`.
672+
673+
If present, the token exchange configuration must contain a `policy` object that describes a default
674+
policy `callable` and its `kwargs` through the `""` key. Different callables can be optionally
675+
defined for each token type supported.
676+
677+
```
678+
"grant_types_supported":{
679+
"urn:ietf:params:oauth:grant-type:token-exchange": {
680+
"class": "oidcop.oidc.token.TokenExchangeHelper",
681+
"kwargs": {
682+
"subject_token_types_supported": [
683+
"urn:ietf:params:oauth:token-type:access_token",
684+
"urn:ietf:params:oauth:token-type:refresh_token",
685+
"urn:ietf:params:oauth:token-type:id_token"
686+
],
687+
"requested_token_types_supported": [
688+
"urn:ietf:params:oauth:token-type:access_token",
689+
"urn:ietf:params:oauth:token-type:refresh_token",
690+
"urn:ietf:params:oauth:token-type:id_token"
691+
],
692+
"policy": {
693+
"urn:ietf:params:oauth:token-type:access_token": {
694+
"callable": "/path/to/callable",
695+
"kwargs": {
696+
"audience": ["https://example.com"],
697+
"scopes": ["openid"]
698+
}
699+
},
700+
"urn:ietf:params:oauth:token-type:refresh_token": {
701+
"callable": "/path/to/callable",
702+
"kwargs": {
703+
"resource": ["https://example.com"],
704+
"scopes": ["openid"]
705+
}
706+
},
707+
"": {
708+
"callable": "/path/to/callable",
709+
"kwargs": {
710+
"scopes": ["openid"]
711+
}
712+
}
713+
}
714+
}
715+
}
716+
}
717+
```
718+
719+
For the per-client configuration a similar configuration scheme should be present in the client's
720+
metadata under the `token_exchange` key.
721+
722+
For example:
723+
724+
```
725+
"token_exchange":{
726+
"urn:ietf:params:oauth:grant-type:token-exchange": {
727+
"class": "oidcop.oidc.token.TokenExchangeHelper",
728+
"kwargs": {
729+
"subject_token_types_supported": [
730+
"urn:ietf:params:oauth:token-type:access_token",
731+
"urn:ietf:params:oauth:token-type:refresh_token",
732+
"urn:ietf:params:oauth:token-type:id_token"
733+
],
734+
"requested_token_types_supported": [
735+
"urn:ietf:params:oauth:token-type:access_token",
736+
"urn:ietf:params:oauth:token-type:refresh_token",
737+
"urn:ietf:params:oauth:token-type:id_token"
738+
],
739+
"policy": {
740+
"urn:ietf:params:oauth:token-type:access_token": {
741+
"callable": "/path/to/callable",
742+
"kwargs": {
743+
"audience": ["https://example.com"],
744+
"scopes": ["openid"]
745+
}
746+
},
747+
"urn:ietf:params:oauth:token-type:refresh_token": {
748+
"callable": "/path/to/callable",
749+
"kwargs": {
750+
"resource": ["https://example.com"],
751+
"scopes": ["openid"]
752+
}
753+
},
754+
"": {
755+
"callable": "/path/to/callable",
756+
"kwargs": {
757+
"scopes": ["openid"]
758+
}
759+
}
760+
}
761+
}
762+
}
763+
}
764+
```
765+
766+
The policy callable accepts a specific argument list and must return the altered token exchange
767+
request or raise an exception.
768+
769+
For example:
770+
771+
```
772+
def custom_token_exchange_policy(request, context, subject_token, **kwargs):
773+
if some_condition in request:
774+
return TokenErrorResponse(
775+
error="invalid_request", error_description="Some error occured"
776+
)
777+
778+
return request
779+
```
666780

667781
=======
668782
Clients

docs/source/contents/usage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ Here an example about how to exchange an access token for a new access token.
133133

134134
import requests
135135

136-
CLIENT_ID = "DBP60x3KUQfCYWZlqFaS_Q"
137-
CLIENT_SECRET="8526270403788522b2444e87ea90c53bcafb984119cec92eeccc12f1"
138-
SUBJECT_TOKEN="Z0FBQUFkF3czZRU...BfdTJkQXlCSm55cVpxQ1A0Y0RkWEtQTT0="
136+
CLIENT_ID=""
137+
CLIENT_SECRET=""
138+
SUBJECT_TOKEN=""
139139
REQUESTED_TOKEN_TYPE="urn:ietf:params:oauth:token-type:access_token"
140140

141141
data = {
@@ -147,7 +147,7 @@ Here an example about how to exchange an access token for a new access token.
147147
}
148148
headers = {'Content-Type': "application/x-www-form-urlencoded" }
149149
response = requests.post(
150-
'https://snf-19725.ok-kno.grnetcloud.net/OIDC/token', verify=False, data=data, headers=headers
150+
'https://example.com/OIDC/token', verify=False, data=data, headers=headers
151151
)
152152

153153
oidc-op will return a json response like this::

src/oidcop/configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def __init__(
564564
"client_secret_basic",
565565
"client_secret_jwt",
566566
"private_key_jwt",
567-
]
567+
],
568568
},
569569
},
570570
"userinfo": {

0 commit comments

Comments
 (0)