Skip to content

Commit e46eb46

Browse files
committed
Add user_refresh() : refresh valid access token
1 parent 263b111 commit e46eb46

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/server/api/user_api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,38 @@ def user_logout():
143143
return jsonify("Logged out " + username)
144144

145145

146+
# Generate a new access token
147+
148+
@user_api.route("/api/user/refresh", methods=["GET"])
149+
@jwt_ops.jwt_required()
150+
def user_refresh():
151+
""" If user still active, send back an access_token with a new expiration stamp """
152+
old_jwt = jwt_ops.validate_decode_jwt()
153+
154+
# If token bad, should be handled & error message sent by jwt_required() and we won't get here
155+
if old_jwt:
156+
user_name = old_jwt['sub']
157+
with engine.connect() as connection:
158+
159+
s = text( """select active from pdp_users where username=:u """ )
160+
s = s.bindparams(u=user_name)
161+
result = connection.execute(s)
162+
163+
if result.rowcount: # Did we get a match on username?
164+
is_active = result.fetchone()
165+
else:
166+
log_user_action(user_name, "Failure", "Valid JWT presented for refesh attempt on unknown username")
167+
return jsonify("Bad credentials"), 401
168+
169+
if is_active[0].lower() == 'y': # In the user DB and still Active?
170+
token = jwt_ops.create_token(user_name,old_jwt['role'])
171+
return token
172+
173+
else:
174+
return jsonify("Bad credentials"), 401
175+
176+
177+
146178
### Unexpired *Admin* JWT required ############################
147179

148180

0 commit comments

Comments
 (0)