diff --git a/backend/endpoints/identity.py b/backend/endpoints/identity.py index df2a08cc1..062906abc 100644 --- a/backend/endpoints/identity.py +++ b/backend/endpoints/identity.py @@ -13,6 +13,10 @@ from models.user import User, Role from utils.cache import cache from utils.auth import authenticate_user, get_password_hash from utils.oauth import protected_route +from exceptions.credentials_exceptions import ( + credentials_exception, + authentication_scheme_exception, +) from config import ROMM_AUTH_ENABLED router = APIRouter() @@ -28,16 +32,8 @@ class UserSchema(BaseModel): orm_mode = True -credentials_exception = HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="Incorrect username or password", - headers={"WWW-Authenticate": "Basic"}, -) - - @router.post("/login", dependencies=[Depends(HTTPBasic(auto_error=False))]) def login(request: Request): - if not os.environ.get("ROMM_AUTH_ENABLED"): return {"message": "RomM auth not enabled."} @@ -48,11 +44,8 @@ def login(request: Request): try: scheme, credentials = auth.split() if scheme.lower() != "basic": - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="Invalid authentication scheme", - headers={"WWW-Authenticate": "Basic"}, - ) + raise authentication_scheme_exception + decoded = base64.b64decode(credentials).decode("ascii") except (ValueError, UnicodeDecodeError, binascii.Error): raise credentials_exception diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index 9227a5f70..775930b12 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -12,7 +12,7 @@ from stream_zip import ZIP_64, stream_zip from logger.logger import log from handler import dbh from utils import fs, get_file_name_with_no_tags -from utils.exceptions import RomNotFoundError, RomAlreadyExistsException +from exceptions.fs_exceptions import RomNotFoundError, RomAlreadyExistsException from utils.oauth import protected_route from models import Rom, Platform from config import LIBRARY_BASE_PATH diff --git a/backend/endpoints/scan.py b/backend/endpoints/scan.py index ef8ceb051..987286279 100644 --- a/backend/endpoints/scan.py +++ b/backend/endpoints/scan.py @@ -4,7 +4,7 @@ from rq import Queue from logger.logger import log from utils import fs, fastapi -from utils.exceptions import PlatformsNotFoundException, RomsNotFoundException +from exceptions.fs_exceptions import PlatformsNotFoundException, RomsNotFoundException from handler import dbh from utils.socket import socket_server from utils.cache import redis_client, redis_url, redis_connectable diff --git a/backend/exceptions/credentials_exceptions.py b/backend/exceptions/credentials_exceptions.py new file mode 100644 index 000000000..ab9d343c8 --- /dev/null +++ b/backend/exceptions/credentials_exceptions.py @@ -0,0 +1,14 @@ +from fastapi import HTTPException, status + + +credentials_exception = HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect username or password", + headers={"WWW-Authenticate": "Basic"}, +) + +authentication_scheme_exception = HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid authentication scheme", + headers={"WWW-Authenticate": "Basic"}, +) diff --git a/backend/utils/exceptions.py b/backend/exceptions/fs_exceptions.py similarity index 100% rename from backend/utils/exceptions.py rename to backend/exceptions/fs_exceptions.py diff --git a/backend/utils/fs.py b/backend/utils/fs.py index 7e0e1eec1..b711c7dfd 100644 --- a/backend/utils/fs.py +++ b/backend/utils/fs.py @@ -14,7 +14,7 @@ from config import ( DEFAULT_PATH_COVER_S, ) from config.config_loader import config -from utils.exceptions import ( +from exceptions.fs_exceptions import ( PlatformsNotFoundException, RomsNotFoundException, RomNotFoundError, diff --git a/backend/utils/tests/test_fastapi.py b/backend/utils/tests/test_fastapi.py index f99650549..09f332a03 100644 --- a/backend/utils/tests/test_fastapi.py +++ b/backend/utils/tests/test_fastapi.py @@ -1,7 +1,7 @@ import pytest from ..fastapi import scan_platform, scan_rom -from ..exceptions import RomsNotFoundException +from ...exceptions.fs_exceptions import RomsNotFoundException from models import Platform, Rom