feat: 新增 lightcore-cli 命令行工具项目
新增完整的 lightcore-cli 命令行工具代码,包含: - CLI 入口与参数解析 - System 和 Media 应用的客户端实现 - 丰富的子命令与操作接口 - 测试用例与项目配置 - .gitignore 与 README 文档
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"""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}
|
||||
Reference in New Issue
Block a user