55from flask import Flask , request , jsonify
66
77from 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
1112USERS = {
2627app .debug = True
2728app .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
3036app .config ['JWT_BLACKLIST_ENABLED' ] = True
3137
4248# 'refresh': Check blacklist only for refresh tokens
4349app .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-
5051jwt = 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
5556def 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' ])
8687def 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' ])
102103def 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' ])
118119def refresh_token ():
119120 return refresh_access_token ()
120121
121122
122123@app .route ('/protected' , methods = ['GET' ])
123124@jwt_required
124125def 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
132135def 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
137142if __name__ == '__main__' :
0 commit comments