Skip to content

Commit f4bb7b6

Browse files
committed
More refactoring and cleanup example app
1 parent 04d6c86 commit f4bb7b6

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
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
2-
from .utils import (jwt_identity, jwt_user_claims, jwt_required, fresh_jwt_required,
2+
from .utils import (jwt_identity, jwt_claims, jwt_required, fresh_jwt_required,
33
create_refresh_access_tokens, refresh_access_token, create_fresh_access_token)

flask_jwt_extended/app.py

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

77
from flask_jwt_extended import JWTManager, jwt_required, fresh_jwt_required,\
8-
create_refresh_access_tokens, create_fresh_access_token, refresh_access_token, jwt_identity, jwt_user_claims
8+
create_refresh_access_tokens, create_fresh_access_token, refresh_access_token,\
9+
jwt_identity, jwt_claims
910

1011
# Example users database
1112
USERS = {
@@ -26,6 +27,11 @@
2627
app.debug = True
2728
app.secret_key = 'super-secret'
2829

30+
# Optional configuration options for flask_jwt_extended
31+
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=1) # defaults to 15 minutes
32+
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = timedelta(days=7) # defaults to 30 days
33+
app.config['JWT_ALGORITHM'] = 'HS512' # Default to HS256
34+
2935
# Enable JWT blacklist / token revoke
3036
app.config['JWT_BLACKLIST_ENABLED'] = True
3137

@@ -42,15 +48,10 @@
4248
# 'refresh': Check blacklist only for refresh tokens
4349
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'refresh'
4450

45-
# Optional configuration options
46-
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=1) # defaults to 15 minutes
47-
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = timedelta(days=7) # defaults to 30 days
48-
app.config['JWT_ALGORITHM'] = 'HS512' # Default to HS256
49-
5051
jwt = JWTManager(app)
5152

5253

53-
# Function to add custom claims to the JWT (optional)
54+
# Function to add custom claims to the JWT (optional).
5455
@jwt.user_claims_loader
5556
def my_claims(identity):
5657
return {
@@ -82,7 +83,7 @@ def my_expired_response():
8283

8384

8485
# Endpoint for authing a user
85-
@app.route('/auth', methods=['POST'])
86+
@app.route('/auth/login', methods=['POST'])
8687
def login():
8788
username = request.json.get('username', None)
8889
password = request.json.get('password', None)
@@ -98,7 +99,7 @@ def login():
9899

99100

100101
# Endpoint for getting a fresh access token for a user
101-
@app.route('/fresh-auth', methods=['POST'])
102+
@app.route('/auth/fresh-login', methods=['POST'])
102103
def fresh_login():
103104
username = request.json.get('username', None)
104105
password = request.json.get('username', None)
@@ -114,24 +115,28 @@ def fresh_login():
114115

115116

116117
# Endpoint for generating a non-fresh access token from the refresh token
117-
@app.route('/refresh', methods=['POST'])
118+
@app.route('/auth/refresh', methods=['POST'])
118119
def refresh_token():
119120
return refresh_access_token()
120121

121122

122123
@app.route('/protected', methods=['GET'])
123124
@jwt_required
124125
def non_fresh_protected():
125-
ip = jwt_user_claims['ip']
126-
msg = '{} says hello from {}'.format(jwt_identity, ip)
126+
ip = jwt_claims['ip'] # Access data stored in custom claims on the JWT
127+
username = jwt_identity # Access identity through jwt_identity proxy
128+
129+
msg = '{} says hello from {}'.format(username, ip)
127130
return jsonify({'msg': msg})
128131

129132

130133
@app.route('/protected-fresh', methods=['GET'])
131134
@fresh_jwt_required
132135
def fresh_protected():
133-
ip = jwt_user_claims['ip']
134-
msg = '{} says hello from {} (fresh)'.format(jwt_identity, ip)
136+
ip = jwt_claims['ip'] # Access data stored in custom claims on the JWT
137+
username = jwt_identity # Access identity through jwt_identity proxy
138+
139+
msg = '{} says hello from {} (fresh)'.format(username, ip)
135140
return jsonify({'msg': msg})
136141

137142
if __name__ == '__main__':

flask_jwt_extended/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from werkzeug.local import LocalProxy
88
from flask import request, jsonify, current_app
99
try:
10-
# see: http://flask.pocoo.org/docs/0.11/extensiondev/
1110
from flask import _app_ctx_stack as ctx_stack
1211
except ImportError:
1312
from flask import _request_ctx_stack as ctx_stack
@@ -22,7 +21,7 @@
2221
jwt_identity = LocalProxy(lambda: _get_identity())
2322

2423
# Proxy for getting the dictionary of custom user claims in this JWT
25-
jwt_user_claims = LocalProxy(lambda: _get_user_claims())
24+
jwt_claims = LocalProxy(lambda: _get_user_claims())
2625

2726

2827
def _get_identity():

0 commit comments

Comments
 (0)