Skip to content

Commit 04d6c86

Browse files
committed
Refactoring
1 parent d37bd55 commit 04d6c86

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

flask_jwt_extended/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .jwt_manager import JWTManager
22
from .utils import (jwt_identity, jwt_user_claims, jwt_required, fresh_jwt_required,
3-
authenticate, refresh, fresh_authenticate)
3+
create_refresh_access_tokens, refresh_access_token, create_fresh_access_token)

flask_jwt_extended/app.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from flask import Flask, request, jsonify
66

77
from flask_jwt_extended import JWTManager, jwt_required, fresh_jwt_required,\
8-
authenticate, fresh_authenticate, refresh, jwt_identity, jwt_user_claims
8+
create_refresh_access_tokens, create_fresh_access_token, refresh_access_token, jwt_identity, jwt_user_claims
99

1010
# Example users database
1111
USERS = {
@@ -27,16 +27,20 @@
2727
app.secret_key = 'super-secret'
2828

2929
# Enable JWT blacklist / token revoke
30-
#
30+
app.config['JWT_BLACKLIST_ENABLED'] = True
31+
3132
# We are going to be using a simple in memory blacklist for this example. In
3233
# production, you will likely prefer something like redis (it can work with
3334
# multiple threads and processes, and supports automatic removal of expired
3435
# tokens so the blacklist doesn't blow up). Check here for available options:
3536
# http://pythonhosted.org/simplekv/
3637
blacklist_store = simplekv.memory.DictStore()
37-
app.config['JWT_BLACKLIST_ENABLED'] = True
3838
app.config['JWT_BLACKLIST_STORE'] = blacklist_store
39-
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'refresh' # only check blacklist for refresh tokens
39+
40+
# Only check the blacklist for refresh token. Available options are:
41+
# 'all': Check blacklist for access and refresh tokens
42+
# 'refresh': Check blacklist only for refresh tokens
43+
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'refresh'
4044

4145
# Optional configuration options
4246
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=1) # defaults to 15 minutes
@@ -90,7 +94,7 @@ def login():
9094
if USERS[username]['password'] != password:
9195
return jsonify({"msg": "Bad username or password"}), 401
9296

93-
return authenticate(identity=username)
97+
return create_refresh_access_tokens(identity=username)
9498

9599

96100
# Endpoint for getting a fresh access token for a user
@@ -106,13 +110,13 @@ def fresh_login():
106110
if USERS[username]['password'] != password:
107111
return jsonify({"msg": "Bad username or password"}), 401
108112

109-
return fresh_authenticate(identity=username)
113+
return create_fresh_access_token(identity=username)
110114

111115

112116
# Endpoint for generating a non-fresh access token from the refresh token
113117
@app.route('/refresh', methods=['POST'])
114118
def refresh_token():
115-
return refresh()
119+
return refresh_access_token()
116120

117121

118122
@app.route('/protected', methods=['GET'])

flask_jwt_extended/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@
2121
# See: http://pythonhosted.org/simplekv/index.html#simplekv.TimeToLiveMixin
2222
BLACKLIST_STORE = None
2323

24-
# blacklist check requests. Possible values are all, refresh, and None
25-
# TODO when accessing this value in app.config, make sure it is one of the expected values
26-
BLACKLIST_TOKEN_CHECKS = None
24+
# blacklist check requests. Possible values are all and refresh
25+
BLACKLIST_TOKEN_CHECKS = 'refresh'

flask_jwt_extended/utils.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ def _check_blacklist(jwt_data):
188188
token_type = jwt_data['type']
189189
jti = jwt_data['jti']
190190

191+
# Only check access tokens if BLACKLIST_TOKEN_CHECKS is set to 'all`
191192
if token_type == 'access' and _blacklist_checks() == 'all':
192193
token_status = store[jti]
193194
if token_status != 'active':
194195
raise RevokedTokenError('{} has been revoked'.format)
195196

196-
if token_type == 'refresh' and _blacklist_checks() in ('all', 'refresh'):
197+
# Always check refresh tokens
198+
if token_type == 'refresh':
197199
token_status = store[jti]
198200
if token_status != 'active':
199201
raise RevokedTokenError('{} has been revoked'.format)
@@ -264,7 +266,7 @@ def wrapper(*args, **kwargs):
264266
return wrapper
265267

266268

267-
def authenticate(identity):
269+
def create_refresh_access_tokens(identity):
268270
# Token settings
269271
config = current_app.config
270272
access_expire_delta = config.get('JWT_ACCESS_TOKEN_EXPIRES', ACCESS_EXPIRES)
@@ -285,8 +287,21 @@ def authenticate(identity):
285287
return jsonify(ret), 200
286288

287289

290+
def create_fresh_access_token(identity):
291+
# Token options
292+
secret = _get_secret_key()
293+
config = current_app.config
294+
access_expire_delta = config.get('JWT_ACCESS_TOKEN_EXPIRES', ACCESS_EXPIRES)
295+
algorithm = config.get('JWT_ALGORITHM', ALGORITHM)
296+
user_claims = current_app.jwt_manager.user_claims_callback(identity)
297+
access_token = _encode_access_token(identity, secret, algorithm, access_expire_delta,
298+
fresh=True, user_claims=user_claims)
299+
ret = {'access_token': access_token}
300+
return jsonify(ret), 200
301+
302+
288303
@_handle_callbacks_on_error
289-
def refresh():
304+
def refresh_access_token():
290305
# Get the JWT
291306
jwt_data = _decode_jwt_from_request()
292307

@@ -310,19 +325,6 @@ def refresh():
310325
return jsonify(ret), 200
311326

312327

313-
def fresh_authenticate(identity):
314-
# Token options
315-
secret = _get_secret_key()
316-
config = current_app.config
317-
access_expire_delta = config.get('JWT_ACCESS_TOKEN_EXPIRES', ACCESS_EXPIRES)
318-
algorithm = config.get('JWT_ALGORITHM', ALGORITHM)
319-
user_claims = current_app.jwt_manager.user_claims_callback(identity)
320-
access_token = _encode_access_token(identity, secret, algorithm, access_expire_delta,
321-
fresh=True, user_claims=user_claims)
322-
ret = {'access_token': access_token}
323-
return jsonify(ret), 200
324-
325-
326328
def _get_secret_key():
327329
key = current_app.config.get('SECRET_KEY', None)
328330
if not key:
@@ -339,7 +341,11 @@ def _get_blacklist_store():
339341

340342

341343
def _blacklist_checks():
342-
return current_app.config.get('JWT_BLACKLIST_TOKEN_CHECKS', BLACKLIST_TOKEN_CHECKS)
344+
config = current_app.config
345+
check_type = config.get('JWT_BLACKLIST_TOKEN_CHECKS', BLACKLIST_TOKEN_CHECKS)
346+
if check_type not in ('all', 'refresh'):
347+
raise RuntimeError('Invalid option for JWT_BLACKLIST_TOKEN_CHECKS')
348+
return check_type
343349

344350

345351
def _store_supports_ttl(store):

0 commit comments

Comments
 (0)