Skip to content

Commit d985e26

Browse files
authored
Merge pull request #496 from Robin-Van-de-Merghel/robin-fix-read_token
[HotFix]: Bad error handling can lead to a 500 error
2 parents 7de010a + 0799189 commit d985e26

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

diracx-logic/src/diracx/logic/auth/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import httpx
99
from authlib.integrations.starlette_client import OAuthError
1010
from authlib.jose import JsonWebKey, JsonWebToken
11-
from authlib.jose.errors import DecodeError
1211
from authlib.oidc.core import IDToken
1312
from cachetools import TTLCache
1413
from cryptography.fernet import Fernet
@@ -195,13 +194,10 @@ def read_token(
195194
) -> dict:
196195
# First transform it into bytes, then return a jwt object
197196
# Don't take settings as a parameter to allow claims_options to be None or something else
198-
try:
199-
encoded_payload = payload.encode("ascii")
200-
jwt = JsonWebToken(token_algorithm)
201-
decoded_jwt = jwt.decode(encoded_payload, key, claims_options=claims_options)
202-
decoded_jwt.validate()
203-
except DecodeError as e:
204-
raise ValueError("wrong json payload") from e
197+
encoded_payload = payload.encode("ascii")
198+
jwt = JsonWebToken(token_algorithm)
199+
decoded_jwt = jwt.decode(encoded_payload, key, claims_options=claims_options)
200+
decoded_jwt.validate()
205201
return decoded_jwt
206202

207203

diracx-routers/src/diracx/routers/auth/management.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
from typing import Annotated, Any
1111

12+
from authlib.jose import JoseError
1213
from fastapi import Depends, HTTPException, status
1314
from typing_extensions import TypedDict
1415
from uuid_utils import UUID
@@ -79,7 +80,7 @@ async def revoke_refresh_token_by_refresh_token(
7980
await revoke_refresh_token_by_refresh_token_bl(
8081
auth_db, None, refresh_token, settings
8182
)
82-
except ValueError:
83+
except JoseError:
8384
logger.warning("Someone tried to revoke its token but failed.")
8485

8586
return "Refresh token revoked"

diracx-routers/tests/auth/test_standard.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,20 @@ async def test_refresh_token_invalid(test_client, auth_httpx_mock: HTTPXMock):
685685
assert data["detail"] == "Invalid JWT: bad_signature: "
686686

687687

688+
async def test_bad_access_token(test_client):
689+
"""Test accessing a resource with a bad token."""
690+
# From https://github.com/DIRACGrid/diracx/pull/496
691+
r = test_client.get(
692+
"/api/auth/userinfo", headers={"Authorization": "Bearer thisisabadbearer"}
693+
)
694+
data = r.json()
695+
696+
# Should raise a 401, with "Invalid JWT"
697+
# Not Invalid Authorization Header because raised when decoding the token
698+
assert r.status_code == 401, data
699+
assert data["detail"] == "Invalid JWT"
700+
701+
688702
async def test_list_refresh_tokens(test_client, auth_httpx_mock: HTTPXMock):
689703
"""Test the refresh token listing with 2 users, a normal one and token manager:
690704
- normal user gets a refresh token and lists it
@@ -875,7 +889,6 @@ async def test_revoke_refresh_token_classic(test_client, auth_httpx_mock: HTTPXM
875889
"refresh_token": "does-not-exist",
876890
"client_id": DIRAC_CLIENT_ID,
877891
},
878-
headers={"Authorization": f"Bearer {normal_user_access_token}"},
879892
)
880893
assert r.status_code == 200
881894

@@ -886,7 +899,6 @@ async def test_revoke_refresh_token_classic(test_client, auth_httpx_mock: HTTPXM
886899
"refresh_token": normal_user_refresh_token,
887900
"client_id": DIRAC_CLIENT_ID,
888901
},
889-
headers={"Authorization": f"Bearer {normal_user_access_token}"},
890902
)
891903
assert r.status_code == 200
892904

@@ -905,7 +917,6 @@ async def test_revoke_refresh_token_classic(test_client, auth_httpx_mock: HTTPXM
905917
"refresh_token": normal_user_refresh_token,
906918
"client_id": "a_wrong_dirac_client_id",
907919
},
908-
headers={"Authorization": f"Bearer {normal_user_access_token}"},
909920
)
910921
assert r.status_code == 403
911922
assert r.json()["detail"] == "Unrecognised client_id"

0 commit comments

Comments
 (0)