feat: 增加管理后台的会话、配置和用户管理
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""进程内只保留当前管理员的用户名和密码密文。
|
||||
|
||||
不缓存明文密码,也不缓存 e 学用户、登录凭证或检查结果。
|
||||
启动时从设置表加载,修改用户名或密码后同步。重启会重新加载。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
from euniv.crypto import decrypt_secret
|
||||
|
||||
|
||||
class AdminIdentity:
|
||||
def __init__(self) -> None:
|
||||
self._username = ""
|
||||
self._password_cipher = ""
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def replace(self, username: str, password_cipher: str) -> None:
|
||||
with self._lock:
|
||||
self._username = username
|
||||
self._password_cipher = password_cipher
|
||||
|
||||
def matches(self, account: str) -> bool:
|
||||
with self._lock:
|
||||
username = self._username
|
||||
return bool(username) and account == username
|
||||
|
||||
def password_ok(self, password: str) -> bool:
|
||||
with self._lock:
|
||||
cipher = self._password_cipher
|
||||
if not cipher:
|
||||
return False
|
||||
try:
|
||||
return decrypt_secret(cipher) == password
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def username(self) -> str:
|
||||
with self._lock:
|
||||
return self._username
|
||||
|
||||
def password_cipher(self) -> str:
|
||||
with self._lock:
|
||||
return self._password_cipher
|
||||
|
||||
|
||||
identity = AdminIdentity()
|
||||
Reference in New Issue
Block a user