Merge branch 'master' of http://192.168.0.200/knowledge-base-bj/deerflow-server into dhk-1
This commit is contained in:
commit
44d7304f8e
14 files changed
+436
-49
No files matched your search
@@ -31,6 +31,7 @@ class User(BaseModel):
|
||||
needs_setup: bool = Field(default=False, description="True for auto-created admin until setup completes")
|
||||
token_version: int = Field(default=0, description="Incremented on password change to invalidate old JWTs")
|
||||
approved: bool = Field(default=True, description="登录白名单放行标记(黑名单模式);仅当系统设置 login_whitelist.enabled 打开时拦截被取消放行的用户。新账号默认放行(True),管理员可「取消放行」(False)封禁个别用户。")
|
||||
login_password: str | None = Field(default=None, description="管理员在白名单管理页给该账号设置的「登录口令」(明文,仅 admin 可见)。非空 → 该账号用户名直登必须带匹配的 ?password=;为空 → 走原有共享口令门/免口令逻辑。")
|
||||
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
|
||||
@@ -47,6 +47,7 @@ class SQLiteUserRepository(UserRepository):
|
||||
needs_setup=row.needs_setup,
|
||||
token_version=row.token_version,
|
||||
approved=row.approved,
|
||||
login_password=row.login_password,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@@ -62,6 +63,7 @@ class SQLiteUserRepository(UserRepository):
|
||||
needs_setup=user.needs_setup,
|
||||
token_version=user.token_version,
|
||||
approved=user.approved,
|
||||
login_password=user.login_password,
|
||||
)
|
||||
|
||||
# ── CRUD ──────────────────────────────────────────────────────────
|
||||
@@ -157,6 +159,7 @@ class SQLiteUserRepository(UserRepository):
|
||||
row.needs_setup = user.needs_setup
|
||||
row.token_version = user.token_version
|
||||
row.approved = user.approved
|
||||
row.login_password = user.login_password
|
||||
await session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ from app.gateway.auth import (
|
||||
UserResponse,
|
||||
create_access_token,
|
||||
)
|
||||
from app.gateway.auth.models import User
|
||||
from app.gateway.auth.config import get_auth_config
|
||||
from app.gateway.auth.errors import AuthErrorCode, AuthErrorResponse
|
||||
from app.gateway.csrf_middleware import is_secure_request
|
||||
@@ -279,6 +280,25 @@ async def _get_or_create_user_by_username(username: str):
|
||||
return user, created
|
||||
|
||||
|
||||
async def _lookup_user_by_username(username: str) -> User | None:
|
||||
"""Resolve a username to an **existing** local user, without creating one.
|
||||
|
||||
Mirrors the lookup half of ``_get_or_create_user_by_username`` (canonical
|
||||
``<username>@<domain>`` email + legacy local-part fallback) so the
|
||||
per-account 登录口令 gate can read the target account's ``login_password``
|
||||
before deciding whether to admit the login. Returns ``None`` for a username
|
||||
that has never registered (the personal-password gate then simply does not
|
||||
apply, and ``_get_or_create_user_by_username`` later auto-registers it).
|
||||
"""
|
||||
provider = get_local_provider()
|
||||
email = _username_to_email(username)
|
||||
local_part = _normalize_username_local_part(username)
|
||||
user = await provider.get_user_by_email(email)
|
||||
if user is None and local_part is not None:
|
||||
user = await provider.find_user_by_email_local_part(local_part)
|
||||
return user
|
||||
|
||||
|
||||
def enforce_user_approved(user) -> None:
|
||||
"""登录白名单拦截(**黑名单模式**):开关打开且用户被「取消放行」时拒绝登录。
|
||||
|
||||
@@ -566,16 +586,40 @@ async def login_by_username(
|
||||
- If mapped account exists, sign in directly (no password required).
|
||||
- If account does not exist, create a regular user and sign in.
|
||||
|
||||
Password gate: when ``auth_login.username_login.require_password`` is
|
||||
enabled, callers must additionally pass ``?password=`` matching the
|
||||
configured shared secret (default ``123ewq`` when unset). The gate is a
|
||||
single shared password — it does not change which account is logged in.
|
||||
Password gates (checked in this order):
|
||||
|
||||
1. **Per-account 登录口令** — if an admin assigned this specific account a
|
||||
personal password (``users.login_password``, set on the 白名单管理 page),
|
||||
the caller must pass ``?password=`` exactly matching it. This takes
|
||||
precedence over the shared gate and is the「临时给某账号开通+设新口令」flow.
|
||||
2. **Shared gate** — otherwise, when
|
||||
``auth_login.username_login.require_password`` is enabled, callers must
|
||||
pass ``?password=`` matching the configured shared secret (default
|
||||
``123ewq`` when unset). A single shared password; does not change which
|
||||
account is logged in.
|
||||
|
||||
Returns the JWT access token in the response body and also sets it as
|
||||
a session cookie for browser callers.
|
||||
"""
|
||||
username_login_config = get_app_config().auth_login.username_login
|
||||
if username_login_config.require_password:
|
||||
|
||||
# Per-account password takes precedence: look the account up (no create) and,
|
||||
# if it carries a personal 登录口令, require an exact match — ignoring the
|
||||
# shared gate entirely for that account.
|
||||
existing = await _lookup_user_by_username(body.username)
|
||||
personal_password = (getattr(existing, "login_password", None) or "").strip() if existing else ""
|
||||
|
||||
if personal_password:
|
||||
client_ip = _get_client_ip(request)
|
||||
_check_rate_limit(client_ip)
|
||||
if (password or "") != personal_password:
|
||||
_record_login_failure(client_ip)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="口令错误").model_dump(),
|
||||
)
|
||||
_record_login_success(client_ip)
|
||||
elif username_login_config.require_password:
|
||||
client_ip = _get_client_ip(request)
|
||||
_check_rate_limit(client_ip)
|
||||
if password != username_login_config.effective_password:
|
||||
@@ -830,6 +874,7 @@ class UserListItem(BaseModel):
|
||||
agent_count: int
|
||||
qa_count: int = 0 # 问答次数: number of runs (one run = one question)
|
||||
approved: bool = True # 登录白名单放行标记(白名单管理页据此放行/取消)
|
||||
login_password: str | None = None # 账号级登录口令(明文,管理员可见/可复制登录链接)
|
||||
|
||||
|
||||
@router.get("/users", response_model=list[UserListItem])
|
||||
@@ -863,6 +908,7 @@ async def list_all_users(
|
||||
agent_count=await agent_store.count_owned(uid),
|
||||
qa_count=qa_counts.get(uid, 0),
|
||||
approved=getattr(u, "approved", True),
|
||||
login_password=getattr(u, "login_password", None),
|
||||
)
|
||||
)
|
||||
return result
|
||||
@@ -897,6 +943,48 @@ async def set_user_approval(user_id: str, body: UserApprovalRequest, request: Re
|
||||
agent_count=0,
|
||||
qa_count=0,
|
||||
approved=saved.approved,
|
||||
login_password=saved.login_password,
|
||||
)
|
||||
|
||||
|
||||
class UserLoginPasswordRequest(BaseModel):
|
||||
password: str | None = Field(
|
||||
default=None,
|
||||
max_length=255,
|
||||
description="设为非空 → 该账号用户名直登需带此明文口令;传 null/空字符串 → 清除该口令(回到共享口令门/免口令)。",
|
||||
)
|
||||
|
||||
|
||||
@router.put("/users/{user_id}/login-password", response_model=UserListItem)
|
||||
async def set_user_login_password(user_id: str, body: UserLoginPasswordRequest, request: Request) -> UserListItem:
|
||||
"""设置 / 清除某个账号的「登录口令」(明文)。Admin only。
|
||||
|
||||
设置后,该账号的用户名直登(``/login/username``)必须带匹配的 ``?password=``;
|
||||
管理员据返回的明文口令拼出登录链接 ``…/login/<用户名>?password=xxxx`` 发给用户。
|
||||
不动 ``token_version``,现有会话不受影响(仅影响下次登录)。
|
||||
"""
|
||||
admin = await get_current_user_from_request(request)
|
||||
if admin.system_role != "admin":
|
||||
raise HTTPException(status_code=403, detail="Admin only")
|
||||
|
||||
provider = get_local_provider()
|
||||
target = await provider.get_user(user_id)
|
||||
if target is None:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
|
||||
# 空字符串 / null / 纯空白 → 清除(None);否则存去掉首尾空白后的明文。
|
||||
pwd = (body.password or "").strip()
|
||||
target.login_password = pwd or None
|
||||
saved = await provider.update_user(target)
|
||||
return UserListItem(
|
||||
id=str(saved.id),
|
||||
email=saved.email,
|
||||
system_role=saved.system_role,
|
||||
skill_count=0,
|
||||
agent_count=0,
|
||||
qa_count=0,
|
||||
approved=saved.approved,
|
||||
login_password=saved.login_password,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user