@@ -3,35 +3,39 @@ Blacklist and Token Revoking
3
3
4
4
This extension supports optional token revoking out of the box. This will
5
5
allow you to revoke a specific token so that it can no longer access your endpoints.
6
- In order to revoke a token, we need some storage where we can save a list of all
7
- the tokens we have created, as well as if they have been revoked or not . In order
8
- to make the underlying storage as agnostic as possible, we use ` simplekv
9
- <http://pythonhosted.org/simplekv/> `_ to provide assess to a variety of backends.
10
-
11
- In production, it is important to use a backend that can have some sort of
12
- persistent storage, so we don't 'forget' that we revoked a token if the flask
13
- process is restarted. We also need something that can be safely used by the
14
- multiple thread and processes running your application. At present we believe
15
- redis is a good fit for this. It has the added benefit of removing expired tokens
16
- from the store automatically, so it wont blow up into something huge .
17
-
18
- We also have to choose what tokens we want to check against the blacklist. We could
19
- check all tokens (refresh and access), or only the refresh tokens. There are pros
20
- and cons to either way, namely extra overhead on jwt_required endpoints vs someone
21
- being able to use an access token freely until it expires. In this example, we are
22
- looking at all tokens:
6
+
7
+ You will have to choose what tokens you want to check against the blacklist . In
8
+ most cases, you will probably want to check both refresh and access tokens, which
9
+ is the default behavior. However, if the extra overhead of checking tokens is a
10
+ concern you could instead only check the refresh tokens, and set the access
11
+ tokens to have a short expires time so any damage a compromised token could
12
+ cause is minimal.
13
+
14
+ Blacklisting works by is providing a callback function to this extension, using the
15
+ ** @jwt.token_in_blacklist_loader ** decorator. This method will be called whenever the
16
+ specified tokens (`` 'access' `` and/or `` 'refresh' ``) are used to access a protected endpoint .
17
+ If the callback function says that the token is revoked, we will not allow the
18
+ call to continue, otherwise we will allow the call to access the endpoint as normal.
19
+
20
+
21
+ Here is a basic example of this in action.
22
+
23
23
24
24
.. literalinclude :: ../examples/blacklist.py
25
25
26
- If you want better performance (ie, not having to check the blacklist store
27
- with every request), you could check only the refresh tokens. This makes it
28
- so any call to a jwt_required endpoint does not need to check the blacklist
29
- store, but on the flip side would allow a compromised access token to be used
30
- until it expired. If using the approach, you should set the access tokens to
31
- have a very short lifetime to help combat this.
32
-
33
- It's worth noting that if your selected backend support the `time to live mixin
34
- <http://pythonhosted.org/simplekv/#simplekv.TimeToLiveMixin> `_ (such as redis),
35
- keys will be automatically deleted from the store at some point after they have
36
- expired. This prevents your store from blowing up with old keys without you having
37
- to do any work to prune it back down.
26
+ In production, you will likely want to use either a database or in memory store
27
+ (such as redis) to store your tokens. In memory stores are great if you are wanting
28
+ to revoke a token when the users logs out, as they are blazing fast. A downside
29
+ to using redis is that in the case of a power outage or other such event, it's
30
+ possible that you might 'forget' that some tokens have been revoked, depending
31
+ on if the redis data was synced to disk.
32
+
33
+ In contrast to that, databases are great if the data persistance is of the highest
34
+ importance (for example, if you have very long lived tokens that other developers
35
+ use to access your api), or if you want to add some addition features like showing
36
+ users all of their active tokens, and letting them revoke and unrevoke those tokens.
37
+
38
+ For more in depth examples of these, check out:
39
+
40
+ - https://github.com/vimalloc/flask-jwt-extended/examples/redis_blacklist.py
41
+ - https://github.com/vimalloc/flask-jwt-extended/examples/database_blacklist
0 commit comments