-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
50 lines (42 loc) · 1.57 KB
/
Copy pathauth.py
File metadata and controls
50 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from fastapi import Depends, HTTPException, Request, status
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.ext.asyncio import AsyncSession
from src.core.database.postgres.session import get_db
from src.core.dependency.tenant import get_optional_tenant_id
from src.modules.user.infrastructure.repositories.user_repository import (
SQLAlchemyUserRepository,
)
oauth2_scheme = OAuth2PasswordBearer(
tokenUrl="/api/v1/auth/login",
refreshUrl="/api/v1/auth/refresh",
auto_error=False,
)
async def get_current_user(
request: Request,
token: str = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_db),
tenant_id: int | None = Depends(get_optional_tenant_id),
) -> dict:
"""
1. 'token' is extracted by oauth2_scheme (for Swagger docs).
2. 'request.state.user_id' was already validated and set by our Auth Middleware.
3. We fetch the full user object from the DB for downstream use.
"""
user_id = getattr(request.state, "user_id", None)
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated. Token invalid or missing.",
)
repo = SQLAlchemyUserRepository(db, tenant_id)
user = await repo.get_by_id(int(user_id))
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Authenticated user not found in database",
)
return {
"id": user.id,
"email": user.email,
"raw_payload": getattr(request.state, "token_payload", {}),
}