Skip to content

Commit 43e4b22

Browse files
committed
Refactor JWT validation in SessionService by removing pre-flight algorithm checks and updating error handling. Adjust tests to reflect changes in algorithm validation, specifically for PyJWKClientError.
1 parent 9fc73bd commit 43e4b22

2 files changed

Lines changed: 5 additions & 22 deletions

File tree

src/corbado_python_sdk/services/implementation/session_service.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import jwt
22
from jwt import (
3-
get_unverified_header, # ← added
43
ExpiredSignatureError,
54
ImmatureSignatureError,
65
InvalidAlgorithmError,
@@ -81,23 +80,6 @@ def validate_token(self, session_token: StrictStr) -> UserEntity:
8180
message=ValidationErrorType.CODE_JWT_EMPTY_SESSION_TOKEN.name,
8281
)
8382

84-
# ---- pre-flight alg rejection ----
85-
try:
86-
header = get_unverified_header(session_token)
87-
except Exception as err:
88-
raise TokenValidationException(
89-
error_type=ValidationErrorType.CODE_JWT_GENERAL,
90-
message=f"Error parsing JWT header: {session_token}",
91-
original_exception=err,
92-
)
93-
if header.get("alg") not in ALLOWED_ALGS:
94-
raise TokenValidationException(
95-
error_type=ValidationErrorType.CODE_JWT_INVALID_SIGNATURE,
96-
message="Algorithm not allowed",
97-
original_exception=InvalidAlgorithmError("Algorithm not allowed"),
98-
)
99-
# -----------------------------------------
100-
10183
# retrieve signing key
10284
try:
10385
signing_key: jwt.PyJWK = self._jwk_client.get_signing_key_from_jwt(token=session_token)

tests/unit/test_session_service.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ImmatureSignatureError,
1111
InvalidAlgorithmError,
1212
InvalidSignatureError,
13+
PyJWKClientError,
1314
encode,
1415
)
1516
from pydantic import ValidationError
@@ -128,8 +129,8 @@ def _provide_jwts(self):
128129
False,
129130
"""eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6
130131
IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.dyt0CoTl4WoVjAHI9Q_CwSKhl6d_9rhM3NrXuJttkao""",
131-
InvalidAlgorithmError,
132-
"Algorithm not allowed",
132+
PyJWKClientError,
133+
'Unable to find a signing key that matches: "None"',
133134
),
134135
# Not before (nfb) in future
135136
(
@@ -183,8 +184,8 @@ def _provide_jwts(self):
183184
(
184185
False,
185186
self._generate_jwt(iss="https://auth.acme.com", exp=int(time()) + 100, nbf=int(time()) - 100, algorithm="none"),
186-
InvalidAlgorithmError,
187-
"Algorithm not allowed",
187+
PyJWKClientError,
188+
'Unable to find a signing key that matches: "None"',
188189
),
189190
# Success with old Frontend API URL in config (2)
190191
(

0 commit comments

Comments
 (0)