- Added `auth.py` for mock login and JWT issuance. - Introduced `chat.py` for handling chat requests with role-based access control. - Enhanced `main.py` to include new routers and middleware for tracing. - Implemented input validation in `input_guard.py` to prevent SQL injection. - Created repositories for managing agent sessions and audit logs. - Added exception handling for authorization errors. - Updated settings to include JWT configuration. - Introduced tests for authentication and input validation.
26 lines
817 B
Python
26 lines
817 B
Python
"""开发期 Mock 登录:签发 JWT。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from app.gateway.jwt_service import infer_roles, issue_token
|
|
from app.model.schemas import LoginRequest, LoginResponseData
|
|
from app.utils.response import ok
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/login")
|
|
def login(body: LoginRequest, request: Request):
|
|
roles = infer_roles(body.actor_id, body.token_type, body.roles)
|
|
token, expires_in = issue_token(body.actor_id, body.token_type, roles)
|
|
trace_id = getattr(request.state, "trace_id", "unknown")
|
|
data = LoginResponseData(
|
|
access_token=token,
|
|
expires_in=expires_in,
|
|
sub=body.actor_id,
|
|
roles=roles,
|
|
)
|
|
return ok(data.model_dump(), trace_id, message="login ok")
|