feat(auth): 重构统一认证并支持平级服务无感登录
This commit is contained in:
1 parent
d79def5519
commit
ca312ea751
64 files changed
+3713
-449
No files matched your search
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
backend/app/services/auth/casdoor/config.py
|
||||
|
||||
用途:定义纯 Casdoor 客户端配置及 HXOPS Flask 配置适配器。
|
||||
依赖:dataclasses、flask.current_app、app.core.errors。
|
||||
逻辑:公共客户端只接收 CasdoorConfig;不同服务只需替换配置适配器。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.core.errors import AppError
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CasdoorConfig:
|
||||
"""Casdoor OAuth/OIDC 客户端运行参数,secret 不参与 repr。"""
|
||||
|
||||
base_url: str
|
||||
client_id: str
|
||||
client_secret: str
|
||||
issuer: str
|
||||
scope: str = 'openid profile email'
|
||||
timeout_seconds: int = 8
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""输出可诊断但不包含 client_secret 的配置摘要。"""
|
||||
return (
|
||||
'CasdoorConfig('
|
||||
f'base_url={self.base_url!r}, client_id={self.client_id!r}, '
|
||||
f'issuer={self.issuer!r}, scope={self.scope!r}, '
|
||||
f'timeout_seconds={self.timeout_seconds!r})'
|
||||
)
|
||||
|
||||
|
||||
def casdoor_config_from_flask() -> CasdoorConfig:
|
||||
"""
|
||||
从当前 Flask 应用读取并校验 Casdoor 客户端配置。
|
||||
|
||||
Returns:
|
||||
可交给 CasdoorClient 的不可变配置。
|
||||
|
||||
Raises:
|
||||
AppError: 基础地址、客户端凭据或 issuer 未配置。
|
||||
"""
|
||||
base_url = str(current_app.config.get('CASDOOR_BASE_URL') or '').strip().rstrip('/')
|
||||
client_id = str(current_app.config.get('CASDOOR_CLIENT_ID') or '').strip()
|
||||
client_secret = str(current_app.config.get('CASDOOR_CLIENT_SECRET') or '')
|
||||
issuer = str(current_app.config.get('AUTH_JWT_ISSUER') or '').strip().rstrip('/')
|
||||
scope = str(current_app.config.get('CASDOOR_SCOPE') or 'openid profile email').strip()
|
||||
timeout_seconds = int(current_app.config.get('CASDOOR_HTTP_TIMEOUT_SECONDS') or 8)
|
||||
if not base_url or not client_id or not client_secret or not issuer:
|
||||
raise AppError(
|
||||
code='CASDOOR_CONFIG_ERROR',
|
||||
message='Authentication service is not configured. Please contact the administrator.',
|
||||
http_status=503,
|
||||
)
|
||||
return CasdoorConfig(
|
||||
base_url=base_url,
|
||||
client_id=client_id,
|
||||
client_secret=client_secret,
|
||||
issuer=issuer,
|
||||
scope=scope or 'openid profile email',
|
||||
timeout_seconds=max(1, timeout_seconds),
|
||||
)
|
||||
Reference in new issue
Block a user