2026-05-21 22:11:38 +08:00
|
|
|
"""Nexus — Auth API Routes (Login / TOTP / JWT / Refresh / Logout)
|
2026-05-20 14:42:55 +08:00
|
|
|
Presentation layer — receives HTTP requests, delegates to AuthService.
|
2026-05-21 22:11:38 +08:00
|
|
|
|
|
|
|
|
A2: TOTP endpoints now require JWT authentication via get_current_admin.
|
2026-05-20 14:42:55 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
2026-05-21 22:11:38 +08:00
|
|
|
from pydantic import BaseModel, Field
|
2026-05-20 14:42:55 +08:00
|
|
|
|
|
|
|
|
from server.api.dependencies import get_auth_service
|
2026-05-21 22:11:38 +08:00
|
|
|
from server.api.auth_jwt import get_current_admin
|
2026-05-20 14:42:55 +08:00
|
|
|
from server.application.services.auth_service import AuthService
|
2026-05-21 22:11:38 +08:00
|
|
|
from server.domain.models import Admin
|
2026-05-20 14:42:55 +08:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
# ── Request Models ──
|
|
|
|
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
|
|
|
|
username: str = Field(..., min_length=1, max_length=100)
|
|
|
|
|
password: str = Field(..., min_length=1, max_length=255)
|
|
|
|
|
totp_code: Optional[str] = Field(None, min_length=6, max_length=6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RefreshRequest(BaseModel):
|
|
|
|
|
refresh_token: str = Field(..., min_length=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogoutRequest(BaseModel):
|
|
|
|
|
admin_id: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TotpSetupRequest(BaseModel):
|
|
|
|
|
admin_id: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TotpVerifyRequest(BaseModel):
|
|
|
|
|
admin_id: int
|
|
|
|
|
totp_code: str = Field(..., min_length=6, max_length=6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Public Routes (no JWT required) ──
|
|
|
|
|
|
|
|
|
|
@router.post("/login")
|
2026-05-20 14:42:55 +08:00
|
|
|
async def login(
|
|
|
|
|
request: Request,
|
2026-05-21 22:11:38 +08:00
|
|
|
payload: LoginRequest,
|
2026-05-20 14:42:55 +08:00
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
2026-05-21 22:11:38 +08:00
|
|
|
"""Authenticate admin user (password + optional TOTP) → return JWT tokens"""
|
2026-05-20 14:42:55 +08:00
|
|
|
ip_address = request.client.host if request.client else "unknown"
|
|
|
|
|
result = await service.login(
|
2026-05-21 22:11:38 +08:00
|
|
|
username=payload.username,
|
|
|
|
|
password=payload.password,
|
2026-05-20 14:42:55 +08:00
|
|
|
ip_address=ip_address,
|
2026-05-21 22:11:38 +08:00
|
|
|
totp_code=payload.totp_code,
|
2026-05-20 14:42:55 +08:00
|
|
|
)
|
|
|
|
|
if not result["success"]:
|
|
|
|
|
status_code = 401
|
|
|
|
|
if result.get("reason") == "account_locked":
|
|
|
|
|
status_code = 429
|
2026-05-21 22:11:38 +08:00
|
|
|
elif result.get("reason") == "totp_required":
|
|
|
|
|
status_code = 202 # Accepted but needs TOTP
|
2026-05-20 14:42:55 +08:00
|
|
|
raise HTTPException(status_code=status_code, detail=result.get("message", "Login failed"))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
@router.post("/refresh")
|
|
|
|
|
async def refresh_token(
|
|
|
|
|
payload: RefreshRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
|
|
|
|
"""Exchange refresh token for new access + refresh token pair"""
|
|
|
|
|
result = await service.refresh_token(payload.refresh_token)
|
|
|
|
|
if not result["success"]:
|
|
|
|
|
raise HTTPException(status_code=401, detail=result.get("message", "Invalid refresh token"))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/logout")
|
|
|
|
|
async def logout(
|
|
|
|
|
payload: LogoutRequest,
|
|
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
|
|
|
|
"""Invalidate refresh token (client should also discard access token)"""
|
|
|
|
|
result = await service.logout(payload.admin_id)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Protected Routes (JWT required) ──
|
|
|
|
|
|
|
|
|
|
@router.post("/totp/setup")
|
2026-05-20 14:42:55 +08:00
|
|
|
async def setup_totp(
|
2026-05-21 22:11:38 +08:00
|
|
|
payload: TotpSetupRequest,
|
|
|
|
|
admin: Admin = Depends(get_current_admin),
|
2026-05-20 14:42:55 +08:00
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
2026-05-21 22:11:38 +08:00
|
|
|
"""Generate TOTP secret for admin user (requires JWT auth)
|
2026-05-20 14:42:55 +08:00
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
A2: JWT authentication required — admin can only setup TOTP for themselves.
|
2026-05-20 14:42:55 +08:00
|
|
|
"""
|
2026-05-21 22:11:38 +08:00
|
|
|
# Security: only allow setting up TOTP for the authenticated admin
|
|
|
|
|
if payload.admin_id != admin.id:
|
|
|
|
|
raise HTTPException(status_code=403, detail="Can only setup TOTP for yourself")
|
|
|
|
|
|
|
|
|
|
result = await service.setup_totp(admin.id)
|
2026-05-20 14:42:55 +08:00
|
|
|
if not result["success"]:
|
|
|
|
|
raise HTTPException(status_code=400, detail=result.get("reason", "Setup failed"))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
@router.post("/totp/enable")
|
2026-05-20 14:42:55 +08:00
|
|
|
async def enable_totp(
|
2026-05-21 22:11:38 +08:00
|
|
|
payload: TotpVerifyRequest,
|
|
|
|
|
admin: Admin = Depends(get_current_admin),
|
2026-05-20 14:42:55 +08:00
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
2026-05-21 22:11:38 +08:00
|
|
|
"""Enable TOTP after verification (requires JWT auth)
|
2026-05-20 14:42:55 +08:00
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
A2: JWT authentication required — admin can only enable TOTP for themselves.
|
2026-05-20 14:42:55 +08:00
|
|
|
"""
|
2026-05-21 22:11:38 +08:00
|
|
|
if payload.admin_id != admin.id:
|
|
|
|
|
raise HTTPException(status_code=403, detail="Can only enable TOTP for yourself")
|
|
|
|
|
|
|
|
|
|
result = await service.enable_totp(admin.id, payload.totp_code)
|
2026-05-20 14:42:55 +08:00
|
|
|
if not result["success"]:
|
|
|
|
|
raise HTTPException(status_code=400, detail=result.get("message", "Enable failed"))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
@router.post("/totp/disable")
|
2026-05-20 14:42:55 +08:00
|
|
|
async def disable_totp(
|
2026-05-21 22:11:38 +08:00
|
|
|
payload: TotpSetupRequest,
|
|
|
|
|
admin: Admin = Depends(get_current_admin),
|
2026-05-20 14:42:55 +08:00
|
|
|
service: AuthService = Depends(get_auth_service),
|
|
|
|
|
):
|
2026-05-21 22:11:38 +08:00
|
|
|
"""Disable TOTP for admin user (requires JWT auth)
|
2026-05-20 14:42:55 +08:00
|
|
|
|
2026-05-21 22:11:38 +08:00
|
|
|
A2: JWT authentication required — admin can only disable TOTP for themselves.
|
2026-05-20 14:42:55 +08:00
|
|
|
"""
|
2026-05-21 22:11:38 +08:00
|
|
|
if payload.admin_id != admin.id:
|
|
|
|
|
raise HTTPException(status_code=403, detail="Can only disable TOTP for yourself")
|
|
|
|
|
|
|
|
|
|
result = await service.disable_totp(admin.id)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/me")
|
|
|
|
|
async def get_me(admin: Admin = Depends(get_current_admin)):
|
|
|
|
|
"""Get current authenticated admin info (JWT required)"""
|
|
|
|
|
return {
|
|
|
|
|
"id": admin.id,
|
|
|
|
|
"username": admin.username,
|
|
|
|
|
"email": admin.email,
|
|
|
|
|
"totp_enabled": admin.totp_enabled,
|
|
|
|
|
"is_active": admin.is_active,
|
|
|
|
|
"last_login": str(admin.last_login) if admin.last_login else None,
|
|
|
|
|
}
|