Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error when token has expired #768

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/auth/auth_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from jwt import PyJWKClient
from datetime import datetime, timedelta
from decouple import config
from fastapi import HTTPException


JWT_SECRET = config("JWT_SECRET")
JWT_ALGORITHM = config("JWT_ALGORITHM")
Expand All @@ -23,7 +25,10 @@ def decode_token(token: str) -> dict:
jwks_client = PyJWKClient(OIDC_CERTS_URL)
test = jwks_client.get_signing_key_from_jwt(token)
signing_key = test.key
decoded_token = jwt.decode(token, signing_key, audience=JWT_AUDIENCE, algorithms=[JWT_ALGORITHM])
try:
decoded_token = jwt.decode(token, signing_key, audience=JWT_AUDIENCE, algorithms=[JWT_ALGORITHM])
except jwt.exceptions.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token expired. Please authenticate again.")
return decoded_token if decoded_token["exp"] >= time.time() else None


Expand Down