Skip to content

Commit f458460

Browse files
committed
Update blacklist documentation
1 parent 19094aa commit f458460

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

docs/blacklist_and_token_revoking.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ from the store automatically, so it wont blow up into something huge.
1717

1818
We also have to choose what tokens we want to check against the blacklist. We could
1919
check all tokens (refresh and access), or only the refresh tokens. There are pros
20-
and cons to either way (extra overhead on jwt_required endpoints vs someone being
21-
able to use an access token freely until it expires). In this example, we are going
22-
to only check refresh tokens, and set the access tokes to a small expires time to
23-
help minimize damage that could be done with a stolen access token.
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:
2423

2524
.. literalinclude:: ../examples/blacklist.py
2625

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+
2733
It's worth noting that if your selected backend support the `time to live mixin
2834
<http://pythonhosted.org/simplekv/#simplekv.TimeToLiveMixin>`_ (such as redis),
2935
keys will be automatically deleted from the store at some point after they have

examples/blacklist.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
app.config['JWT_BLACKLIST_ENABLED'] = True
2222
app.config['JWT_BLACKLIST_STORE'] = simplekv.memory.DictStore()
2323

24-
# Only check the refresh token for being revoked, and set a small time to
25-
# live on the access tokens to prevent a compromised one from being used
26-
# for a long period of time
27-
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'refresh'
28-
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = datetime.timedelta(minutes=3)
24+
# Check all tokens (access and refresh) to see if they have been revoked.
25+
# You can alternately check only the refresh tokens here, by setting this
26+
# to 'refresh' instead of 'all'
27+
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'all'
28+
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = datetime.timedelta(minutes=5)
2929

3030
jwt = JWTManager(app)
3131

@@ -56,23 +56,42 @@ def refresh():
5656
return jsonify(ret), 200
5757

5858

59-
# Endpoint for revoking an access token when logging out.
60-
# Please make sure JWT_BLACKLIST_TOKEN_CHECKS is set to 'all'
59+
# Helper method to revoke the current token used to access
60+
# a protected endpoint
61+
def _revoke_current_token():
62+
current_token = get_raw_jwt()
63+
jti = current_token['jti']
64+
revoke_token(jti)
65+
66+
67+
# Endpoint for revoking the current users access token
6168
@app.route('/logout', methods=['POST'])
6269
@jwt_required
6370
def logout():
64-
jwt = get_raw_jwt()
65-
jti = jwt['jti']
6671
try:
67-
revoke_token(jti)
72+
_revoke_current_token()
73+
except KeyError:
74+
return jsonify({
75+
'msg': 'Access token not found in the blacklist store'
76+
}), 500
77+
return jsonify({"msg": "Successfully logged out"}), 200
78+
79+
80+
# Endpoint for revoking the current users refresh token
81+
@app.route('/logout2', methods=['POST'])
82+
@jwt_refresh_token_required
83+
def logout2():
84+
try:
85+
_revoke_current_token()
6886
except KeyError:
6987
return jsonify({
70-
'msg': 'Requires access tokens to be blacklisted'
88+
'msg': 'Refresh token not found in the blacklist store'
7189
}), 500
7290
return jsonify({"msg": "Successfully logged out"}), 200
7391

7492

7593
# Endpoint for listing tokens that have the same identity as you
94+
# NOTE: This is currently very inefficient.
7695
@app.route('/auth/tokens', methods=['GET'])
7796
@jwt_required
7897
def list_identity_tokens():

0 commit comments

Comments
 (0)