Skip to content

Commit

Permalink
chore: Replace python-jose with PyJWT (#3521)
Browse files Browse the repository at this point in the history
Co-authored-by: boc-the-git <3479092+boc-the-git@users.noreply.github.com>
  • Loading branch information
michael-genson and boc-the-git committed Apr 29, 2024
1 parent ab8c3be commit 786aa22
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 65 deletions.
17 changes: 9 additions & 8 deletions mealie/core/dependencies/dependencies.py
Expand Up @@ -5,9 +5,10 @@
from uuid import uuid4

import fastapi
import jwt
from fastapi import BackgroundTasks, Depends, HTTPException, Request, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from jwt.exceptions import PyJWTError
from sqlalchemy.orm.session import Session

from mealie.core import root_logger
Expand Down Expand Up @@ -96,8 +97,8 @@ async def get_current_user(

try:
payload = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
user_id: str = payload.get("sub")
long_token: str = payload.get("long_token")
user_id: str | None = payload.get("sub")
long_token: str | None = payload.get("long_token")

if long_token is not None:
return validate_long_live_token(session, token, payload.get("id"))
Expand All @@ -106,7 +107,7 @@ async def get_current_user(
raise credentials_exception

token_data = TokenData(user_id=user_id)
except JWTError as e:
except PyJWTError as e:
raise credentials_exception from e

repos = get_repositories(session)
Expand All @@ -126,7 +127,7 @@ async def get_integration_id(token: str = Depends(oauth2_scheme)) -> str:
decoded_token = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
return decoded_token.get("integration_id", DEFAULT_INTEGRATION_ID)

except JWTError as e:
except PyJWTError as e:
raise credentials_exception from e


Expand Down Expand Up @@ -162,7 +163,7 @@ def validate_file_token(token: str | None = None) -> Path:
try:
payload = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
file_path = Path(payload.get("file"))
except JWTError as e:
except PyJWTError as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
Expand All @@ -181,7 +182,7 @@ def validate_recipe_token(token: str | None = None) -> str:
Raises:
HTTPException: 400 Bad Request when no token or the recipe doesn't exist
HTTPException: 401 JWTError when token is invalid
HTTPException: 401 PyJWTError when token is invalid
Returns:
str: token data
Expand All @@ -192,7 +193,7 @@ def validate_recipe_token(token: str | None = None) -> str:
try:
payload = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
slug: str | None = payload.get("slug")
except JWTError as e:
except PyJWTError as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
Expand Down
2 changes: 1 addition & 1 deletion mealie/core/security/providers/auth_provider.py
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timedelta, timezone
from typing import Generic, TypeVar

from jose import jwt
import jwt
from sqlalchemy.orm.session import Session

from mealie.core.config import get_app_settings
Expand Down
2 changes: 1 addition & 1 deletion mealie/core/security/security.py
Expand Up @@ -2,8 +2,8 @@
from datetime import datetime, timedelta, timezone
from pathlib import Path

import jwt
from fastapi import Request
from jose import jwt
from sqlalchemy.orm.session import Session

from mealie.core import root_logger
Expand Down
72 changes: 18 additions & 54 deletions 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 pyproject.toml
Expand Up @@ -31,7 +31,6 @@ pyhumps = "^3.5.3"
python = "^3.10"
python-dateutil = "^2.8.2"
python-dotenv = "^1.0.0"
python-jose = "^3.3.0"
python-ldap = "^3.3.1"
python-multipart = "^0.0.9"
python-slugify = "^8.0.0"
Expand All @@ -48,6 +47,7 @@ html2text = "^2024.0.0"
paho-mqtt = "^1.6.1"
pydantic-settings = "^2.1.0"
pillow-heif = "^0.16.0"
pyjwt = "^2.8.0"

[tool.poetry.group.postgres.dependencies]
psycopg2-binary = { version = "^2.9.1" }
Expand Down

0 comments on commit 786aa22

Please sign in to comment.