Skip to content

Commit f7928b6

Browse files
authored
fix: don't crash when decoding JWT fails (#398)
1 parent e4eff82 commit f7928b6

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

api/app/main.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ def get_user(token: Annotated[HTTPAuthorizationCredentials, Depends(security)]):
4646
openid_configuration = get_openid_configuration()
4747
jwks_client = get_jwks_client()
4848

49-
signing_key = jwks_client.get_signing_key_from_jwt(token.credentials)
50-
decoded_jwt = jwt.decode(
51-
token.credentials,
52-
key=signing_key.key,
53-
algorithms=openid_configuration["id_token_signing_alg_values_supported"],
54-
options={"verify_aud": False},
55-
)
56-
if not decoded_jwt["media"]:
49+
try:
50+
signing_key = jwks_client.get_signing_key_from_jwt(token.credentials)
51+
decoded_jwt = jwt.decode(
52+
token.credentials,
53+
key=signing_key.key,
54+
algorithms=openid_configuration["id_token_signing_alg_values_supported"],
55+
options={"verify_aud": False},
56+
)
57+
except (jwt.exceptions.PyJWTError, Exception) as e:
58+
raise HTTPException(
59+
status_code=status.HTTP_401_UNAUTHORIZED, detail=f"Invalid token"
60+
)
61+
62+
if not decoded_jwt.get("media"):
5763
raise HTTPException(
5864
status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authorized"
5965
)

0 commit comments

Comments
 (0)