e51f357b48
Co-authored-by: Cursor <cursoragent@cursor.com>
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
from ..config import Settings, get_settings
|
|
from .auth import LoginRequest, LoginResponse, issue_token, require_user
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/login", response_model=LoginResponse)
|
|
async def login(body: LoginRequest, settings: Annotated[Settings, Depends(get_settings)]) -> LoginResponse:
|
|
user_ok = body.username == settings.auth_username
|
|
pass_ok = body.password == settings.auth_password
|
|
if not (user_ok and pass_ok):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户名或密码错误")
|
|
token, ttl = issue_token(body.username, settings)
|
|
return LoginResponse(
|
|
token=token,
|
|
username=body.username,
|
|
expires_in=ttl,
|
|
env_name=settings.env_name,
|
|
mode=settings.mode,
|
|
)
|
|
|
|
|
|
@router.get("/me")
|
|
async def me(username: Annotated[str, Depends(require_user)], settings: Annotated[Settings, Depends(get_settings)]) -> dict:
|
|
return {
|
|
"username": username,
|
|
"env_name": settings.env_name,
|
|
"mode": settings.mode,
|
|
"sim": settings.is_sim,
|
|
}
|