Files
xiaoxia 53fb25efcf
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 2s
CI/CD Pipeline / Check push changed paths (push) Successful in 19s
CI/CD Pipeline / Build Staging API Image (push) Successful in 41s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 48s
CI/CD Pipeline / Integration Tests (push) Successful in 3m10s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 3m17s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 3m30s
CI/CD Pipeline / Validate - Style (push) Successful in 4m17s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 59s
CI/CD Pipeline / Frontend Unit Tests (push) Failing after 5m33s
CI/CD Pipeline / Validate - Security (push) Successful in 7m12s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m38s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 3m13s
CI/CD Pipeline / Unit Tests (push) Successful in 10m11s
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Failing after 26h14m3s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 26h24m21s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 26h19m47s
CI/CD Pipeline / PR Build Web Image (push) Failing after 26h23m44s
CI/CD Pipeline / PR Build API Image (push) Failing after 26h23m44s
CI/CD Pipeline / Deploy Production (push) Failing after 26h13m23s
CI/CD Pipeline / Build Production Web Image (push) Failing after 26h13m26s
CI/CD Pipeline / CI Gate (push) Failing after 26h13m25s
CI/CD Pipeline / Build Production API Image (push) Failing after 26h13m26s
CI/CD Pipeline / Canary Release to Production (push) Failing after 26h13m23s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 26h19m46s
CI/CD Pipeline / Frontend Lint (push) Failing after 26h23m37s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 26h23m45s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 26h19m46s
fix(#1834): 批量修复 UP 系列静态分析警告(UP007/UP006/UP017/UP035) (#1928)
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com>
Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
2026-09-15 12:59:17 +08:00

138 lines
3.6 KiB
Python

"""
JWT 处理器委托层
此模块作为 Domain 层 (packages.domain.auth.jwt_service) 和 Application 层之间的委托层,
隔离 Domain 层对 jwt 库的直接依赖。
使用方式:
from packages.application.auth.jwt_handler import JWTHandler, get_jwt_handler
jwt_handler = JWTHandler(secret_key="<YOUR_SECRET_KEY>")
token = jwt_handler.create_access_token(user_id="user123", role="admin")
payload = jwt_handler.verify_access_token(token)
"""
from typing import Any, Optional
from packages.application.auth.jwt_service import JWTConfig, JWTService
class JWTHandler:
"""
JWT 处理器委托类
委托给 packages.domain.auth.jwt_service.JWTService 进行实际的 JWT 操作,
此层仅负责配置和封装,不直接依赖 jwt 库。
"""
def __init__(self, secret_key: str, algorithm: str = "HS256", access_token_expire_minutes: int = 30):
"""
初始化 JWT 处理器
Args:
secret_key: JWT 签名密钥(必须从环境变量或配置注入)
algorithm: 加密算法,默认 HS256
access_token_expire_minutes: Access Token 过期时间(分钟)
"""
config = JWTConfig(
secret_key=secret_key,
algorithm=algorithm,
access_token_expire_minutes=access_token_expire_minutes,
)
self._service = JWTService(config)
def create_access_token(
self,
user_id: str,
role: str = "",
additional_claims: Optional[dict[str, Any]] = None,
) -> str:
"""
创建 access_token
Args:
user_id: 用户 ID
role: 用户角色
additional_claims: 额外的声明信息
Returns:
JWT Token 字符串
"""
return self._service.create_access_token(
user_id=user_id,
role=role,
additional_claims=additional_claims,
)
def verify_access_token(self, token: str) -> dict[str, Any]:
"""
验证 access_token
Args:
token: JWT Token 字符串
Returns:
Token payload
Raises:
ExpiredSignatureError: Token 已过期
ValueError: Token 类型不是 access
"""
return self._service.verify_access_token(token)
def verify_token(self, token: str) -> dict[str, Any]:
"""
验证任意 Token
Args:
token: JWT Token 字符串
Returns:
Token payload
"""
return self._service.verify_token(token)
# 默认处理器实例(需要通过 configure_jwt_handler 配置)
_default_handler: Optional[JWTHandler] = None
def configure_jwt_handler(
secret_key: str,
algorithm: str = "HS256",
access_token_expire_minutes: int = 30,
) -> JWTHandler:
"""
配置全局 JWT 处理器
Args:
secret_key: JWT 签名密钥
algorithm: 加密算法
access_token_expire_minutes: Access Token 过期时间(分钟)
Returns:
配置好的 JWTHandler 实例
"""
global _default_handler
_default_handler = JWTHandler(
secret_key=secret_key,
algorithm=algorithm,
access_token_expire_minutes=access_token_expire_minutes,
)
return _default_handler
def get_jwt_handler() -> JWTHandler:
"""
获取全局 JWT 处理器
Returns:
JWTHandler 实例
Raises:
RuntimeError: 如果尚未配置 JWT 处理器
"""
if _default_handler is None:
raise RuntimeError("JWT handler not configured. Call configure_jwt_handler() first.")
return _default_handler