2026-02-05 15:13:06 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-02-25 10:20:43 +08:00
|
|
|
from typing import Annotated
|
|
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
from fastapi import APIRouter, Depends, Response
|
2026-02-25 10:20:43 +08:00
|
|
|
from fastapi import HTTPException
|
2026-02-05 15:13:06 +08:00
|
|
|
|
2026-02-25 10:20:43 +08:00
|
|
|
from core.auth.models import CurrentUser
|
2026-02-25 13:34:02 +08:00
|
|
|
from v1.auth.rate_limit import enforce_rate_limit
|
2026-02-05 15:13:06 +08:00
|
|
|
from v1.auth.dependencies import get_auth_service
|
2026-02-25 10:20:43 +08:00
|
|
|
from v1.profile.dependencies import get_current_user
|
2026-02-24 16:38:30 +08:00
|
|
|
from v1.auth.schemas import (
|
2026-02-25 13:34:02 +08:00
|
|
|
AuthResendCodeResponse,
|
|
|
|
|
AuthSignupStartResponse,
|
2026-02-05 15:13:06 +08:00
|
|
|
AuthTokenResponse,
|
2026-02-25 10:20:43 +08:00
|
|
|
AuthUserByEmailResponse,
|
2026-02-05 15:13:06 +08:00
|
|
|
LoginRequest,
|
|
|
|
|
LogoutRequest,
|
|
|
|
|
RefreshRequest,
|
2026-02-25 13:34:02 +08:00
|
|
|
SignupResendRequest,
|
|
|
|
|
SignupStartRequest,
|
|
|
|
|
SignupVerifyRequest,
|
2026-02-05 15:13:06 +08:00
|
|
|
)
|
|
|
|
|
from v1.auth.service import AuthService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 13:34:02 +08:00
|
|
|
@router.post("/signup/start", response_model=AuthSignupStartResponse, status_code=202)
|
|
|
|
|
async def signup_start(
|
|
|
|
|
payload: SignupStartRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthSignupStartResponse:
|
|
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="signup_start",
|
|
|
|
|
identifier=payload.email,
|
|
|
|
|
limit=5,
|
|
|
|
|
window_seconds=60,
|
|
|
|
|
)
|
|
|
|
|
return await service.signup_start(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/signup/verify", response_model=AuthTokenResponse)
|
|
|
|
|
async def signup_verify(
|
|
|
|
|
payload: SignupVerifyRequest,
|
2026-02-05 15:13:06 +08:00
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthTokenResponse:
|
2026-02-25 13:34:02 +08:00
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="signup_verify",
|
|
|
|
|
identifier=payload.email,
|
|
|
|
|
limit=10,
|
|
|
|
|
window_seconds=600,
|
|
|
|
|
)
|
|
|
|
|
return await service.signup_verify(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/signup/resend", response_model=AuthResendCodeResponse)
|
|
|
|
|
async def signup_resend(
|
|
|
|
|
payload: SignupResendRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthResendCodeResponse:
|
|
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="signup_resend",
|
|
|
|
|
identifier=payload.email,
|
2026-02-26 12:07:40 +08:00
|
|
|
limit=5,
|
2026-02-25 13:34:02 +08:00
|
|
|
window_seconds=60,
|
|
|
|
|
)
|
|
|
|
|
return await service.signup_resend(payload)
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/login", response_model=AuthTokenResponse)
|
|
|
|
|
async def login(
|
|
|
|
|
payload: LoginRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthTokenResponse:
|
2026-02-25 16:51:12 +08:00
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="login",
|
|
|
|
|
identifier=payload.email,
|
|
|
|
|
limit=10,
|
|
|
|
|
window_seconds=60,
|
|
|
|
|
)
|
2026-02-05 15:13:06 +08:00
|
|
|
return await service.login(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/refresh", response_model=AuthTokenResponse)
|
|
|
|
|
async def refresh(
|
|
|
|
|
payload: RefreshRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthTokenResponse:
|
2026-02-25 16:51:12 +08:00
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="refresh",
|
|
|
|
|
identifier=payload.refresh_token,
|
|
|
|
|
limit=10,
|
|
|
|
|
window_seconds=60,
|
|
|
|
|
)
|
2026-02-05 15:13:06 +08:00
|
|
|
return await service.refresh(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/logout", status_code=204)
|
|
|
|
|
async def logout(
|
|
|
|
|
payload: LogoutRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> Response:
|
2026-02-25 16:51:12 +08:00
|
|
|
await enforce_rate_limit(
|
|
|
|
|
scope="logout",
|
|
|
|
|
identifier=payload.refresh_token,
|
|
|
|
|
limit=10,
|
|
|
|
|
window_seconds=60,
|
|
|
|
|
)
|
2026-02-05 15:13:06 +08:00
|
|
|
await service.logout(payload.refresh_token)
|
|
|
|
|
return Response(status_code=204)
|
2026-02-25 10:20:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/users/by-email", response_model=AuthUserByEmailResponse)
|
|
|
|
|
async def get_user_by_email(
|
|
|
|
|
email: str,
|
|
|
|
|
current_user: Annotated[CurrentUser, Depends(get_current_user)],
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
) -> AuthUserByEmailResponse:
|
|
|
|
|
if current_user.role != "service_role" and current_user.email != email:
|
|
|
|
|
raise HTTPException(status_code=403, detail="Forbidden")
|
|
|
|
|
return await service.get_user_by_email(email)
|