Skip to content

Commit 3ce3ef2

Browse files
committed
Enhance session token validation by adding support for InvalidAlgorithmError and updating error handling for token decoding. Update tests to cover disallowed algorithms.
1 parent 3440ff1 commit 3ce3ef2

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/corbado_python_sdk/services/implementation/session_service.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
ExpiredSignatureError,
44
ImmatureSignatureError,
55
InvalidSignatureError,
6+
InvalidAlgorithmError,
67
decode,
78
)
89
from jwt.jwks_client import PyJWKClient
@@ -16,6 +17,7 @@
1617
)
1718

1819
DEFAULT_SESSION_TOKEN_LENGTH = 300
20+
ALLOWED_ALGS = {"RS256"}
1921

2022

2123
class SessionService(BaseModel):
@@ -90,7 +92,7 @@ def validate_token(self, session_token: StrictStr) -> UserEntity:
9092

9193
# decode short session (jwt) with signing key
9294
try:
93-
payload = decode(jwt=session_token, key=signing_key.key, algorithms=["RS256"])
95+
payload = decode(jwt=session_token, key=signing_key.key, algorithms=list(ALLOWED_ALGS))
9496

9597
# extract information from decoded payload
9698
token_issuer: str = payload.get("iss")
@@ -104,15 +106,21 @@ def validate_token(self, session_token: StrictStr) -> UserEntity:
104106
)
105107
except ExpiredSignatureError as error:
106108
raise TokenValidationException(
107-
error_type=ValidationErrorType.CODE_JWT_INVALID_SIGNATURE,
108-
message=f"Error occured during token decode: {session_token}. {ValidationErrorType.CODE_JWT_INVALID_SIGNATURE.value}",
109+
error_type=ValidationErrorType.CODE_JWT_EXPIRED,
110+
message=f"Error occured during token decode: {session_token}. {ValidationErrorType.CODE_JWT_EXPIRED.value}",
109111
original_exception=error,
110112
)
111113

112114
except InvalidSignatureError as error:
113115
raise TokenValidationException(
114-
error_type=ValidationErrorType.CODE_JWT_EXPIRED,
115-
message=f"Error occured during token decode: {session_token}. {ValidationErrorType.CODE_JWT_EXPIRED.value}",
116+
error_type=ValidationErrorType.CODE_JWT_INVALID_SIGNATURE,
117+
message=f"Error occured during token decode: {session_token}. {ValidationErrorType.CODE_JWT_INVALID_SIGNATURE.value}",
118+
original_exception=error,
119+
)
120+
except InvalidAlgorithmError as error:
121+
raise TokenValidationException(
122+
error_type=ValidationErrorType.CODE_JWT_INVALID_SIGNATURE,
123+
message="Algorithm not allowed",
116124
original_exception=error,
117125
)
118126

tests/unit/test_session_service.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ExpiredSignatureError,
1010
ImmatureSignatureError,
1111
InvalidSignatureError,
12+
InvalidAlgorithmError,
1213
PyJWKClientError,
1314
encode,
1415
)
@@ -193,6 +194,16 @@ def _provide_jwts(self):
193194
None,
194195
None,
195196
),
197+
# Disallowed algorithm "none"
198+
(
199+
False,
200+
"eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0."
201+
"eyJpc3MiOiJodHRwczovL2F1dGguYWNtZS5jb20iLCJzdWIiOiIxMjM0NSIsImlhdCI6"
202+
+ str(int(time()))
203+
+ "f.",
204+
InvalidAlgorithmError,
205+
"Algorithm not allowed",
206+
),
196207
]
197208

198209
@classmethod

0 commit comments

Comments
 (0)