|
1 | 1 | Changing Default Behaviors |
2 | 2 | ========================== |
3 | 3 |
|
4 | | -NOTE: THIS DOCUMENTATION HAS NOT YET BEEN UPDATED |
5 | | - |
6 | | - |
7 | | -Changing callback functions |
8 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9 | | - |
10 | | -We provide what we think are sensible behaviors when attempting to access a |
11 | | -protected endpoint. If the access token is not valid for any reason (missing, |
12 | | -expired, tampered with, etc) we will return json in the format of {'msg': 'why |
13 | | -accessing endpoint failed'} along with an appropriate http status code |
14 | | -(generally 401 or 422). However, you may want to customize what you return in |
15 | | -some situations. We can do that with the jwt_manager loader functions. |
16 | | - |
| 4 | +This extension provides sensible default behaviors. For example, if an expired |
| 5 | +token attempts to access a protected endpoint, you will get a JSON response back |
| 6 | +like ``{"msg": "Token has expired"}`` and a 401 status code. However there may |
| 7 | +be various behaviors of this extension that you want to customize to your |
| 8 | +application's needs. We can do that with the various loader functions. Here is |
| 9 | +an example of how to do that. |
17 | 10 |
|
18 | 11 | .. literalinclude:: ../examples/loaders.py |
19 | 12 |
|
20 | | -Here are the possible loader functions. Click on the links for a more |
21 | | -more details about what arguments your callback functions should expect |
22 | | -and what the return values of your callback functions need to be. |
23 | | - |
24 | | -.. list-table:: |
25 | | - :header-rows: 1 |
26 | | - |
27 | | - * - Loader Decorator |
28 | | - - Description |
29 | | - * - :meth:`~flask_jwt_extended.JWTManager.token_verification_loader` |
30 | | - - Function that is called to do additional verifcations on the jwt data. Must return True or False |
31 | | - * - :meth:`~flask_jwt_extended.JWTManager.token_verification_failed_loader` |
32 | | - - Function that is called when the user claims verification callback returns False |
33 | | - * - :meth:`~flask_jwt_extended.JWTManager.decode_key_loader` |
34 | | - - Function that is called to get the decode key before verifying a token |
35 | | - * - :meth:`~flask_jwt_extended.JWTManager.encode_key_loader` |
36 | | - - Function that is called to get the encode key before creating a token |
37 | | - * - :meth:`~flask_jwt_extended.JWTManager.expired_token_loader` |
38 | | - - Function to call when an expired token accesses a protected endpoint |
39 | | - * - :meth:`~flask_jwt_extended.JWTManager.invalid_token_loader` |
40 | | - - Function to call when an invalid token accesses a protected endpoint |
41 | | - * - :meth:`~flask_jwt_extended.JWTManager.needs_fresh_token_loader` |
42 | | - - Function to call when a non-fresh token accesses a :func:`~flask_jwt_extended.jwt_required` endpoint |
43 | | - * - :meth:`~flask_jwt_extended.JWTManager.revoked_token_loader` |
44 | | - - Function to call when a revoked token accesses a protected endpoint |
45 | | - * - :meth:`~flask_jwt_extended.JWTManager.token_in_blocklist_loader` |
46 | | - - Function that is called to check if a token has been revoked |
47 | | - * - :meth:`~flask_jwt_extended.JWTManager.unauthorized_loader` |
48 | | - - Function to call when a request with no JWT accesses a protected endpoint |
49 | | - * - :meth:`~flask_jwt_extended.JWTManager.user_lookup_loader` |
50 | | - - Function to call to load a user object when token accesses a protected endpoint |
51 | | - * - :meth:`~flask_jwt_extended.JWTManager.user_lookup_error_loader` |
52 | | - - Function that is called when the user_lookup callback function returns `None` |
53 | | - |
54 | | -Dynamic token expires time |
55 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~ |
56 | | - |
57 | | -You can also change the expires time for a token via the `expires_delta` kwarg |
58 | | -in the :func:`~flask_jwt_extended.create_refresh_token` and |
59 | | -:func:`~flask_jwt_extended.create_access_token` functions. This takes |
60 | | -a `datetime.timedelta` and overrides the `JWT_REFRESH_TOKEN_EXPIRES` and |
61 | | -`JWT_ACCESS_TOKEN_EXPIRES` settings (see :ref:`Configuration Options`). |
62 | | - |
63 | | -This can be useful if you have different use cases for different tokens. |
64 | | -For example, you might use short lived access tokens used in your web |
65 | | -application, but you allow the creation of long lived access tokens that other |
66 | | -developers can generate and use to interact with your api in their programs. |
67 | | -You could accomplish this like such: |
68 | | - |
69 | | -.. code-block:: python |
70 | | -
|
71 | | - @app.route('/create-dev-token', methods=['POST']) |
72 | | - @jwt_required |
73 | | - def create_dev_token(): |
74 | | - username = get_jwt_identity() |
75 | | - expires = datetime.timedelta(days=365) |
76 | | - token = create_access_token(username, expires_delta=expires) |
77 | | - return jsonify({'token': token}), 201 |
78 | | -
|
79 | | -You can even disable expiration by setting `expires_delta` to `False`: |
80 | | - |
81 | | -.. code-block:: python |
82 | | -
|
83 | | - @app.route('/create-api-token', methods=['POST']) |
84 | | - @jwt_required |
85 | | - def create_api_token(): |
86 | | - username = get_jwt_identity() |
87 | | - token = create_access_token(username, expires_delta=False) |
88 | | - return jsonify({'token': token}), 201 |
89 | | -
|
90 | | -Note that in this case, you should enable token revoking (see :ref:`Blocklist and Token Revoking`). |
| 13 | +There are all sorts of callbacks that can be defined to customize the behaviors |
| 14 | +of this extension. See the :ref:`Configuring Flask-JWT-Extended` API Documentation |
| 15 | +for a full list of callback functions that are available in this extension. |
0 commit comments