@@ -3,8 +3,8 @@ Refreshing Tokens
33In most web applications, it would not be ideal if a user was logged out in the
44middle of doing something because their JWT expired. Unfortunately we can't just
55change the expires time on a JWT on each request, as once a JWT is created it
6- cannot be modified, only replaced with a new JWT . Lets take a look at how we can
7- simulate these behaviors .
6+ cannot be modified. Lets take a look at some options for solving this problem
7+ by refreshing JWTs .
88
99Implicit Refreshing With Cookies
1010~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -24,6 +24,59 @@ This is our recommended approach when your frontend is a website.
2424
2525Explicit Refreshing With Refresh Tokens
2626~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27- TODO
27+ Alternatively, this extension comes out of the box with refresh token support.
28+ A refresh token is a long lived JWT that can only be used to creating new access
29+ tokens.
30+
31+ You have a couple choices about how to utilize a refresh token. You could store
32+ the expires time of your access token on your frontend, and each time you make
33+ an API request first check if the current access token is near or already
34+ expired, and refresh it as needed. This approach is pretty simple and will work
35+ fine in most cases, but do be aware that if your frontend has a clock that is
36+ significantly off, you might run into issues.
37+
38+ An alternative approach involves making an API request with your access token
39+ and then checking the result to see if it worked. If the result of the request
40+ is an error message saying that your token is expired, use the refresh token to
41+ generate a new access token and redo the request with the new token. This approach
42+ will work regardless of the clock on your frontend, but it does require having
43+ some potentially more complicated logic.
44+
45+ Using refresh tokens is our recommended approach when your frontend is not a
46+ website (mobile, api only, etc).
2847
2948.. literalinclude :: ../examples/refresh_tokens.py
49+
50+ Making a request with a refresh token looks just like making a request with
51+ an access token. Here is an example using `HTTPie <https://httpie.io/ >`_.
52+
53+ .. code-block :: bash
54+
55+ $ http POST :5000/refresh Authorization:"Bearer $REFRESH_TOKEN"
56+
57+
58+ Token Freshness Pattern
59+ ~~~~~~~~~~~~~~~~~~~~~~~
60+ The token freshness pattern is a very simple idea. Every time a user authenticates
61+ by providing a username and password, they receive a `fresh ` access token that
62+ can access any route. But after some time, that token should no longer be considered
63+ `fresh `, and some critical or dangerous routes will be blocked until the user
64+ verifies their password again. All other routes will still work normally for
65+ the user even though their token is no longer `fresh `. As an example, we might
66+ not allow users to change their email address unless they have a `fresh ` token,
67+ but we do allow them use the rest of our Flask application normally.
68+
69+ The token freshness pattern is built into this extension, and works seamlessly
70+ with both token refreshing strategies discussed above. Lets take a look at this
71+ with the explicit refresh example (it will look basically same in the implicit
72+ refresh example).
73+
74+ .. literalinclude :: ../examples/token_freshness.py
75+
76+ We also support marking a token as fresh for a given amount of time after it
77+ is created. You can do this by passing a `datetime.timedelta ` to the `fresh `
78+ option when creating JWTs:
79+
80+ .. code-block :: python
81+
82+ create_access_token(identity, fresh=datetime.timedelta(minutes=15))
0 commit comments