Skip to content

Commit

Permalink
misc: Move auth constants to separate file
Browse files Browse the repository at this point in the history
This simplifies avoiding circular imports when trying to use auth
handlers.
  • Loading branch information
adamantike committed Jan 9, 2025
1 parent fb02db6 commit 865370e
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 77 deletions.
2 changes: 1 addition & 1 deletion backend/decorators/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from fastapi.security.http import HTTPBasic
from fastapi.security.oauth2 import OAuth2PasswordBearer
from fastapi.types import DecoratedCallable
from handler.auth.base_handler import (
from handler.auth.constants import (
DEFAULT_SCOPES_MAP,
FULL_SCOPES_MAP,
WRITE_SCOPES_MAP,
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
CollectionPermissionError,
)
from fastapi import Request, UploadFile
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_collection_handler
from handler.filesystem import fs_resource_handler
from handler.filesystem.base_handler import CoverSize
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ConfigNotWritableException,
)
from fastapi import HTTPException, Request, status
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from logger.logger import log
from utils.router import APIRouter

Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WebrcadeFeedSchema,
)
from fastapi import Request
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_platform_handler, db_rom_handler
from handler.metadata import meta_igdb_handler
from handler.metadata.base_hander import SWITCH_TITLEDB_REGEX
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from endpoints.responses.firmware import AddFirmwareResponse, FirmwareSchema
from fastapi import File, HTTPException, Request, UploadFile, status
from fastapi.responses import FileResponse
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_firmware_handler, db_platform_handler
from handler.filesystem import fs_firmware_handler
from handler.scan_handler import scan_firmware
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from exceptions.endpoint_exceptions import PlatformNotFoundInDatabaseException
from exceptions.fs_exceptions import PlatformAlreadyExistsException
from fastapi import Request
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_platform_handler
from handler.filesystem import fs_platform_handler
from handler.metadata.igdb_handler import IGDB_PLATFORM_LIST
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from decorators.auth import protected_route
from fastapi import Request
from fastapi.responses import FileResponse
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from utils.router import APIRouter

router = APIRouter()
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from exceptions.fs_exceptions import RomAlreadyExistsException
from fastapi import HTTPException, Query, Request, UploadFile, status
from fastapi.responses import Response
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_collection_handler, db_platform_handler, db_rom_handler
from handler.filesystem import fs_resource_handler, fs_rom_handler
from handler.filesystem.base_handler import CoverSize
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/saves.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from endpoints.responses.assets import SaveSchema, UploadedSavesResponse
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from fastapi import File, HTTPException, Request, UploadFile, status
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_rom_handler, db_save_handler, db_screenshot_handler
from handler.filesystem import fs_asset_handler
from handler.scan_handler import scan_save
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/screenshots.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from decorators.auth import protected_route
from endpoints.responses.assets import UploadedScreenshotsResponse
from fastapi import File, HTTPException, Request, UploadFile, status
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_rom_handler, db_screenshot_handler
from handler.filesystem import fs_asset_handler
from handler.scan_handler import scan_screenshot
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from decorators.auth import protected_route
from endpoints.responses.search import SearchCoverSchema, SearchRomSchema
from fastapi import HTTPException, Request, status
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_rom_handler
from handler.metadata import meta_igdb_handler, meta_moby_handler, meta_sgdb_handler
from handler.metadata.igdb_handler import IGDB_API_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from endpoints.responses.assets import StateSchema, UploadedStatesResponse
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from fastapi import File, HTTPException, Request, UploadFile, status
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_rom_handler, db_screenshot_handler, db_state_handler
from handler.filesystem import fs_asset_handler
from handler.scan_handler import scan_state
Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from decorators.auth import protected_route
from endpoints.responses import MessageResponse
from fastapi import Request
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from tasks.update_switch_titledb import update_switch_titledb_task
from utils.router import APIRouter

Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from endpoints.auth import ACCESS_TOKEN_EXPIRE_MINUTES
from fastapi.exceptions import HTTPException
from fastapi.testclient import TestClient
from handler.auth.base_handler import WRITE_SCOPES
from handler.auth.constants import WRITE_SCOPES
from main import app


