新增“数据编辑”

This commit is contained in:
2025-11-10 09:31:54 +08:00
parent aba94c074a
commit 61b1d93718
3 changed files with 34 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
import base64
import hashlib
from elastic.es_connect import get_user_by_username as es_get_user_by_username
def _salt_for_username(username: str) -> bytes:
@@ -12,27 +13,21 @@ def _derive_password(password_plain: str, salt: bytes) -> bytes:
def get_user_by_username(username: str):
"""
Placeholder for ES lookup. Returns fixed JSON for a demo user.
In production this should query ES with the given mapping.
Demo user:
- username: admin
- password: Password123! (stored as PBKDF2-derived secret only)
- user_id: 1
- premission: 0 (admin)
从Elasticsearch获取用户数据
"""
if username != 'admin':
return None
# 首先尝试从ES获取用户数据
es_user = es_get_user_by_username(username)
salt = _salt_for_username(username)
# Demo: derive and store secret from a known password for the placeholder
derived = _derive_password('Password123!', salt)
return {
'user_id': 1,
'username': 'admin',
# Store only the derived secret, not the plaintext password
'password': base64.b64encode(derived).decode('ascii'),
'premission': 0,
# Expose salt to the client during challenge so both sides derive consistently
'salt': base64.b64encode(salt).decode('ascii'),
}
derived = _derive_password(es_user.get('password', ''), salt)
if es_user:
# 如果ES中有用户数据使用ES中的密码
return {
'user_id': es_user.get('user_id', 0),
'username': es_user.get('username', ''),
'password': base64.b64encode(derived).decode('ascii'),
'premission': es_user.get('permission', 1),
'salt': base64.b64encode(salt).decode('ascii'),
}
return None