extracted credentials exceptions in separated file

This commit is contained in:
zurdi
2023-08-18 09:05:54 +02:00
parent 1131fbe227
commit 3bc15bcbd3
7 changed files with 24 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"},
)

View File

@@ -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,

View File

@@ -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