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,57 @@
|
||||
"""
|
||||
backend/app/services/auth/hxops/user_resolver.py
|
||||
|
||||
用途:将统一 AuthIdentity 映射为 HXOPS 本地 User。
|
||||
依赖:Flask 日志、SQLAlchemy session、User、AuthIdentity。
|
||||
逻辑:当前阶段保持原用户名优先、邮箱回退规则,并统一检查本地用户状态。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.extensions import db
|
||||
from app.models import User
|
||||
from app.services.auth.common import AuthIdentity
|
||||
|
||||
|
||||
class HxopsIdentityMappingError(Exception):
|
||||
"""HXOPS 本地用户无法映射或状态不允许登录。"""
|
||||
|
||||
def __init__(self, message: str, *, code: str, status: int):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
self.status = status
|
||||
|
||||
|
||||
def resolve_hxops_user(identity: AuthIdentity) -> User:
|
||||
"""
|
||||
按现有规则将认证身份映射到 HXOPS 用户。
|
||||
|
||||
Args:
|
||||
identity: 已验证 Casdoor issuer/subject 的统一身份。
|
||||
|
||||
Returns:
|
||||
状态为 active 的 HXOPS User。
|
||||
|
||||
Raises:
|
||||
HxopsIdentityMappingError: 用户未同步到 HXOPS 或账号已禁用。
|
||||
"""
|
||||
current_app.logger.info(
|
||||
'[casdoor_login] identity username=%s email=%s subject=%s',
|
||||
identity.username,
|
||||
identity.email,
|
||||
identity.subject,
|
||||
)
|
||||
user = db.session.query(User).filter_by(username=identity.username).first()
|
||||
if user is None and identity.email:
|
||||
user = db.session.query(User).filter_by(email=identity.email).first()
|
||||
if user is None:
|
||||
raise HxopsIdentityMappingError(
|
||||
'No local user matched Casdoor identity. Please contact admin to provision account.',
|
||||
code='CASDOOR_USER_NOT_FOUND',
|
||||
status=403,
|
||||
)
|
||||
if user.status != 'active':
|
||||
raise HxopsIdentityMappingError('Account is disabled', code='ACCOUNT_DISABLED', status=403)
|
||||
return user
|
||||
Reference in new issue
Block a user