@@ -34,35 +34,31 @@ def _jwks_client(config: Annotated[AppConfig, Depends(app_config)]) -> jwt.PyJWK
3434 return jwt .PyJWKClient (config .AUTH .JWKS_URL )
3535
3636
37- class JWTDecoder :
38- """Does all the token verification using PyJWT"""
39-
40- async def __call__ (
41- self ,
42- security_scopes : SecurityScopes ,
43- config : AppConfig = Depends (app_config ),
44- jwks_client : jwt .PyJWKClient = Depends (_jwks_client ),
45- token : Optional [HTTPAuthorizationCredentials ] = Depends (HTTPBearer ()),
46- ):
47- if token is None :
48- raise UnauthenticatedException ()
49-
50- try :
51- signing_key = jwks_client .get_signing_key_from_jwt (token .credentials ).key
52- except jwt .exceptions .PyJWKClientError as error :
53- raise UnauthorizedException (str (error ))
54- except jwt .exceptions .DecodeError as error :
55- raise UnauthorizedException (str (error ))
56-
57- try :
58- # TODO: Review decode setup and verifications
59- # https://pyjwt.readthedocs.io/en/stable/api.html#jwt.decode
60- payload = jwt .decode (
61- jwt = token .credentials ,
62- key = signing_key ,
63- algorithms = [config .AUTH .JWT_ALGORITHM ],
64- )
65- except Exception as error :
66- raise UnauthorizedException (str (error ))
37+ async def decode_jwt (
38+ security_scopes : SecurityScopes ,
39+ config : AppConfig = Depends (app_config ),
40+ jwks_client : jwt .PyJWKClient = Depends (_jwks_client ),
41+ token : Optional [HTTPAuthorizationCredentials ] = Depends (HTTPBearer ()),
42+ ):
43+ if token is None :
44+ raise UnauthenticatedException ()
45+
46+ try :
47+ signing_key = jwks_client .get_signing_key_from_jwt (token .credentials ).key
48+ except jwt .exceptions .PyJWKClientError as error :
49+ raise UnauthorizedException (str (error ))
50+ except jwt .exceptions .DecodeError as error :
51+ raise UnauthorizedException (str (error ))
52+
53+ try :
54+ # TODO: Review decode setup and verifications
55+ # https://pyjwt.readthedocs.io/en/stable/api.html#jwt.decode
56+ payload = jwt .decode (
57+ jwt = token .credentials ,
58+ key = signing_key ,
59+ algorithms = [config .AUTH .JWT_ALGORITHM ],
60+ )
61+ except Exception as error :
62+ raise UnauthorizedException (str (error ))
6763
68- return payload
64+ return payload
0 commit comments