Expand Down
2 changes: 1 addition & 1 deletion backend/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from endpoints.responses.identity import UserSchema
from fastapi import Depends, HTTPException, Request, status
from handler.auth import auth_handler
from handler.auth.base_handler import Scope
from handler.auth.constants import Scope
from handler.database import db_user_handler
from handler.filesystem import fs_asset_handler
from logger.logger import log
Expand Down
58 changes: 2 additions & 56 deletions backend/handler/auth/base_handler.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,18 @@
import enum
import uuid
from datetime import datetime, timedelta, timezone
from typing import Any, Final
from typing import Any

from config import OIDC_ENABLED, ROMM_AUTH_SECRET_KEY
from exceptions.auth_exceptions import OAuthCredentialsException, UserDisabledException
from fastapi import HTTPException, status
from handler.auth.constants import ALGORITHM, DEFAULT_OAUTH_TOKEN_EXPIRY
from joserfc import jwt
from joserfc.errors import BadSignatureError
from joserfc.jwk import OctKey
from logger.logger import log
from passlib.context import CryptContext
from starlette.requests import HTTPConnection

ALGORITHM: Final = "HS256"
DEFAULT_OAUTH_TOKEN_EXPIRY: Final = timedelta(minutes=15)


class Scope(enum.StrEnum):
ME_READ = "me.read"
ME_WRITE = "me.write"
ROMS_READ = "roms.read"
ROMS_WRITE = "roms.write"
ROMS_USER_READ = "roms.user.read"
ROMS_USER_WRITE = "roms.user.write"
PLATFORMS_READ = "platforms.read"
PLATFORMS_WRITE = "platforms.write"
ASSETS_READ = "assets.read"
ASSETS_WRITE = "assets.write"
FIRMWARE_READ = "firmware.read"
FIRMWARE_WRITE = "firmware.write"
COLLECTIONS_READ = "collections.read"
COLLECTIONS_WRITE = "collections.write"
USERS_READ = "users.read"
USERS_WRITE = "users.write"
TASKS_RUN = "tasks.run"


DEFAULT_SCOPES_MAP: Final = {
Scope.ME_READ: "View your profile",
Scope.ME_WRITE: "Modify your profile",
Scope.ROMS_READ: "View ROMs",
Scope.PLATFORMS_READ: "View platforms",
Scope.ASSETS_READ: "View assets",
Scope.ASSETS_WRITE: "Modify assets",
Scope.FIRMWARE_READ: "View firmware",
Scope.ROMS_USER_READ: "View user-rom properties",
Scope.ROMS_USER_WRITE: "Modify user-rom properties",
Scope.COLLECTIONS_READ: "View collections",
Scope.COLLECTIONS_WRITE: "Modify collections",
}

WRITE_SCOPES_MAP: Final = {
Scope.ROMS_WRITE: "Modify ROMs",
Scope.PLATFORMS_WRITE: "Modify platforms",
Scope.FIRMWARE_WRITE: "Modify firmware",
}

FULL_SCOPES_MAP: Final = {
Scope.USERS_READ: "View users",
Scope.USERS_WRITE: "Modify users",
Scope.TASKS_RUN: "Run tasks",
}

DEFAULT_SCOPES: Final = list(DEFAULT_SCOPES_MAP.keys())
WRITE_SCOPES: Final = DEFAULT_SCOPES + list(WRITE_SCOPES_MAP.keys())
FULL_SCOPES: Final = WRITE_SCOPES + list(FULL_SCOPES_MAP.keys())


class AuthHandler:
def __init__(self) -> None:
Expand Down
57 changes: 57 additions & 0 deletions backend/handler/auth/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import enum
from datetime import timedelta
from typing import Final

ALGORITHM: Final = "HS256"
DEFAULT_OAUTH_TOKEN_EXPIRY: Final = timedelta(minutes=15)


