fix(auth): 修改密码当新密码与旧密码相同时弹出错误,不再退出登录
This commit is contained in:
1 parent
9570197378
commit
0f8b318458
18 files changed
+247
-18
No files matched your search
@@ -101,7 +101,7 @@ def login():
|
||||
|
||||
@auth_bp.post('/change-password', endpoint='change_password')
|
||||
def change_password():
|
||||
"""校验当前身份并同步更新本地与 Casdoor 密码。"""
|
||||
"""校验当前身份并同步更新本地与 Casdoor 密码;新密码与原密码相同直接 400。"""
|
||||
user = _compat._get_current_user()
|
||||
if not user:
|
||||
raise UnauthorizedError('Unauthorized')
|
||||
@@ -110,6 +110,12 @@ def change_password():
|
||||
new_password = data.get('new_password')
|
||||
if not current_password or not new_password:
|
||||
raise ValidationError('Current and new password are required')
|
||||
if current_password == new_password:
|
||||
raise AppError(
|
||||
code='AUTH.PASSWORD_UNCHANGED',
|
||||
message='New password must differ from the current password',
|
||||
http_status=400,
|
||||
)
|
||||
try:
|
||||
_compat._verify_current_password_for_change(user, current_password)
|
||||
_compat._sync_casdoor_password(user.username, current_password, new_password)
|
||||
|
||||
@@ -324,12 +324,15 @@ def _verify_current_password_for_change(user, current_password: str):
|
||||
raise
|
||||
|
||||
def _sync_casdoor_password(username: str, current_password: str, new_password: str):
|
||||
"""将用户自助改密同步到 Casdoor;新密码未变化原样抛出,原密码错误转为 403。"""
|
||||
if not _is_casdoor_configured():
|
||||
return
|
||||
user_owner = _casdoor_user_owner()
|
||||
try:
|
||||
_casdoor_set_password(user_owner, username, current_password, new_password)
|
||||
except AppError as first_error:
|
||||
if first_error.code == 'AUTH.PASSWORD_UNCHANGED':
|
||||
raise
|
||||
if first_error.code in ('CASDOOR_INVALID_CREDENTIALS', 'AUTH.INCORRECT_CURRENT_PASSWORD'):
|
||||
_raise_incorrect_current_password()
|
||||
raise
|
||||
|
||||
@@ -235,6 +235,22 @@ def create_user(data: dict[str, Any]) -> dict:
|
||||
|
||||
|
||||
def update_user(user_id: int, data: dict[str, Any]) -> dict:
|
||||
"""
|
||||
更新管理端用户资料,可选同步 Casdoor 改密。
|
||||
|
||||
核心逻辑:先写入本地字段;新密码与当前哈希相同则直接拒绝,避免 Casdoor 返回含 old password 的文案被误判成 401。
|
||||
|
||||
Args:
|
||||
user_id: 目标用户主键。
|
||||
data: 管理端提交的字段,password 为空表示不改密。
|
||||
|
||||
Returns:
|
||||
{'status': 'ok'}。
|
||||
|
||||
Raises:
|
||||
NotFoundError: 用户不存在。
|
||||
AppError: 邮箱非法、新密码未变化或 Casdoor 同步失败。
|
||||
"""
|
||||
user = db.session.get(User, user_id)
|
||||
if not user:
|
||||
raise NotFoundError('User not found')
|
||||
@@ -259,6 +275,12 @@ def update_user(user_id: int, data: dict[str, Any]) -> dict:
|
||||
if 'feature_permissions' in data or 'role' in data:
|
||||
permissions_payload = data.get('feature_permissions', user.feature_permissions)
|
||||
user.set_feature_permissions(permissions_payload)
|
||||
if new_password and user.check_password(new_password):
|
||||
raise AppError(
|
||||
code='AUTH.PASSWORD_UNCHANGED',
|
||||
message='New password must differ from the current password',
|
||||
http_status=400,
|
||||
)
|
||||
if new_password:
|
||||
user.set_password(new_password)
|
||||
if 'agent_ids' in data:
|
||||
|
||||
@@ -3,7 +3,7 @@ backend/app/services/auth/hxops/casdoor_user_sync.py
|
||||
|
||||
用途:集中实现 HXOPS 用户创建和改密到 Casdoor 的管理端同步。
|
||||
依赖:Flask 配置、requests、HXOPS User 模型、公共 Casdoor 错误解析。
|
||||
逻辑:管理端事务调用本模块;HTTP 失败映射为稳定 AppError,由上层决定回滚。
|
||||
逻辑:管理端事务调用本模块;set-password 失败映射为稳定业务码(相同密码/原密码错误不用 401)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -111,13 +111,57 @@ def raise_update_user_error(source) -> None:
|
||||
)
|
||||
|
||||
|
||||
def is_same_password_error(message: str) -> bool:
|
||||
"""判断 Casdoor 是否因新密码与当前密码相同而拒绝改密。"""
|
||||
text = message or ''
|
||||
lowered = text.lower()
|
||||
if any(
|
||||
token in lowered
|
||||
for token in (
|
||||
'cannot be the same',
|
||||
'must be different',
|
||||
'same as the old',
|
||||
'same as old password',
|
||||
'identical to the old',
|
||||
'identical to old',
|
||||
'password unchanged',
|
||||
'new password is the same',
|
||||
)
|
||||
):
|
||||
return True
|
||||
if 'new password' in lowered and 'old password' in lowered and 'same' in lowered:
|
||||
return True
|
||||
return '新密码' in text and '旧密码' in text and ('相同' in text or '一样' in text)
|
||||
|
||||
|
||||
def raise_password_unchanged(source=None) -> None:
|
||||
"""新密码与当前密码相同按业务校验失败返回,禁止使用 401 以免前端清会话。"""
|
||||
data = extract_error_payload(source) if source is not None else {}
|
||||
msg_text = clean_str(data.get('msg') or data.get('error_description') or data.get('message'))
|
||||
raise AppError(
|
||||
code='AUTH.PASSWORD_UNCHANGED',
|
||||
message='New password must differ from the current password',
|
||||
http_status=400,
|
||||
details={'technical': msg_text[:300]} if msg_text else {},
|
||||
)
|
||||
|
||||
|
||||
def raise_set_password_error(source) -> None:
|
||||
"""将 Casdoor set-password 失败映射为稳定业务错误码。"""
|
||||
"""将 Casdoor set-password 失败映射为稳定业务错误码,改密业务失败不使用会话 401。"""
|
||||
data = extract_error_payload(source)
|
||||
msg_text = clean_str(data.get('msg') or data.get('error_description') or data.get('message'))
|
||||
lowered = (msg_text or str(source or '')).lower()
|
||||
raw_text = msg_text or str(source or '')
|
||||
lowered = raw_text.lower()
|
||||
# [STEP 1] 先识别“新密码与旧密码相同”。Casdoor 文案含 old password,若后判会被误成原密码错误 401。
|
||||
if is_same_password_error(raw_text):
|
||||
raise_password_unchanged(source)
|
||||
# [STEP 2] 原密码错误:业务 400。401 会被前端全局 fetch 包装当成登录失效并踢回登录页。
|
||||
if is_password_error(lowered) or 'old password' in lowered or 'current password' in lowered:
|
||||
raise AppError(code='CASDOOR_INVALID_CREDENTIALS', message='Incorrect current password', http_status=401)
|
||||
raise AppError(
|
||||
code='AUTH.INCORRECT_CURRENT_PASSWORD',
|
||||
message='Incorrect current password',
|
||||
http_status=400,
|
||||
)
|
||||
if any(token in lowered for token in ('too short', 'too weak', 'password policy', 'at least')):
|
||||
raise AppError(code='CASDOOR_PASSWORD_POLICY', message='New password does not meet security requirements', http_status=400)
|
||||
if is_user_missing_error(lowered):
|
||||
|
||||
Reference in new issue
Block a user