Skip to content

Commit 2d8cbc0

Browse files
committed
Fix JWT: don't provide token to inactive user
1 parent bbe7350 commit 2d8cbc0

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/bemserver_api/extensions/smorest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,6 @@ class GetJWTRespSchema(Schema):
196196
def get_token(creds):
197197
"""Get an authentication token"""
198198
user = auth.get_user_by_email(creds["email"])
199-
if user is None or not user.check_password(creds["password"]):
199+
if user is None or not user.check_password(creds["password"]) or not user.is_active:
200200
return flask.jsonify({"status": "failure"})
201201
return {"status": "success", "token": auth.encode(user).decode("utf-8")}

tests/extensions/test_smorest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class TestSmorest:
55
def test_get_token(self, app, users):
66
user_1 = users["Active"]["user"]
7+
user_2 = users["Inactive"]["user"]
78

89
client = app.test_client()
910
payload = {"email": user_1.email, "password": "@ctive"}
@@ -12,6 +13,13 @@ def test_get_token(self, app, users):
1213
assert resp.json["status"] == "success"
1314
assert "token" in resp.json
1415

16+
# Inactive user
17+
client = app.test_client()
18+
payload = {"email": user_2.email, "password": "in@ctive"}
19+
resp = client.post("/auth/token", json=payload)
20+
assert resp.status_code == 200
21+
assert resp.json == {"status": "failure"}
22+
1523
# Wrong password
1624
client = app.test_client()
1725
payload = {"email": user_1.email, "password": "dummy"}

0 commit comments

Comments
 (0)