Skip to content

Commit d37bd55

Browse files
committed
Refactoring of blacklist stuff
1 parent c4ecf4f commit d37bd55

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

flask_jwt_extended/utils.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _encode_access_token(identity, secret, algorithm, token_expire_delta,
7878
'user_claims': user_claims,
7979
}
8080
encoded_token = jwt.encode(token_data, secret, algorithm).decode('utf-8')
81-
_store_token_if_blacklist_enabled(uid, token_expire_delta, token_type='access')
81+
_store_token_if_blacklist_enabled(token_data)
8282
return encoded_token
8383

8484

@@ -103,7 +103,7 @@ def _encode_refresh_token(identity, secret, algorithm, token_expire_delta):
103103
'type': 'refresh',
104104
}
105105
encoded_token = jwt.encode(token_data, secret, algorithm).decode('utf-8')
106-
_store_token_if_blacklist_enabled(uid, token_expire_delta, token_type='refresh')
106+
_store_token_if_blacklist_enabled(token_data)
107107
return encoded_token
108108

109109

@@ -346,20 +346,28 @@ def _store_supports_ttl(store):
346346
return getattr(store, 'ttl_support', False)
347347

348348

349-
def _store_token_if_blacklist_enabled(jti, token_expire_delta, token_type):
349+
def _store_token_if_blacklist_enabled(token):
350350
# If the blacklist isn't enabled, do nothing
351-
if not _blacklist_enabled():
351+
if not _blacklist_enabled() or _blacklist_checks() is None:
352352
return
353353

354354
# If configured to only check refresh tokens and this isn't a refresh token, return
355-
if _blacklist_checks() == 'refresh' and token_type != 'refresh':
355+
if _blacklist_checks() == 'refresh' and token['type'] != 'refresh':
356356
return
357357

358+
# TODO store data as json in the store (including jti, identity, and user claims)
359+
358360
# Otherwise store the token in the blacklist (with current status of active)
359361
store = _get_blacklist_store()
360362
if _store_supports_ttl(store):
361-
ttl = token_expire_delta + datetime.timedelta(minutes=15)
363+
config = current_app.config
364+
if token['type'] == 'access':
365+
expire_delta = config.get('JWT_ACCESS_TOKEN_EXPIRES', ACCESS_EXPIRES)
366+
else:
367+
expire_delta = config.get('JWT_REFRESH_TOKEN_EXPIRES', REFRESH_EXPIRES)
368+
369+
ttl = expire_delta + datetime.timedelta(minutes=15)
362370
ttl_secs = ttl.total_seconds()
363-
store.put(key=jti, value="active", ttl_secs=ttl_secs)
371+
store.put(key=token['jti'], value="active", ttl_secs=ttl_secs)
364372
else:
365-
store.put(key=jti, value="active")
373+
store.put(key=token['jti'], value="active")

0 commit comments

Comments
 (0)