66 lines
3.0 KiB
Python
66 lines
3.0 KiB
Python
"""System Admin authentication over the shared HTTP transport."""
|
|||
|
|
|
||
|
|
import base64
|
||
|
|
import getpass
|
||
|
|
import json
|
||
|
|
import time
|
||
|
|
|
||
|
|
from .transport import KEYS, SECRET_FIELDS, Client, ClientError, base_url, compact, session_lock
|
||
|
|
|
||
|
|
|
||
|
|
LOGIN_PATH = "/login/account"
|
||
|
|
LOGOUT_PATH = "/login/outLogin"
|
||
|
|
|
||
|
|
|
||
|
|
class SystemClient(Client):
|
||
|
|
secret_fields = SECRET_FIELDS | {"secretkey", "accesskey", "accesskeyid", "accesskeysecret"}
|
||
|
|
|
||
|
|
def __init__(self, env_file, timeout):
|
||
|
|
super().__init__("system", env_file, timeout)
|
||
|
|
|
||
|
|
def configured_base(self, settings, explicit=None):
|
||
|
|
address = explicit or settings.get(KEYS[0])
|
||
|
|
if not address or not address.strip():
|
||
|
|
raise ClientError("首次登录 System 请通过 --base-url 指定目标后端地址。")
|
||
|
|
return base_url(address, self.app)
|
||
|
|
|
||
|
|
def login(self, explicit_base=None):
|
||
|
|
address = self.login_base(explicit_base)
|
||
|
|
username = input("System 管理员账号: ")
|
||
|
|
password = getpass.getpass("System 管理员密码: ")
|
||
|
|
self.secrets.append(password)
|
||
|
|
payload = compact({"username": username, "password": password, "type": "account"}).encode("utf-8")
|
||
|
|
response = self.send(address, LOGIN_PATH, payload)
|
||
|
|
details = {key: response[key] for key in ("httpStatus", "code", "traceId")}
|
||
|
|
data = (response["json"] or {}).get("data") if self.success(response) else None
|
||
|
|
if not isinstance(data, dict) or data.get("status") != "ok":
|
||
|
|
raise ClientError("System 登录失败,请核对实例与管理员账号。", **details)
|
||
|
|
token = data.get("token")
|
||
|
|
if not isinstance(token, str) or not token:
|
||
|
|
raise ClientError("System 登录响应缺少 Token。", **details)
|
||
|
|
self.secrets.append(token)
|
||
|
|
try:
|
||
|
|
_, encoded, _ = token.split(".")
|
||
|
|
encoded += "=" * (-len(encoded) % 4)
|
||
|
|
claims = json.loads(base64.b64decode(encoded, altchars=b"-_", validate=True))
|
||
|
|
expires_at = claims.get("exp") if isinstance(claims, dict) else None
|
||
|
|
except ValueError:
|
||
|
|
expires_at = None
|
||
|
|
if type(expires_at) is not int or expires_at <= time.time():
|
||
|
|
raise ClientError("System 登录响应的 Token 到期时间无效。", **details)
|
||
|
|
|
||
|
|
with session_lock(self.env, self.timeout):
|
||
|
|
self.login_base(address)
|
||
|
|
self.write_session({KEYS[0]: address, KEYS[1]: token, KEYS[3]: str(expires_at)})
|
||
|
|
return {"ok": True, **details, "message": "已保存本机 System 会话。"}
|
||
|
|
|
||
|
|
def logout(self):
|
||
|
|
with session_lock(self.env, self.timeout):
|
||
|
|
session = self.read_session()
|
||
|
|
response = self.send(session[KEYS[0]], LOGOUT_PATH, b"{}", session[KEYS[1]])
|
||
|
|
details = {key: response[key] for key in ("httpStatus", "code", "traceId")}
|
||
|
|
if not self.success(response):
|
||
|
|
raise ClientError("System 退出失败,本机会话文件尚未更新。", **details)
|
||
|
|
self.clear_session(details)
|
||
|
|
return {"ok": True, **details}
|