mirror of
https://github.com/velocitatem/cvfs.git
synced 2026-05-31 08:43:37 +00:00
Finish MVP and dockerize
This commit is contained in:
42
apps/backend/fastapi/app/api/deps.py
Normal file
42
apps/backend/fastapi/app/api/deps.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
from fastapi import Depends, HTTPException, Request, status
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.config import get_settings
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from dlib.auth import AuthenticatedUser, build_validator
|
||||
|
||||
|
||||
security_scheme = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def _validator():
|
||||
settings = get_settings()
|
||||
return build_validator(
|
||||
issuer=settings.auth_oidc_issuer,
|
||||
audience=settings.auth_oidc_audience,
|
||||
disable=settings.auth_disable_verification,
|
||||
)
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
credentials: HTTPAuthorizationCredentials | None = Depends(security_scheme),
|
||||
) -> AuthenticatedUser:
|
||||
validator = _validator()
|
||||
token = credentials.credentials if credentials else ""
|
||||
try:
|
||||
return await validator.validate(token)
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED, detail=str(exc)
|
||||
) from exc
|
||||
Reference in New Issue
Block a user