32 lines
947 B
Python
32 lines
947 B
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from app.auth.jwt_auth import create_access_token
|
|
from app.config import get_settings
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
|
username: str
|
|
password: str
|
|
|
|
|
|
class TokenResponse(BaseModel):
|
|
access_token: str
|
|
token_type: str = "bearer"
|
|
|
|
|
|
@router.post("/login", response_model=TokenResponse)
|
|
def login(body: LoginRequest) -> TokenResponse:
|
|
settings = get_settings()
|
|
if not settings.sac_admin_password:
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail="SAC_ADMIN_PASSWORD is not configured",
|
|
)
|
|
if body.username != settings.sac_admin_username or body.password != settings.sac_admin_password:
|
|
raise HTTPException(status_code=401, detail="Invalid username or password")
|
|
token = create_access_token(body.username)
|
|
return TokenResponse(access_token=token)
|