class Scope(enum.StrEnum):
ME_READ = "me.read"
ME_WRITE = "me.write"
ROMS_READ = "roms.read"
ROMS_WRITE = "roms.write"
ROMS_USER_READ = "roms.user.read"
ROMS_USER_WRITE = "roms.user.write"
PLATFORMS_READ = "platforms.read"
PLATFORMS_WRITE = "platforms.write"
ASSETS_READ = "assets.read"
ASSETS_WRITE = "assets.write"
FIRMWARE_READ = "firmware.read"
FIRMWARE_WRITE = "firmware.write"
COLLECTIONS_READ = "collections.read"
COLLECTIONS_WRITE = "collections.write"
USERS_READ = "users.read"
USERS_WRITE = "users.write"
TASKS_RUN = "tasks.run"


DEFAULT_SCOPES_MAP: Final = {
Scope.ME_READ: "View your profile",
Scope.ME_WRITE: "Modify your profile",
Scope.ROMS_READ: "View ROMs",
Scope.PLATFORMS_READ: "View platforms",
Scope.ASSETS_READ: "View assets",
Scope.ASSETS_WRITE: "Modify assets",
Scope.FIRMWARE_READ: "View firmware",
Scope.ROMS_USER_READ: "View user-rom properties",
Scope.ROMS_USER_WRITE: "Modify user-rom properties",
Scope.COLLECTIONS_READ: "View collections",
Scope.COLLECTIONS_WRITE: "Modify collections",
}

WRITE_SCOPES_MAP: Final = {
Scope.ROMS_WRITE: "Modify ROMs",
Scope.PLATFORMS_WRITE: "Modify platforms",
Scope.FIRMWARE_WRITE: "Modify firmware",
}

FULL_SCOPES_MAP: Final = {
Scope.USERS_READ: "View users",
Scope.USERS_WRITE: "Modify users",
Scope.TASKS_RUN: "Run tasks",
}

DEFAULT_SCOPES: Final = list(DEFAULT_SCOPES_MAP.keys())
WRITE_SCOPES: Final = DEFAULT_SCOPES + list(WRITE_SCOPES_MAP.keys())
FULL_SCOPES: Final = WRITE_SCOPES + list(FULL_SCOPES_MAP.keys())
2 changes: 1 addition & 1 deletion backend/handler/auth/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from fastapi.exceptions import HTTPException
from handler.auth import auth_handler, oauth_handler
from handler.auth.base_handler import WRITE_SCOPES
from handler.auth.constants import WRITE_SCOPES
from handler.auth.hybrid_auth import HybridAuthBackend
from handler.database import db_user_handler
from models.user import User
Expand Down
2 changes: 1 addition & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from handler.auth.base_handler import ALGORITHM
from handler.auth.constants import ALGORITHM
from handler.auth.hybrid_auth import HybridAuthBackend
from handler.auth.middleware import CustomCSRFMiddleware, SessionMiddleware
from handler.socket_handler import socket_handler
Expand Down
2 changes: 1 addition & 1 deletion backend/models/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from handler.auth.base_handler import DEFAULT_SCOPES, FULL_SCOPES, WRITE_SCOPES
from handler.auth.constants import DEFAULT_SCOPES, FULL_SCOPES, WRITE_SCOPES
from models.user import User


Expand Down
4 changes: 1 addition & 3 deletions backend/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from datetime import datetime, timezone
from typing import TYPE_CHECKING

from handler.auth.constants import DEFAULT_SCOPES, FULL_SCOPES, WRITE_SCOPES, Scope
from models.base import BaseModel
from sqlalchemy import DateTime, Enum, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from starlette.authentication import SimpleUser

if TYPE_CHECKING:
from handler.auth.base_handler import Scope
from models.assets import Save, Screenshot, State
from models.collection import Collection
from models.rom import RomUser
Expand Down Expand Up @@ -47,8 +47,6 @@ class User(BaseModel, SimpleUser):

@property
def oauth_scopes(self) -> list[Scope]:
from handler.auth.base_handler import DEFAULT_SCOPES, FULL_SCOPES, WRITE_SCOPES

if self.role == Role.ADMIN:
return FULL_SCOPES

Expand Down

0 comments on commit 865370e

Please sign in to comment.