Skip to content

Commit

Permalink
feat: auth bearer authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaisberg authored and dreulavelle committed Oct 25, 2024
1 parent f397041 commit 0de32fd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 511 deletions.
20 changes: 13 additions & 7 deletions src/auth.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from typing import Optional
from fastapi import HTTPException, Security, status
from fastapi.security import APIKeyHeader
from fastapi.security import APIKeyHeader, HTTPAuthorizationCredentials, HTTPBearer
from program.settings.manager import settings_manager

api_key_header = APIKeyHeader(name="x-api-key")
def header_auth(header = Security(APIKeyHeader(name="x-api-key", auto_error=False))):
return header == settings_manager.settings.api_key

def resolve_api_key(api_key_header: str = Security(api_key_header)):
if api_key_header == settings_manager.settings.api_key:
return True
else:
def bearer_auth(bearer: HTTPAuthorizationCredentials = Security(HTTPBearer(auto_error=False))):
return bearer and bearer.credentials == settings_manager.settings.api_key

def resolve_api_key(
header: Optional[str] = Security(header_auth),
bearer: Optional[HTTPAuthorizationCredentials] = Security(bearer_auth)
):
if not (header or bearer):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing or invalid API key"
detail="Invalid authentication credentials"
)
6 changes: 2 additions & 4 deletions src/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from routers.secure.items import router as items_router
from routers.secure.scrape import router as scrape_router
from routers.secure.settings import router as settings_router
# from routers.secure.tmdb import router as tmdb_router
from routers.secure.webhooks import router as webooks_router
from routers.secure.webhooks import router as webhooks_router
from routers.secure.stream import router as stream_router

API_VERSION = "v1"
Expand All @@ -25,6 +24,5 @@ async def root(_: Request) -> RootResponse:
app_router.include_router(items_router, dependencies=[Depends(resolve_api_key)])
app_router.include_router(scrape_router, dependencies=[Depends(resolve_api_key)])
app_router.include_router(settings_router, dependencies=[Depends(resolve_api_key)])
# app_router.include_router(tmdb_router, dependencies=[Depends(resolve_api_key)])
app_router.include_router(webooks_router, dependencies=[Depends(resolve_api_key)])
app_router.include_router(webhooks_router, dependencies=[Depends(resolve_api_key)])
app_router.include_router(stream_router, dependencies=[Depends(resolve_api_key)])
Loading

0 comments on commit 0de32fd

Please sign in to comment.