Skip to content

Commit 7c18a81

Browse files
committed
Bug: Valid Refresh Tokens despite user changing password
Fixes anitab-org#903
1 parent 27c0337 commit 7c18a81

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

app/api/resources/user.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,13 @@ def post(cls):
408408
The return value is an access token and the expiry timestamp.
409409
The token is valid for 1 week.
410410
"""
411-
user_id = get_jwt_identity()
412-
access_token = create_access_token(identity=user_id)
411+
req_user = get_jwt_identity()
412+
user = DAO.get_user(req_user["id"])
413+
414+
if user.password_hash != req_user["password"]:
415+
return messages.TOKEN_IS_INVALID, HTTPStatus.UNAUTHORIZED
416+
417+
access_token = create_access_token(identity=req_user["id"])
413418

414419
from run import application
415420

@@ -472,7 +477,9 @@ def post(cls):
472477
)
473478

474479
access_token = create_access_token(identity=user.id)
475-
refresh_token = create_refresh_token(identity=user.id)
480+
refresh_token = create_refresh_token(
481+
identity={"id": user.id, "password": user.password_hash}
482+
)
476483

477484
from run import application
478485

tests/users/test_api_refresh.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def setUp(self):
3131

3232
def test_user_refresh(self):
3333
with self.client:
34-
refresh_header = get_test_request_header(user1["username"], refresh=True)
34+
refresh_header = get_test_request_header(
35+
{"id": self.first_user.id, "password": self.first_user.password_hash},
36+
refresh=True,
37+
)
3538
response = self.client.post(
3639
"/refresh",
3740
headers=refresh_header,
@@ -83,6 +86,22 @@ def test_user_refresh_expired_token(self):
8386
self.assertEqual(401, actual_response.status_code)
8487
self.assertEqual(expected_response, json.loads(actual_response.data))
8588

89+
def test_user_refresh_reset_password(self):
90+
refresh_header = get_test_request_header(
91+
{"id": self.first_user.id, "password": "new_password_hash"},
92+
refresh=True,
93+
)
94+
expected_response = messages.TOKEN_IS_INVALID
95+
actual_response = self.client.post(
96+
"/refresh",
97+
follow_redirects=True,
98+
headers=refresh_header,
99+
content_type="application/json",
100+
)
101+
102+
self.assertEqual(401, actual_response.status_code)
103+
self.assertEqual(expected_response, json.loads(actual_response.data))
104+
86105

87106
if __name__ == "__main__":
88107
unittest.main()

0 commit comments

Comments
 (0)