会员服务改为固定栏目,首页在线服务不再进入报告审核。估价模版智能体可多会话制作模版并下载,下载过程显示加载状态。 Co-authored-by: Cursor <cursoragent@cursor.com>
45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from typing import Literal
|
|
|
|
import jwt
|
|
from fastapi import HTTPException, status
|
|
|
|
from app.core.config import get_settings
|
|
|
|
TokenType = Literal["staff", "member", "agent"]
|
|
|
|
|
|
def create_access_token(subject: str, token_type: TokenType, *, version: int | None = None) -> str:
|
|
settings = get_settings()
|
|
if len(settings.jwt_secret) < 32:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="鉴权未配置",
|
|
)
|
|
minutes = (
|
|
settings.jwt_expire_minutes
|
|
if token_type == "staff"
|
|
else settings.jwt_member_expire_minutes
|
|
)
|
|
now = datetime.now(timezone.utc)
|
|
payload = {"sub": subject, "typ": token_type, "exp": now + timedelta(minutes=minutes), "iat": now}
|
|
if version is not None:
|
|
payload["ver"] = int(version)
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm="HS256")
|
|
|
|
|
|
def decode_access_token(token: str, expected_type: TokenType) -> dict:
|
|
settings = get_settings()
|
|
if len(settings.jwt_secret) < 32:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="鉴权未配置",
|
|
)
|
|
try:
|
|
payload = jwt.decode(token, settings.jwt_secret, algorithms=["HS256"])
|
|
except jwt.PyJWTError:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="登录已失效")
|
|
if payload.get("typ") != expected_type or not payload.get("sub"):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="无权访问该接口")
|
|
return payload
|