Skip to content

Commit

Permalink
Replace python-jose with PyJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
MelissaAutumn committed May 1, 2024
1 parent 202d5d6 commit 7983e3c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ markdown==3.6
MarkupSafe==2.1.2
nh3==0.2.17
python-dotenv==1.0.0
python-jose==3.3.0
python-multipart==0.0.7
PyJWT==2.6.0
pydantic==2.5.2
sentry-sdk==1.26.0
starlette-context==0.3.6
Expand Down
14 changes: 7 additions & 7 deletions backend/src/appointment/dependencies/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from fastapi import Depends, Request, HTTPException, Body
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
import jwt

from sqlalchemy.orm import Session

Expand All @@ -22,18 +22,18 @@ def get_user_from_token(db, token: str):
iat = payload.get("iat")
if sub is None:
raise InvalidTokenException()
except JWTError:
except jwt.exceptions.InvalidTokenError:
raise InvalidTokenException()

id = sub.replace('uid-', '')
subscriber = repo.subscriber.get(db, int(id))

# Token has been expired by us - temp measure to avoid spinning a refresh system, or a deny list for this issue
if subscriber is None:
raise InvalidTokenException()
elif subscriber.minimum_valid_iat_time and not iat:
raise InvalidTokenException()
elif subscriber.minimum_valid_iat_time and subscriber.minimum_valid_iat_time.timestamp() > int(iat):
if any([
subscriber is None,
subscriber and subscriber.minimum_valid_iat_time and not iat,
subscriber and subscriber.minimum_valid_iat_time and subscriber.minimum_valid_iat_time.timestamp() > int(iat)
]):
raise InvalidTokenException()

return subscriber
Expand Down
7 changes: 4 additions & 3 deletions backend/src/appointment/dependencies/fxa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import os

from fastapi import Request, Depends
from jose import jwt, jwk
#from jose import jwt, jwk
import jwt

from ..controller.apis.fxa_client import FxaClient

Expand Down Expand Up @@ -31,7 +32,7 @@ def get_webhook_auth(request: Request, fxa_client: FxaClient = Depends(get_fxa_c
logging.error("No public jwks available.")
return None

headers = jwt.get_unverified_headers(header_token)
headers = jwt.get_unverified_header(header_token)

if 'kid' not in headers:
logging.error("Error decoding token. Key ID is missing from headers.")
Expand All @@ -40,7 +41,7 @@ def get_webhook_auth(request: Request, fxa_client: FxaClient = Depends(get_fxa_c
jwk_pem = None
for current_jwk in public_jwks:
if current_jwk.get('kid') == headers.get('kid'):
jwk_pem = jwk.construct(current_jwk)
jwk_pem = jwt.PyJWK(current_jwk)
break

if jwk_pem is None:
Expand Down
2 changes: 1 addition & 1 deletion backend/src/appointment/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import Annotated

import argon2.exceptions
import jwt
from fastapi.security import OAuth2PasswordRequestForm
from jose import jwt
from sentry_sdk import capture_exception
from sqlalchemy.orm import Session

Expand Down

0 comments on commit 7983e3c

Please sign in to comment.