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

chore: Replace python-jose with PyJWT #3521

Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions mealie/core/dependencies/dependencies.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading