22 lines
821 B
Python
22 lines
821 B
Python
from typing import Annotated
|
|
|
|
from fastapi import Depends
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.errors import UnauthorizedError
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.services.auth_service import AuthService, get_auth_service
|
|
|
|
bearer_scheme = HTTPBearer(auto_error=False)
|
|
|
|
|
|
def get_current_user(
|
|
credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(bearer_scheme)],
|
|
auth_service: Annotated[AuthService, Depends(get_auth_service)],
|
|
) -> User:
|
|
if credentials is None or credentials.scheme.lower() != "bearer":
|
|
raise UnauthorizedError("Missing or invalid authorization token")
|
|
return auth_service.get_user_by_id(auth_service.decode_user_id(credentials.credentials))
|