Skip to content

Commit 0e0891c

Browse files
committed
Make decoder a function
Signed-off-by: Federico Busetti <[email protected]>
1 parent 9e980d8 commit 0e0891c

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/http_app/routes/auth.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/http_app/routes/hello.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
from http_app.templates import templates
55

6-
from .auth import JWTDecoder
6+
from .auth import decode_jwt
77

88
router = APIRouter(prefix="/hello")
99

1010

1111
@router.get("/", response_class=HTMLResponse, include_in_schema=True)
12-
async def hello(request: Request, jwt_token=Security(JWTDecoder())):
12+
async def hello(request: Request, jwt_token=Security(decode_jwt)):
1313
return templates.TemplateResponse(
1414
"hello.html", {"request": request, "token_payload": jwt_token}
1515
)

0 commit comments

Comments
 (0)