Skip to content

Commit 5383253

Browse files
committed
More documentation updates
1 parent b3e3798 commit 5383253

File tree

10 files changed

+95
-56
lines changed

10 files changed

+95
-56
lines changed

docs/add_custom_data_claims.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
Adding Custom Data (Claims) to the Access Token
2-
===============================================
1+
Storing Data in Access Tokens
2+
=============================
33

4-
You may want to store additional information in the access token. Perhaps you
5-
want to save the permissions this user has so you can access them in the view
6-
functions, without having to make a database call each time. This can be done
7-
with the user_claims_loader decorator, and accessed later with the
8-
'get_jwt_claims()' method (in a protected endpoint).
4+
You may want to store additional information in the access token which you could
5+
later access in the protected views. This can be done with the **user_claims_loader**
6+
decorator, and the data can be accessed later in a protected endpoint
7+
with the **get_jwt_claims()** method.
98

109
.. literalinclude:: ../examples/additional_data_in_access_token.py
1110

1211
Storing data in an access token can be good for performance. If you store data
1312
in the token, you wont need to look it up from disk next time you need it in
14-
a protected endpoint. But be warned, any data in the access token can be easily
15-
viewed with anyone who has access to said token, so refrain from storing
16-
critical data there!
13+
a protected endpoint. However, you should take care what data you put in the
14+
token. Any data in the access token can be easily viewed with anyone who has
15+
access to the token. Take care to avoid storing sensative information in here!

docs/basic_usage.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Basic Usage
22
===========
33

4-
54
In its simplest form, there is not much to using flask_jwt_extended.
65

7-
86
.. literalinclude:: ../examples/simple.py
97

10-
To access a jwt_required protected view, all we have to do is send an authorization head with the request that include the token. The header looks like this:
8+
To access a jwt_required protected view, all we have to do is send in the
9+
JWT with the request. By default, this is done with an authorization header
10+
that looks like:
1111

1212
.. code-block :: bash
1313
@@ -35,3 +35,8 @@ We can see this in action using CURL:
3535
{
3636
"hello": "world"
3737
}
38+
39+
NOTE: Remember to change the secret key of your application, and insure that no
40+
one is able to view it. The json web tokens are signed with the secret key, so
41+
if someone gets that, they can create arbitrary tokens, and in essence log in
42+
as any user.

