48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from typing import Annotated
|
|
|
|
import boto3
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.config import get_settings
|
|
from app.core.security import decode_access_token
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
|
|
DbSession = Annotated[AsyncSession, Depends(get_db)]
|
|
|
|
_oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/token", auto_error=True)
|
|
|
|
|
|
async def get_current_user(
|
|
db: DbSession, token: Annotated[str, Depends(_oauth2_scheme)]
|
|
) -> User:
|
|
credentials_error = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
user_id = decode_access_token(token)
|
|
if user_id is None:
|
|
raise credentials_error
|
|
result = await db.execute(select(User).where(User.id == user_id))
|
|
user = result.scalar_one_or_none()
|
|
if user is None:
|
|
raise credentials_error
|
|
return user
|
|
|
|
|
|
CurrentUser = Annotated[User, Depends(get_current_user)]
|
|
|
|
|
|
def get_minio_client():
|
|
settings = get_settings()
|
|
return boto3.client(
|
|
"s3",
|
|
endpoint_url=("https://" if settings.minio_use_ssl else "http://") + settings.minio_endpoint,
|
|
aws_access_key_id=settings.minio_access_key,
|
|
aws_secret_access_key=settings.minio_secret_key,
|
|
)
|