|
21 | 21 | app.config['JWT_BLACKLIST_ENABLED'] = True |
22 | 22 | app.config['JWT_BLACKLIST_STORE'] = simplekv.memory.DictStore() |
23 | 23 |
|
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) |
29 | 29 |
|
30 | 30 | jwt = JWTManager(app) |
31 | 31 |
|
@@ -56,23 +56,42 @@ def refresh(): |
56 | 56 | return jsonify(ret), 200 |
57 | 57 |
|
58 | 58 |
|
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 |
61 | 68 | @app.route('/logout', methods=['POST']) |
62 | 69 | @jwt_required |
63 | 70 | def logout(): |
64 | | - jwt = get_raw_jwt() |
65 | | - jti = jwt['jti'] |
66 | 71 | 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() |
68 | 86 | except KeyError: |
69 | 87 | return jsonify({ |
70 | | - 'msg': 'Requires access tokens to be blacklisted' |
| 88 | + 'msg': 'Refresh token not found in the blacklist store' |
71 | 89 | }), 500 |
72 | 90 | return jsonify({"msg": "Successfully logged out"}), 200 |
73 | 91 |
|
74 | 92 |
|
75 | 93 | # Endpoint for listing tokens that have the same identity as you |
| 94 | +# NOTE: This is currently very inefficient. |
76 | 95 | @app.route('/auth/tokens', methods=['GET']) |
77 | 96 | @jwt_required |
78 | 97 | def list_identity_tokens(): |
|
0 commit comments