-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
jwt_manager.py
563 lines (456 loc) · 23.4 KB
/
jwt_manager.py
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import datetime
from typing import Any
from typing import Callable
from typing import Optional
import jwt
from flask import Flask
from jwt import DecodeError
from jwt import ExpiredSignatureError
from jwt import InvalidAudienceError
from jwt import InvalidIssuerError
from jwt import InvalidTokenError
from jwt import MissingRequiredClaimError
from flask_jwt_extended.config import config
from flask_jwt_extended.default_callbacks import default_additional_claims_callback
from flask_jwt_extended.default_callbacks import default_blocklist_callback
from flask_jwt_extended.default_callbacks import default_decode_key_callback
from flask_jwt_extended.default_callbacks import default_encode_key_callback
from flask_jwt_extended.default_callbacks import default_expired_token_callback
from flask_jwt_extended.default_callbacks import default_invalid_token_callback
from flask_jwt_extended.default_callbacks import default_jwt_headers_callback
from flask_jwt_extended.default_callbacks import default_needs_fresh_token_callback
from flask_jwt_extended.default_callbacks import default_revoked_token_callback
from flask_jwt_extended.default_callbacks import default_token_verification_callback
from flask_jwt_extended.default_callbacks import (
default_token_verification_failed_callback,
)
from flask_jwt_extended.default_callbacks import default_unauthorized_callback
from flask_jwt_extended.default_callbacks import default_user_identity_callback
from flask_jwt_extended.default_callbacks import default_user_lookup_error_callback
from flask_jwt_extended.exceptions import CSRFError
from flask_jwt_extended.exceptions import FreshTokenRequired
from flask_jwt_extended.exceptions import InvalidHeaderError
from flask_jwt_extended.exceptions import InvalidQueryParamError
from flask_jwt_extended.exceptions import JWTDecodeError
from flask_jwt_extended.exceptions import NoAuthorizationError
from flask_jwt_extended.exceptions import RevokedTokenError
from flask_jwt_extended.exceptions import UserClaimsVerificationError
from flask_jwt_extended.exceptions import UserLookupError
from flask_jwt_extended.exceptions import WrongTokenError
from flask_jwt_extended.tokens import _decode_jwt
from flask_jwt_extended.tokens import _encode_jwt
from flask_jwt_extended.typing import ExpiresDelta
from flask_jwt_extended.typing import Fresh
from flask_jwt_extended.utils import current_user_context_processor
class JWTManager(object):
"""
An object used to hold JWT settings and callback functions for the
Flask-JWT-Extended extension.
Instances of :class:`JWTManager` are *not* bound to specific apps, so
you can create one in the main body of your code and then bind it
to your app in a factory function.
"""
def __init__(
self, app: Optional[Flask] = None, add_context_processor: bool = False
) -> None:
"""
Create the JWTManager instance. You can either pass a flask application
in directly here to register this extension with the flask app, or
call init_app after creating this object (in a factory pattern).
:param app:
The Flask Application object
:param add_context_processor:
Controls if `current_user` is should be added to flasks template
context (and thus be available for use in Jinja templates). Defaults
to ``False``.
"""
# Register the default error handler callback methods. These can be
# overridden with the appropriate loader decorators
self._decode_key_callback = default_decode_key_callback
self._encode_key_callback = default_encode_key_callback
self._expired_token_callback = default_expired_token_callback
self._invalid_token_callback = default_invalid_token_callback
self._jwt_additional_header_callback = default_jwt_headers_callback
self._needs_fresh_token_callback = default_needs_fresh_token_callback
self._revoked_token_callback = default_revoked_token_callback
self._token_in_blocklist_callback = default_blocklist_callback
self._token_verification_callback = default_token_verification_callback
self._unauthorized_callback = default_unauthorized_callback
self._user_claims_callback = default_additional_claims_callback
self._user_identity_callback = default_user_identity_callback
self._user_lookup_callback: Optional[Callable] = None
self._user_lookup_error_callback = default_user_lookup_error_callback
self._token_verification_failed_callback = (
default_token_verification_failed_callback
)
# Register this extension with the flask app now (if it is provided)
if app is not None:
self.init_app(app, add_context_processor)
def init_app(self, app: Flask, add_context_processor: bool = False) -> None:
"""
Register this extension with the flask app.
:param app:
The Flask Application object
:param add_context_processor:
Controls if `current_user` is should be added to flasks template
context (and thus be available for use in Jinja templates). Defaults
to ``False``.
"""
# Save this so we can use it later in the extension
if not hasattr(app, "extensions"): # pragma: no cover
app.extensions = {}
app.extensions["flask-jwt-extended"] = self
if add_context_processor:
app.context_processor(current_user_context_processor)
# Set all the default configurations for this extension
self._set_default_configuration_options(app)
self._set_error_handler_callbacks(app)
def _set_error_handler_callbacks(self, app: Flask) -> None:
@app.errorhandler(CSRFError)
def handle_csrf_error(e):
return self._unauthorized_callback(str(e))
@app.errorhandler(DecodeError)
def handle_decode_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(ExpiredSignatureError)
def handle_expired_error(e):
return self._expired_token_callback(e.jwt_header, e.jwt_data)
@app.errorhandler(FreshTokenRequired)
def handle_fresh_token_required(e):
return self._needs_fresh_token_callback(e.jwt_header, e.jwt_data)
@app.errorhandler(MissingRequiredClaimError)
def handle_missing_required_claim_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(InvalidAudienceError)
def handle_invalid_audience_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(InvalidIssuerError)
def handle_invalid_issuer_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(InvalidHeaderError)
def handle_invalid_header_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(InvalidTokenError)
def handle_invalid_token_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(JWTDecodeError)
def handle_jwt_decode_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(NoAuthorizationError)
def handle_auth_error(e):
return self._unauthorized_callback(str(e))
@app.errorhandler(InvalidQueryParamError)
def handle_invalid_query_param_error(e):
return self._invalid_token_callback(str(e))
@app.errorhandler(RevokedTokenError)
def handle_revoked_token_error(e):
return self._revoked_token_callback(e.jwt_header, e.jwt_data)
@app.errorhandler(UserClaimsVerificationError)
def handle_failed_token_verification(e):
return self._token_verification_failed_callback(e.jwt_header, e.jwt_data)
@app.errorhandler(UserLookupError)
def handler_user_lookup_error(e):
return self._user_lookup_error_callback(e.jwt_header, e.jwt_data)
@app.errorhandler(WrongTokenError)
def handle_wrong_token_error(e):
return self._invalid_token_callback(str(e))
@staticmethod
def _set_default_configuration_options(app: Flask) -> None:
app.config.setdefault(
"JWT_ACCESS_TOKEN_EXPIRES", datetime.timedelta(minutes=15)
)
app.config.setdefault("JWT_ACCESS_COOKIE_NAME", "access_token_cookie")
app.config.setdefault("JWT_ACCESS_COOKIE_PATH", "/")
app.config.setdefault("JWT_ACCESS_CSRF_COOKIE_NAME", "csrf_access_token")
app.config.setdefault("JWT_ACCESS_CSRF_COOKIE_PATH", "/")
app.config.setdefault("JWT_ACCESS_CSRF_FIELD_NAME", "csrf_token")
app.config.setdefault("JWT_ACCESS_CSRF_HEADER_NAME", "X-CSRF-TOKEN")
app.config.setdefault("JWT_ALGORITHM", "HS256")
app.config.setdefault("JWT_COOKIE_CSRF_PROTECT", True)
app.config.setdefault("JWT_COOKIE_DOMAIN", None)
app.config.setdefault("JWT_COOKIE_SAMESITE", None)
app.config.setdefault("JWT_COOKIE_SECURE", False)
app.config.setdefault("JWT_CSRF_CHECK_FORM", False)
app.config.setdefault("JWT_CSRF_IN_COOKIES", True)
app.config.setdefault("JWT_CSRF_METHODS", ["POST", "PUT", "PATCH", "DELETE"])
app.config.setdefault("JWT_DECODE_ALGORITHMS", None)
app.config.setdefault("JWT_DECODE_AUDIENCE", None)
app.config.setdefault("JWT_DECODE_ISSUER", None)
app.config.setdefault("JWT_DECODE_LEEWAY", 0)
app.config.setdefault("JWT_ENCODE_AUDIENCE", None)
app.config.setdefault("JWT_ENCODE_ISSUER", None)
app.config.setdefault("JWT_ERROR_MESSAGE_KEY", "msg")
app.config.setdefault("JWT_HEADER_NAME", "Authorization")
app.config.setdefault("JWT_HEADER_TYPE", "Bearer")
app.config.setdefault("JWT_IDENTITY_CLAIM", "sub")
app.config.setdefault("JWT_JSON_KEY", "access_token")
app.config.setdefault("JWT_PRIVATE_KEY", None)
app.config.setdefault("JWT_PUBLIC_KEY", None)
app.config.setdefault("JWT_QUERY_STRING_NAME", "jwt")
app.config.setdefault("JWT_QUERY_STRING_VALUE_PREFIX", "")
app.config.setdefault("JWT_REFRESH_COOKIE_NAME", "refresh_token_cookie")
app.config.setdefault("JWT_REFRESH_COOKIE_PATH", "/")
app.config.setdefault("JWT_REFRESH_CSRF_COOKIE_NAME", "csrf_refresh_token")
app.config.setdefault("JWT_REFRESH_CSRF_COOKIE_PATH", "/")
app.config.setdefault("JWT_REFRESH_CSRF_FIELD_NAME", "csrf_token")
app.config.setdefault("JWT_REFRESH_CSRF_HEADER_NAME", "X-CSRF-TOKEN")
app.config.setdefault("JWT_REFRESH_JSON_KEY", "refresh_token")
app.config.setdefault("JWT_REFRESH_TOKEN_EXPIRES", datetime.timedelta(days=30))
app.config.setdefault("JWT_SECRET_KEY", None)
app.config.setdefault("JWT_SESSION_COOKIE", True)
app.config.setdefault("JWT_TOKEN_LOCATION", ("headers",))
app.config.setdefault("JWT_ENCODE_NBF", True)
def additional_claims_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to add additional claims
when creating a JWT. The claims returned by this function will be merged
with any claims passed in via the ``additional_claims`` argument to
:func:`~flask_jwt_extended.create_access_token` or
:func:`~flask_jwt_extended.create_refresh_token`.
The decorated function must take **one** argument.
The argument is the identity that was used when creating a JWT.
The decorated function must return a dictionary of claims to add to the JWT.
"""
self._user_claims_callback = callback
return callback
def additional_headers_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to add additional headers
when creating a JWT. The headers returned by this function will be merged
with any headers passed in via the ``additional_headers`` argument to
:func:`~flask_jwt_extended.create_access_token` or
:func:`~flask_jwt_extended.create_refresh_token`.
The decorated function must take **one** argument.
The argument is the identity that was used when creating a JWT.
The decorated function must return a dictionary of headers to add to the JWT.
"""
self._jwt_additional_header_callback = callback
return callback
def decode_key_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for dynamically setting the JWT
decode key based on the **UNVERIFIED** contents of the token. Think
carefully before using this functionality, in most cases you probably
don't need it.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the
unverified JWT.
The second argument is a dictionary containing the payload data of the
unverified JWT.
The decorated function must return a *string* that is used to decode and
verify the token.
"""
self._decode_key_callback = callback
return callback
def encode_key_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for dynamically setting the JWT
encode key based on the tokens identity. Think carefully before using this
functionality, in most cases you probably don't need it.
The decorated function must take **one** argument.
The argument is the identity used to create this JWT.
The decorated function must return a *string* which is the secrete key used to
encode the JWT.
"""
self._encode_key_callback = callback
return callback
def expired_token_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for returning a custom
response when an expired JWT is encountered.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return a Flask Response.
"""
self._expired_token_callback = callback
return callback
def invalid_token_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for returning a custom
response when an invalid JWT is encountered.
This decorator sets the callback function that will be used if an
invalid JWT attempts to access a protected endpoint.
The decorated function must take **one** argument.
The argument is a string which contains the reason why a token is invalid.
The decorated function must return a Flask Response.
"""
self._invalid_token_callback = callback
return callback
def needs_fresh_token_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for returning a custom
response when a valid and non-fresh token is used on an endpoint
that is marked as ``fresh=True``.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return a Flask Response.
"""
self._needs_fresh_token_callback = callback
return callback
def revoked_token_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function for returning a custom
response when a revoked token is encountered.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return a Flask Response.
"""
self._revoked_token_callback = callback
return callback
def token_in_blocklist_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to check if a JWT has
been revoked.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must be return ``True`` if the token has been
revoked, ``False`` otherwise.
"""
self._token_in_blocklist_callback = callback
return callback
def token_verification_failed_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to return a custom
response when the claims verification check fails.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return a Flask Response.
"""
self._token_verification_failed_callback = callback
return callback
def token_verification_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used for custom verification
of a valid JWT.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return ``True`` if the token is valid, or
``False`` otherwise.
"""
self._token_verification_callback = callback
return callback
def unauthorized_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to return a custom
response when no JWT is present.
The decorated function must take **one** argument.
The argument is a string that explains why the JWT could not be found.
The decorated function must return a Flask Response.
"""
self._unauthorized_callback = callback
return callback
def user_identity_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to convert an identity to
a JSON serializable format when creating JWTs. This is useful for
using objects (such as SQLAlchemy instances) as the identity when
creating your tokens.
The decorated function must take **one** argument.
The argument is the identity that was used when creating a JWT.
The decorated function must return JSON serializable data.
"""
self._user_identity_callback = callback
return callback
def user_lookup_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to convert a JWT into
a python object that can be used in a protected endpoint. This is useful
for automatically loading a SQLAlchemy instance based on the contents
of the JWT.
The object returned from this function can be accessed via
:attr:`~flask_jwt_extended.current_user` or
:meth:`~flask_jwt_extended.get_current_user`
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function can return any python object, which can then be
accessed in a protected endpoint. If an object cannot be loaded, for
example if a user has been deleted from your database, ``None`` must be
returned to indicate that an error occurred loading the user.
"""
self._user_lookup_callback = callback
return callback
def user_lookup_error_loader(self, callback: Callable) -> Callable:
"""
This decorator sets the callback function used to return a custom
response when loading a user via
:meth:`~flask_jwt_extended.JWTManager.user_lookup_loader` fails.
The decorated function must take **two** arguments.
The first argument is a dictionary containing the header data of the JWT.
The second argument is a dictionary containing the payload data of the JWT.
The decorated function must return a Flask Response.
"""
self._user_lookup_error_callback = callback
return callback
def _encode_jwt_from_config(
self,
identity: Any,
token_type: str,
claims=None,
fresh: Fresh = False,
expires_delta: Optional[ExpiresDelta] = None,
headers=None,
) -> str:
header_overrides = self._jwt_additional_header_callback(identity)
if headers is not None:
header_overrides.update(headers)
claim_overrides = self._user_claims_callback(identity)
if claims is not None:
claim_overrides.update(claims)
if expires_delta is None:
if token_type == "access":
expires_delta = config.access_expires
else:
expires_delta = config.refresh_expires
return _encode_jwt(
algorithm=config.algorithm,
audience=config.encode_audience,
claim_overrides=claim_overrides,
csrf=config.cookie_csrf_protect,
expires_delta=expires_delta,
fresh=fresh,
header_overrides=header_overrides,
identity=self._user_identity_callback(identity),
identity_claim_key=config.identity_claim_key,
issuer=config.encode_issuer,
json_encoder=config.json_encoder,
secret=self._encode_key_callback(identity),
token_type=token_type,
nbf=config.encode_nbf,
)
def _decode_jwt_from_config(
self, encoded_token: str, csrf_value=None, allow_expired: bool = False
) -> dict:
unverified_claims = jwt.decode(
encoded_token,
algorithms=config.decode_algorithms,
options={"verify_signature": False},
)
unverified_headers = jwt.get_unverified_header(encoded_token)
secret = self._decode_key_callback(unverified_headers, unverified_claims)
kwargs = {
"algorithms": config.decode_algorithms,
"audience": config.decode_audience,
"csrf_value": csrf_value,
"encoded_token": encoded_token,
"identity_claim_key": config.identity_claim_key,
"issuer": config.decode_issuer,
"leeway": config.leeway,
"secret": secret,
"verify_aud": config.decode_audience is not None,
}
try:
return _decode_jwt(**kwargs, allow_expired=allow_expired)
except ExpiredSignatureError as e:
# TODO: If we ever do another breaking change, don't raise this pyjwt
# error directly, instead raise a custom error of ours from this
# error.
e.jwt_header = unverified_headers # type: ignore
e.jwt_data = _decode_jwt(**kwargs, allow_expired=True) # type: ignore
raise