22from jwt import (
33 ExpiredSignatureError ,
44 ImmatureSignatureError ,
5+ InvalidAlgorithmError ,
56 InvalidSignatureError ,
67 decode ,
78)
1617)
1718
1819DEFAULT_SESSION_TOKEN_LENGTH = 300
20+ ALLOWED_ALGS = {"RS256" }
1921
2022
2123class 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
0 commit comments