Skip to content

Commit

Permalink
Migrate from python-jose to pyjwt (#30)
Browse files Browse the repository at this point in the history
* Remove python-jose and add pyjwt dependency

* Refactor to migrate from python-jose to pyjwt
  • Loading branch information
StreetLamb authored May 22, 2024
1 parent 6f5c989 commit 16a9969
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 176 deletions.
5 changes: 3 additions & 2 deletions backend/app/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections.abc import Generator
from typing import Annotated

import jwt
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from jwt.exceptions import InvalidTokenError
from pydantic import ValidationError
from sqlmodel import Session

Expand Down Expand Up @@ -32,7 +33,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
)
token_data = TokenPayload(**payload)
except (JWTError, ValidationError):
except (InvalidTokenError, ValidationError):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Could not validate credentials",
Expand Down
2 changes: 1 addition & 1 deletion backend/app/core/security.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta
from typing import Any

from jose import jwt
import jwt
from passlib.context import CryptContext

from app.core.config import settings
Expand Down
5 changes: 3 additions & 2 deletions backend/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from typing import Any

import emails # type: ignore
import jwt
from jinja2 import Template
from jose import JWTError, jwt
from jwt.exceptions import InvalidTokenError

from app.core.config import settings

Expand Down Expand Up @@ -112,5 +113,5 @@ def verify_password_reset_token(token: str) -> str | None:
try:
decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
return str(decoded_token["sub"])
except JWTError:
except InvalidTokenError:
return None
188 changes: 18 additions & 170 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ emails = "^0.6"
gunicorn = "^22.0.0"
jinja2 = "^3.1.4"
alembic = "^1.12.1"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
httpx = "^0.25.1"
psycopg = {extras = ["binary"], version = "^3.1.13"}
sqlmodel = "^0.0.16"
Expand All @@ -40,6 +39,7 @@ langchain-google-genai = "^1.0.2"
google-search-results = "^2.4.2"
yfinance = "^0.2.38"
langchain-core = "0.2.1"
pyjwt = "^2.8.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
Expand Down

0 comments on commit 16a9969

Please sign in to comment.