docs/changing_default_behavior.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ We provide what we think are sensible behaviors when attempting to access a
55
protected endpoint. If the access token is not valid for any reason (missing,
66
expired, tampered with, etc) we will return json in the format of {'msg': 'why
77
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.
8+
(generally 401 or 422). However, you may want to customize what you return in
9+
some situations. We can do that with the jwt_manager loader functions.
1010

1111

1212
.. literalinclude:: ../examples/loaders.py
@@ -16,21 +16,21 @@ Possible loader functions are:
1616
.. list-table::
1717
:header-rows: 1
1818

19-
* - Decorator
19+
* - Loader Decorator
2020
- Description
21-
- Callback Function Arguments
22-
* - expired_token_loader
23-
- Function to call when an expired token accesses a protected view
21+
- Function Arguments
22+
* - **expired_token_loader**
23+
- Function to call when an expired token accesses a protected endpoint
2424
- 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
25+
* - **invalid_token_loader**
26+
- Function to call when an invalid token accesses a protected endpoint
27+
- Takes one argument - an error string indicating why the token is invalid
28+
* - **unauthorized_loader**
29+
- Function to call when a request with no JWT accesses a protected endpoint
3030
- None
31-
* - needs_fresh_token_loader
32-
- Function to call when a non-fresh token tries to access a **fresh_jwt_required** view
31+
* - **needs_fresh_token_loader**
32+
- Function to call when a non-fresh token accesses a **fresh_jwt_required** endpoint
3333
- None
34-
* - revoked_token_loader
35-
- Function to call when a revoked token accesses a protected view
34+
* - **revoked_token_loader**
35+
- Function to call when a revoked token accesses a protected endpoint
3636
- None

docs/options.rst

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
Configuration Options
22
=====================
33

4-
.. literalinclude:: ../flask_jwt_extended/config.py
5-
:language: python
4+
You can change many options for how this extension works via
65

7-
.. .. automodule:: flask_jwt_extended.config
8-
.. :members:
6+
.. code-block:: python
7+
8+
app.config[OPTION_NAME] = new_options
9+
10+
The available options are:
11+
12+
.. tabularcolumns:: |p{6.5cm}|p{8.5cm}|
13+
================================= =========================================
14+
``JWT_TOKEN_LOCATION`` Where to find the JWT in the request. The options are ``'headers'`` or
15+
``'cookies'``. Defaults to ``'headers'``
16+
``JWT_HEADER_NAME`` What header to look for the JWT in a request. Only has an effect if
17+
JWT_TOKEN_LOCATION is 'headers'. Defaults to ``'Authorization'``
18+
``JWT_HEADER_TYPE`` What type of header the JWT is in. Defaults to ``'Bearer'``. This can be
19+
an empty string, in which case the header only contains the JWT
20+
``JWT_COOKIE_CSRF_PROTECT`` Enable/disable CSRF protection when using 'cookies' as the JWT_TOKEN_LOCATION.
21+
This has no affect if using 'headers' as the JWT_TOKEN_LOCATION
22+
``JWT_ACCESS_CSRF_COOKIE_NAME`` Name of the CSRF access cookie. Defaults to ``'csrf_access_token'``. Only used
23+
if using cookies with CSRF protection enabled
24+
``JWT_REFRESH_CSRF_COOKIE_NAME`` Name of the CSRF refresh cookie. Defaults to ``'csrf_refresh_token'``. Only used
25+
if using cookies with CSRF protection enabled
26+
``JWT_CSRF_HEADER_NAME`` Name of the header that we will look for the CSRF double submit token in.
27+
Defaults to ``X-CSRF-TOKEN``. Only used if using cookies with CSRF protection enabled
28+
``JWT_ACCESS_TOKEN_EXPIRES`` How long an access token should live before it expires. This takes a
29+
``datetime.timedelta``, and defaults to 15 minutes
30+
``JWT_REFRESH_TOKEN_EXPIRES`` How long a refresh token should live before it expires. This takes a
31+
``datetime.timedelta``, and defaults to 30 days
32+
``JWT_ALGORITHM`` Which algorithm to sign the JWT with. `See here
33+
<https://pyjwt.readthedocs.io/en/latest/algorithms.html>`_ for the options. Defaults
34+
to ``'HS256'``. Note that Asymmetric (Public-key) Algorithms are not currently supported.
35+
``JWT_BLACKLIST_ENABLED`` Enable/disable token blackliting and revoking. Defaults to ``False``
36+
``JWT_BLACKLIST_STORE`` Where to save revoked tokens. `See here
37+
<http://pythonhosted.org/simplekv/>`_ for options.
38+
``JWT_BLACKLIST_CHECKS`` What token types to check against the blacklist. Options are
39+
``'refresh'`` or ``'all'``. Defaults to ``'refresh'``
40+
================================= =========================================

docs/refresh_tokens.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
Refresh Tokens
22
==============
33

4-
Flask-JWT-Extended supports refresh tokens out of the box. These are longer
5-
lived token which cannot access a jwt_required protected endpoint, but can be
6-
used to create new access tokens once an old access token has expired.
4+
Flask-JWT-Extended supports refresh tokens out of the box. These are long
5+
lived tokens which can be used to create new access tokens once an old access
6+
token has expired. Refresh tokens cannot access a **jwt_requred** protected
7+
endpoint.
78

89
.. literalinclude:: ../examples/refresh_tokens.py
910

1011
By setting the access tokens to a shorter lifetime (see Configuration Options),
11-
and utilizing fresh tokens for critical views (see Token Freshness next) we can
12-
help reduce the damage done if an access token is stolen.
12+
and utilizing refresh tokens we can help reduce the damage that can be done if
13+
an access token is stolen. However, we need to take extra care to prevent the
14+
refresh token from being stolen. If an attacker gets his hands on this, he can
15+
keep generating new access tokens and accessing protected endpoints as though
16+
he was that user. We can help combat this by using fresh tokens, discussed in
17+
the next section.

docs/token_freshness.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
Token Freshness
22
===============
33

4-
5-
64
We have the idea of token freshness built into this extension. In a nutshell,
75
you can choose to mark some access tokens as fresh and others as non-fresh, and
8-
use the fresh_jwt_required decorator to only allow fresh tokens to access some
9-
views.
6+
use the **fresh_jwt_required** decorator to only allow fresh tokens to access some
7+
endpoints.
108

119
This is useful for allowing fresh tokens to do some critical things (maybe
1210
change a password, or complete an online purchase), but to deny those features
13-
to non-fresh tokens without forcing them to re-authenticate. This still allows
14-
your users to access any of the normal jwt_protected endpoints while using a
15-
non-fresh token. This can lead to a more secure site, without creating a
16-
burden on the users experiences by forcing them to always be re-authenticating.
11+
to non-fresh tokens (until they re-authenticate and get a new fresh token).
12+
Fresh tokens can lead to a more secure site, without creating a bad users
13+
experience by making users re-authenticate all the time.
1714

18-
The provided API gives you the power to use the token freshness however you may
19-
want to. A very natural way to do this would be to mark a token as fresh when
20-
they first login, mark any tokens generated with the refresh token to as not
15+
The provided API gives you the power to create and use fresh tokens however you
16+
want. A very natural way to utilize them is to mark a token as fresh when
17+
someone first logs in, and mark tokens generated by a refresh token as not
2118
fresh.
2219

23-
2420
.. literalinclude:: ../examples/token_freshness.py
2521

examples/loaders.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
jwt = JWTManager(app)
88

99

10-
# Use the expired_token_loader to call this function whenever an
11-
# expired but # otherwise valid access token tries to access
12-
# an endpoint
10+
# Using the expired_token_loader decorator, we will now call
11+
# this function whenever an expired but otherwise valid access
12+
# token attempts to access an endpoint
1313
@jwt.expired_token_loader
1414
def my_expired_token_callback():
1515
return jsonify({

examples/refresh_tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def login():
2727
# The jwt_refresh_token_required decorator insures a valid refresh
2828
# token is present in the request before calling this endpoint. We
2929
# can use the get_jwt_identity() function to get the identity of
30-
# the refresh toke, and use the create_access_token() function again
30+
# the refresh token, and use the create_access_token() function again
3131
# to make a new access token for this identity.
3232
@app.route('/refresh', methods=['POST'])
3333
@jwt_refresh_token_required

examples/token_freshness.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def login():
2929

3030

3131
# Fresh login endpoint. This is designed to be used if we need to
32-
# make a fresh # token for a user (by verifying they have the
32+
# make a fresh token for a user (by verifying they have the
3333
# correct username and password). Unlike the standard login endpoint,
34-
# this will only return a new access token (so that we don't keep
35-
# generating new refresh tokens, which defeats their point)
34+
# this will only return a new access token, so that we don't keep
35+
# generating new refresh tokesn, which entirely defeats their point.
3636
@app.route('/fresh-login', methods=['POST'])
3737
def fresh_login():
3838
username = request.json.get('username', None)

flask_jwt_extended/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
ACCESS_COOKIE_PATH = None
3030
REFRESH_COOKIE_PATH = None
3131

32+
# TODO only one header for both access and refresh tokens, as we will only be
33+
# checking one of those at a time
3234
# Options for using double submit for verifying CSRF tokens
3335
COOKIE_CSRF_PROTECT = True
3436
ACCESS_CSRF_COOKIE_NAME = 'csrf_access_token'

0 commit comments

Comments
 (0)