|
7 | 7 | from flask_jwt_extended import JWTManager, jwt_required, \ |
8 | 8 | get_jwt_identity, revoke_token, unrevoke_token, \ |
9 | 9 | get_stored_tokens, get_all_stored_tokens, create_access_token, \ |
10 | | - create_refresh_token, jwt_refresh_token_required |
| 10 | + create_refresh_token, jwt_refresh_token_required, get_stored_token |
11 | 11 |
|
12 | 12 | # Setup flask |
13 | 13 | app = Flask(__name__) |
|
18 | 18 |
|
19 | 19 | # Enable and configure the JWT blacklist / token revoke. We are using an in |
20 | 20 | # memory store for this example. In production, you should use something |
21 | | -# else (csuch as redis, memcached, sqlalchemy). See here for options: |
| 21 | +# persistant (such as redis, memcached, sqlalchemy). See here for options: |
22 | 22 | # http://pythonhosted.org/simplekv/ |
23 | 23 | app.config['JWT_BLACKLIST_ENABLED'] = True |
24 | 24 | app.config['JWT_BLACKLIST_STORE'] = simplekv.memory.DictStore() |
@@ -60,33 +60,40 @@ def list_identity_tokens(): |
60 | 60 |
|
61 | 61 |
|
62 | 62 | # Endpoint for listing all tokens. In your app, you should either not expose |
63 | | -# this, or put some addition security on top of it so only trusted users, |
| 63 | +# this endpoint, or put some addition security on top of it so only trusted users, |
64 | 64 | # (administrators, etc) can access it |
65 | 65 | @app.route('/auth/all-tokens') |
66 | 66 | def list_all_tokens(): |
67 | 67 | return jsonify(get_all_stored_tokens()), 200 |
68 | 68 |
|
69 | 69 |
|
70 | | -# Endpoint for revoking a token |
| 70 | +# Endpoint for allowing users to revoke their tokens |
71 | 71 | @app.route('/auth/tokens/revoke/<string:jti>', methods=['PUT']) |
72 | 72 | @jwt_required |
73 | 73 | def change_jwt_revoke_state(jti): |
| 74 | + username = get_jwt_identity() |
74 | 75 | try: |
| 76 | + token_data = get_stored_token(jti) |
| 77 | + if token_data['token']['identity'] != username: |
| 78 | + raise KeyError |
75 | 79 | revoke_token(jti) |
76 | 80 | return jsonify({"msg": "Token successfully revoked"}), 200 |
77 | 81 | except KeyError: |
78 | | - return jsonify({'msg': 'Token not foun'}), 404 |
| 82 | + return jsonify({'msg': 'Token not found'}), 404 |
79 | 83 |
|
80 | 84 |
|
81 | | -# Endpoint for un-revoking a token |
82 | 85 | @app.route('/auth/tokens/unrevoke/<string:jti>', methods=['PUT']) |
83 | 86 | @jwt_required |
84 | | -def change_jwt_revoke_state(jti): |
| 87 | +def change_jwt_unrevoke_state(jti): |
| 88 | + username = get_jwt_identity() |
85 | 89 | try: |
| 90 | + token_data = get_stored_token(jti) |
| 91 | + if token_data['token']['identity'] != username: |
| 92 | + raise KeyError |
86 | 93 | unrevoke_token(jti) |
87 | 94 | return jsonify({"msg": "Token successfully unrevoked"}), 200 |
88 | 95 | except KeyError: |
89 | | - return jsonify({'msg': 'Token not foun'}), 404 |
| 96 | + return jsonify({'msg': 'Token not found'}), 404 |
90 | 97 |
|
91 | 98 |
|
92 | 99 | @app.route('/protected', methods=['GET']) |
|
0 commit comments