Skip to content

Commit 555e36c

Browse files
committed
Fixes to get token revoke working
1 parent 444715a commit 555e36c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

flask_jwt_extended/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ def revoke_jwt(jti):
136136

137137
if revoke:
138138
revoke_token(jti)
139+
return jsonify({"msg": "Token successfully revoked"})
139140
else:
140141
unrevoke_token(jti)
142+
return jsonify({"msg": "Token successfully unrevoked"})
141143

142144

143145
# Endpoint for generating a non-fresh access token from the refresh token

flask_jwt_extended/utils.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,15 @@ def _check_blacklist(token):
190190

191191
# Only check access tokens if BLACKLIST_TOKEN_CHECKS is set to 'all`
192192
if token_type == 'access' and _blacklist_checks() == 'all':
193-
stored_data = json.loads(store.get(jti))
194-
if stored_data['revoked'] != 'active':
193+
194+
stored_data = json.loads(store.get(jti).decode('utf-8'))
195+
if stored_data['revoked']:
195196
raise RevokedTokenError('Token has been revoked')
196197

197198
# Always check refresh tokens
198199
if token_type == 'refresh':
199-
stored_data = json.loads(store.get(jti))
200-
if stored_data['revoked'] != 'active':
200+
stored_data = json.loads(store.get(jti).decode('utf-8'))
201+
if stored_data['revoked']:
201202
raise RevokedTokenError('Token has been revoked')
202203

203204

@@ -330,7 +331,7 @@ def get_stored_tokens():
330331
raise RuntimeError("Blacklist must be enabled to list tokens")
331332

332333
store = _get_blacklist_store()
333-
return [json.loads(store.get(jti)) for jti in store.iter_keys()]
334+
return [json.loads(store.get(jti).decode('utf-8')) for jti in store.iter_keys()]
334335

335336

336337
def _update_token(jti, revoked):
@@ -339,15 +340,16 @@ def _update_token(jti, revoked):
339340

340341
store = _get_blacklist_store()
341342
try:
342-
token = store.get(jti)
343+
stored_data = json.loads(store.get(jti).decode('utf-8'))
344+
token = stored_data['token']
343345
_store_token(token, revoked)
344346
except KeyError:
345347
# Token does not exist in the store. Could have been automatically
346348
# removed from the store via ttl expiring # (in case of redis or
347349
# memcached), or could have never been in the store, which probably
348350
# indicates a bug in the callers code.
349351
# TODO should this raise an error? Or silently return?
350-
return
352+
raise
351353

352354

353355
def revoke_token(jti):
@@ -366,7 +368,7 @@ def _get_secret_key():
366368

367369

368370
def _blacklist_enabled():
369-
return current_app.config.get('JWT_BLACKLIST', BLACKLIST_ENABLED)
371+
return current_app.config.get('JWT_BLACKLIST_ENABLED', BLACKLIST_ENABLED)
370372

371373

372374
def _get_blacklist_store():
@@ -417,13 +419,13 @@ def _store_token(token, revoked):
417419
'token': token,
418420
'last_used': _utc_datetime_to_ts(datetime.datetime.utcnow()),
419421
'revoked': revoked
420-
})
422+
}).encode('utf-8')
421423

422424
store = _get_blacklist_store()
423425
if _store_supports_ttl(store):
424426
# Add 15 minutes to token ttl to account for possible time drift
425427
ttl = _get_token_ttl(token) + datetime.timedelta(minutes=15)
426428
ttl_secs = ttl.total_seconds()
427-
store.put(key=token['jti'], value=data_to_store, ttl_secs=ttl_secs)
429+
store.put(token['jti'], data_to_store, ttl_secs=ttl_secs)
428430
else:
429-
store.put(key=token['jti'], value=data_to_store)
431+
store.put(token['jti'], data_to_store)

0 commit comments

Comments
 (0)