|
1 | 1 | Changing Default Behaviors |
2 | 2 | ========================== |
3 | 3 |
|
4 | | - |
5 | | -We provide what we think are sensible behaviors when attempting to access a protected endpoint. If the access token is not valid for any reason (missing, expired, tampered with, etc) we will return json in the format of {'msg': 'why accessing endpoint failed'} along with an appropriate http status code (generally 401 or 422). However, you may want to customize what you returned in these situations. We can do that with the jwt_manager _loader functions. |
6 | | - |
7 | | - |
8 | | -.. code-block:: python |
9 | | -
|
10 | | - from flask import Flask, jsonify, request |
11 | | - from flask_jwt_extended import JWTManager, jwt_required, create_access_token |
12 | | -
|
13 | | - app = Flask(__name__) |
14 | | - app.secret_key = 'super-secret' # Change this! |
15 | | - jwt = JWTManager(app) |
16 | | -
|
17 | | -
|
18 | | - # Use the expired_token_loader to call this function whenever an expired but |
19 | | - # otherwise valid access token tries to access an endpoint |
20 | | - @jwt.expired_token_loader |
21 | | - def my_expired_token_callback(): |
22 | | - return jsonify({ |
23 | | - 'status': 401, |
24 | | - 'sub_status': 101, |
25 | | - 'msg': 'The token has expired' |
26 | | - }), 200 |
27 | | -
|
28 | | -
|
29 | | - @app.route('/login', methods=['POST']) |
30 | | - def login(): |
31 | | - username = request.json.get('username', None) |
32 | | - password = request.json.get('password', None) |
33 | | - if username != 'test' and password != 'test': |
34 | | - return jsonify({"msg": "Bad username or password"}), 401 |
35 | | -
|
36 | | - ret = {'access_token': create_access_token(username)} |
37 | | - return jsonify(ret), 200 |
38 | | -
|
39 | | -
|
40 | | - @app.route('/protected', methods=['GET']) |
41 | | - @jwt_required |
42 | | - def protected(): |
43 | | - return jsonify({'hello': 'world'}), 200 |
44 | | -
|
45 | | - if __name__ == '__main__': |
46 | | - app.run() |
47 | | -
|
48 | | -
|
49 | | -
|
50 | | -************************************ |
51 | | -Loader functions are: |
52 | | -************************************ |
53 | | - |
54 | | -.. autoclass:: flask_jwt_extended.jwt_manager.JWTManager |
55 | | - :members: |
56 | | -.. |
57 | | -.. .. literalinclude:: ../flask_jwt_extended/jwt_manager.py |
58 | | -.. :language: python |
59 | | -.. :emphasize-lines: 60-122 |
60 | | -.. :linenos: |
| 4 | +We provide what we think are sensible behaviors when attempting to access a |
| 5 | +protected endpoint. If the access token is not valid for any reason (missing, |
| 6 | +expired, tampered with, etc) we will return json in the format of {'msg': 'why |
| 7 | +accessing endpoint failed'} along with an appropriate http status code |
| 8 | +(generally 401 or 422). However, you may want to customize what you returned in |
| 9 | +these situations. We can do that with the jwt_manager loader functions. |
| 10 | + |
| 11 | + |
| 12 | +.. literalinclude:: ../examples/loaders.py |
| 13 | + |
| 14 | +Possible loader functions are: |
| 15 | + |
| 16 | +.. list-table:: |
| 17 | + :header-rows: 1 |
| 18 | + |
| 19 | + * - Decorator |
| 20 | + - Description |
| 21 | + - Callback Function Arguments |
| 22 | + * - expired_token_loader |
| 23 | + - Function to call when an expired token accesses a protected view |
| 24 | + - None |
| 25 | + * - invalid_token_loader |
| 26 | + - Function to call when an invalid token accesses a protected view |
| 27 | + - One argument: error string of why it is invalid |
| 28 | + * - unauthorized_loader |
| 29 | + - Function to call when a request with no JWT accesses a protected view |
| 30 | + - None |
| 31 | + * - needs_fresh_token_loader |
| 32 | + - Function to call when a non-fresh token tries to access a **fresh_jwt_required** view |
| 33 | + - None |
| 34 | + * - revoked_token_loader |
| 35 | + - Function to call when a revoked token accesses a protected view |
| 36 | + - None |
0 commit comments