This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtoken.py
More file actions
executable file
·370 lines (301 loc) · 13.3 KB
/
token.py
File metadata and controls
executable file
·370 lines (301 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import logging
from typing import Optional
from typing import Union
from cryptojwt.jwe.exception import JWEException
from cryptojwt.jws.exception import NoSuitableSigningKeys
from cryptojwt.jwt import utc_time_sans_frac
from oidcmsg import oidc
from oidcmsg.message import Message
from oidcmsg.oidc import RefreshAccessTokenRequest
from oidcmsg.oidc import TokenErrorResponse
from oidcop import oauth2
from oidcop import sanitize
from oidcop.oauth2.token import TokenEndpointHelper
from oidcop.session.grant import AuthorizationCode
from oidcop.session.grant import RefreshToken
from oidcop.session.token import MintingNotAllowed
from oidcop.token.exception import UnknownToken
logger = logging.getLogger(__name__)
class AccessTokenHelper(TokenEndpointHelper):
def process_request(self, req: Union[Message, dict], **kwargs):
"""
:param req:
:param kwargs:
:return:
"""
_context = self.endpoint.server_get("endpoint_context")
_mngr = _context.session_manager
logger.debug("OIDC Access Token")
if req["grant_type"] != "authorization_code":
return self.error_cls(error="invalid_request", error_description="Unknown grant_type")
try:
_access_code = req["code"].replace(" ", "+")
except KeyError: # Missing code parameter - absolutely fatal
return self.error_cls(error="invalid_request", error_description="Missing code")
_session_info = _mngr.get_session_info_by_token(_access_code, grant=True)
logger.debug(f"Session info: {_session_info}")
client_id = _session_info["client_id"]
if client_id != req["client_id"]:
logger.debug("{} owner of token".format(client_id))
logger.warning("{} using token it was not given".format(req["client_id"]))
return self.error_cls(error="invalid_grant", error_description="Wrong client")
if "grant_types_supported" in _context.cdb[client_id]:
grant_types_supported = _context.cdb[client_id].get("grant_types_supported")
else:
grant_types_supported = _context.provider_info["grant_types_supported"]
grant = _session_info["grant"]
token_type = "Bearer"
# Is DPOP supported
try:
_dpop_enabled = _context.dpop_enabled
except AttributeError:
_dpop_enabled = False
if _dpop_enabled:
_dpop_jkt = req.get("dpop_jkt")
if _dpop_jkt:
grant.extra["dpop_jkt"] = _dpop_jkt
token_type = "DPoP"
_based_on = grant.get_token(_access_code)
_supports_minting = _based_on.usage_rules.get("supports_minting", [])
_authn_req = grant.authorization_request
# If redirect_uri was in the initial authorization request
# verify that the one given here is the correct one.
if "redirect_uri" in _authn_req:
if req["redirect_uri"] != _authn_req["redirect_uri"]:
return self.error_cls(
error="invalid_request", error_description="redirect_uri mismatch"
)
logger.debug("All checks OK")
issue_refresh = kwargs.get("issue_refresh", None)
# The existence of offline_access scope overwrites issue_refresh
if issue_refresh is None and "offline_access" in grant.scope:
issue_refresh = True
_response = {
"token_type": token_type,
"scope": grant.scope,
}
if "access_token" in _supports_minting:
try:
token = self._mint_token(
token_class="access_token",
grant=grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=_based_on,
token_type=token_type,
)
except MintingNotAllowed as err:
logger.warning(err)
else:
_response["access_token"] = token.value
if token.expires_at:
_response["expires_in"] = token.expires_at - utc_time_sans_frac()
if (
issue_refresh
and "refresh_token" in _supports_minting
and "refresh_token" in grant_types_supported
):
try:
refresh_token = self._mint_token(
token_class="refresh_token",
grant=grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=_based_on,
)
except MintingNotAllowed as err:
logger.warning(err)
else:
_response["refresh_token"] = refresh_token.value
# since the grant content has changed. Make sure it's stored
_mngr[_session_info["session_id"]] = grant
if "openid" in _authn_req["scope"] and "id_token" in _supports_minting:
if "id_token" in _based_on.usage_rules.get("supports_minting"):
try:
_idtoken = self._mint_token(
token_class="id_token",
grant=grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=_based_on,
)
except (JWEException, NoSuitableSigningKeys) as err:
logger.warning(str(err))
resp = self.error_cls(
error="invalid_request",
error_description="Could not sign/encrypt id_token",
)
return resp
_response["id_token"] = _idtoken.value
_based_on.register_usage()
return _response
def post_parse_request(
self, request: Union[Message, dict], client_id: Optional[str] = "", **kwargs
):
"""
This is where clients come to get their access tokens
:param request: The request
:param client_id: Client identifier
:returns:
"""
_mngr = self.endpoint.server_get("endpoint_context").session_manager
try:
_session_info = _mngr.get_session_info_by_token(request["code"], grant=True)
except (KeyError, UnknownToken) as err:
logger.error("Access Code invalid", exc_info=err)
return self.error_cls(error="invalid_grant", error_description="Unknown code")
grant = _session_info["grant"]
code = grant.get_token(request["code"])
if not isinstance(code, AuthorizationCode):
return self.error_cls(error="invalid_request", error_description="Wrong token type")
if code.used: # Has been used already
# invalidate all tokens that has been minted using this code
grant.revoke_token(based_on=request["code"], recursive=True)
return self.error_cls(error="invalid_grant", error_description="Code inactive")
if code.is_active() is False:
return self.error_cls(error="invalid_grant", error_description="Code inactive")
_auth_req = grant.authorization_request
if "client_id" not in request: # Optional for access token request
request["client_id"] = _auth_req["client_id"]
logger.debug("%s: %s" % (request.__class__.__name__, sanitize(request)))
return request
class RefreshTokenHelper(TokenEndpointHelper):
def process_request(self, req: Union[Message, dict], **kwargs):
_context = self.endpoint.server_get("endpoint_context")
_mngr = _context.session_manager
if req["grant_type"] != "refresh_token":
return self.error_cls(error="invalid_request", error_description="Wrong grant_type")
token_value = req["refresh_token"]
_session_info = _mngr.get_session_info_by_token(token_value, grant=True)
if _session_info["client_id"] != req["client_id"]:
logger.debug("{} owner of token".format(_session_info["client_id"]))
logger.warning("{} using token it was not given".format(req["client_id"]))
return self.error_cls(error="invalid_grant", error_description="Wrong client")
_grant = _session_info["grant"]
token_type = "Bearer"
# Is DPOP supported
if "dpop_signing_alg_values_supported" in _context.provider_info:
_dpop_jkt = req.get("dpop_jkt")
if _dpop_jkt:
_grant.extra["dpop_jkt"] = _dpop_jkt
token_type = "DPoP"
token = _grant.get_token(token_value)
scope = _grant.find_scope(token.based_on)
if "scope" in req:
scope = req["scope"]
access_token = self._mint_token(
token_class="access_token",
grant=_grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=token,
scope=scope,
token_type=token_type,
)
_resp = {
"access_token": access_token.value,
"token_type": token_type,
"scope": scope,
}
if access_token.expires_at:
_resp["expires_in"] = access_token.expires_at - utc_time_sans_frac()
_mints = token.usage_rules.get("supports_minting")
issue_refresh = kwargs.get("issue_refresh", None)
# The existence of offline_access scope overwrites issue_refresh
if issue_refresh is None and "offline_access" in scope:
issue_refresh = True
if "refresh_token" in _mints and issue_refresh:
refresh_token = self._mint_token(
token_class="refresh_token",
grant=_grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=token,
scope=scope,
)
refresh_token.usage_rules = token.usage_rules.copy()
_resp["refresh_token"] = refresh_token.value
if "id_token" in _mints and "openid" in scope:
try:
_idtoken = self._mint_token(
token_class="id_token",
grant=_grant,
session_id=_session_info["session_id"],
client_id=_session_info["client_id"],
based_on=token,
scope=scope,
)
except (JWEException, NoSuitableSigningKeys) as err:
logger.warning(str(err))
resp = self.error_cls(
error="invalid_request",
error_description="Could not sign/encrypt id_token",
)
return resp
_resp["id_token"] = _idtoken.value
token.register_usage()
if (
"client_id" in req
and req["client_id"] in _context.cdb
and "revoke_refresh_on_issue" in _context.cdb[req["client_id"]]
):
revoke_refresh = _context.cdb[req["client_id"]].get("revoke_refresh_on_issue")
else:
revoke_refresh = revoke_refresh = self.endpoint.revoke_refresh_on_issue
if revoke_refresh:
token.revoke()
return _resp
def post_parse_request(
self, request: Union[Message, dict], client_id: Optional[str] = "", **kwargs
):
"""
This is where clients come to refresh their access tokens
:param request: The request
:param client_id: Client identifier
:returns:
"""
request = RefreshAccessTokenRequest(**request.to_dict())
_context = self.endpoint.server_get("endpoint_context")
try:
keyjar = _context.keyjar
except AttributeError:
keyjar = ""
request.verify(keyjar=keyjar, opponent_id=client_id)
_mngr = _context.session_manager
try:
_session_info = _mngr.get_session_info_by_token(request["refresh_token"], grant=True)
except KeyError:
logger.error("Access Code invalid")
return self.error_cls(error="invalid_grant")
grant = _session_info["grant"]
token = grant.get_token(request["refresh_token"])
if not isinstance(token, RefreshToken):
return self.error_cls(error="invalid_request", error_description="Wrong token type")
if token.is_active() is False:
return self.error_cls(
error="invalid_request", error_description="Refresh token inactive"
)
if "scope" in request:
req_scopes = set(request["scope"])
scopes = set(grant.find_scope(token.based_on))
if not req_scopes.issubset(scopes):
return self.error_cls(
error="invalid_request",
error_description="Invalid refresh scopes",
)
return request
class Token(oauth2.token.Token):
request_cls = Message
response_cls = oidc.AccessTokenResponse
error_cls = TokenErrorResponse
request_format = "json"
request_placement = "body"
response_format = "json"
response_placement = "body"
endpoint_name = "token_endpoint"
name = "token"
default_capabilities = {"token_endpoint_auth_signing_alg_values_supported": None}
helper_by_grant_type = {
"authorization_code": AccessTokenHelper,
"refresh_token": RefreshTokenHelper,
}