feat(app-iced): M114 账户页三轨登录(微软正版 / 第三方皮肤站 / 本地档案)
- 账户页:当前账户卡 + 全部账户列表 + 空态三入口 + 皮肤区;五项动作(添加/切换/删除/重新登录/校验) - 三轨添加流程:设备码(用户码+链接+倒计时+过期重试+取消)、授权码(浏览器+可复制链接+取消)、 本地档案(名字校验)、第三方站(服务地址 + 服务发现校验 + 密码登录,密码掩码且用完即弃) - 导航:账户为一等页,入口是轨底头像位(未登录=描边剪影 / 已登录=首字母块+名字;窄高收成图标条) - 凭据红线:DTO 侧形状脱敏 + 状态层二次脱敏 + 密码掩码;Inject-JWT 测试断言视图模型干净(已做变异取证) - presenter:authlib 登录/服务发现/皮肤抓取(复用下载引擎的 HTTP 客户端,无新增依赖)、 皮肤原图经 DTO 到 UI 裁剪成头像(既有 image 依赖,未改 Cargo.toml) - i18n:新增 account 域 51 条中英文案(离线档案口径全量写入),金表同步
This commit is contained in:
1 parent
e7b04c4afe
commit
1ff00bbe50
43 files changed
+4343
-117
No files matched your search
@@ -3,6 +3,19 @@
|
||||
//! **凭据红线**:本模块(以及整个 `crate::dto`)只有 [`AccountView`] 这种公开字段视图。
|
||||
//! `core::auth` 的 `Credentials` / `MicrosoftSession` / token 永远不进 app 层——
|
||||
//! host 侧在拿到账户快照的那一刻就丢掉凭据,只留「能显示什么」。
|
||||
//!
|
||||
//! | 文件 | 职责 |
|
||||
//! |---|---|
|
||||
//! | 本文件 | 账户/登录**可见状态**(全部是可上屏字段) |
|
||||
//! | [`redact`] | 形状脱敏:JWT / 不透明串 / `token=...` 一律抹掉(第二道脱敏器) |
|
||||
//! | `tests` | 契约 round-trip + 凭据红线的入口断言 |
|
||||
|
||||
mod redact;
|
||||
#[cfg(test)]
|
||||
#[path = "accounts/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
pub use redact::{looks_like_jwt, redact, REDACTED};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -18,7 +31,7 @@ pub enum AccountKind {
|
||||
}
|
||||
|
||||
impl AccountKind {
|
||||
/// 徽标文字。
|
||||
/// 徽标文字(CLI/终端用;GUI 走 i18n 词表,不读这里的中文)。
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Offline => "离线",
|
||||
@@ -60,9 +73,63 @@ pub struct AccountView {
|
||||
/// 登录名(微软轨才有;可空)
|
||||
pub login_name: String,
|
||||
/// 创建时间展示串(`2026-10-31 20:12`)
|
||||
///
|
||||
/// 口径:core 的 `AccountMeta` 只有 `created_at_ms`,**没有 last-used**,所以列表上贴的是
|
||||
/// 「添加于 {created}」,不伪造「最近使用」(缺口登记在 M114 报告)。
|
||||
pub created: String,
|
||||
/// 是否是当前启动使用的账户
|
||||
pub active: bool,
|
||||
/// 第三方轨的认证服务器 API 根(其余轨为空串)
|
||||
#[serde(default)]
|
||||
pub server_url: String,
|
||||
/// 皮肤原图(第三方轨才可能有;`None` = 没有/没取到,UI 退回首字母块)
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub skin: Option<SkinImage>,
|
||||
}
|
||||
|
||||
impl AccountView {
|
||||
/// 首字母块上的字:展示名第一个非空字符(大写化),空名字回落 `?`。
|
||||
///
|
||||
/// 放在 DTO 而不是页面里,是为了「没有皮肤时画什么」这件事只有一处判据,两个 UI 版一致。
|
||||
#[must_use]
|
||||
pub fn monogram(&self) -> String {
|
||||
self.name
|
||||
.chars()
|
||||
.find(|c| !c.is_whitespace())
|
||||
.map(|c| c.to_uppercase().to_string())
|
||||
.unwrap_or_else(|| "?".to_string())
|
||||
}
|
||||
|
||||
/// 首字母块的取色索引(0..6):由 `profile_id` 稳定推导,同一个人每次都是同一个色块。
|
||||
#[must_use]
|
||||
pub fn monogram_tone(&self) -> u8 {
|
||||
let mut hash: u32 = 2_166_136_261;
|
||||
for byte in self.profile_id.as_bytes() {
|
||||
hash ^= u32::from(*byte);
|
||||
hash = hash.wrapping_mul(16_777_619);
|
||||
}
|
||||
u8::try_from(hash % 6).unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// 一张皮肤原图。
|
||||
///
|
||||
/// **停在「原图」这一层是刻意的**:presenter 不引入图片解码依赖(`presenter-no-ui` +
|
||||
/// 依赖极简),裁剪/缩放/取首字母块都由 UI 层自己做。离线轨永远没有它——没有皮肤就不给假皮肤。
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SkinImage {
|
||||
/// PNG 字节(Minecraft 皮肤:64×64 或旧版 64×32)
|
||||
pub png: Vec<u8>,
|
||||
/// 来源 URL(**只进取证日志,不上屏**)
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
impl SkinImage {
|
||||
/// 是否有可用字节(0 字节等同没有皮肤)。
|
||||
#[must_use]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.png.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// 微软登录阶段(UI 据此切换面板;两条流共用)。
|
||||
@@ -174,7 +241,7 @@ impl DeviceLoginView {
|
||||
expires_in.max(1)
|
||||
);
|
||||
if let Some(warning) = warning {
|
||||
message.push_str(&format!("({warning})"));
|
||||
message.push_str(&format!("({})", redact(warning)));
|
||||
}
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Waiting,
|
||||
@@ -187,11 +254,11 @@ impl DeviceLoginView {
|
||||
}
|
||||
}
|
||||
|
||||
/// 失败态。
|
||||
/// 失败态(文案在入口就脱敏:错误原文里可能带着服务端回显的 token)。
|
||||
pub fn failed(message: impl Into<String>) -> Self {
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Failed,
|
||||
message: message.into(),
|
||||
message: redact(&message.into()),
|
||||
..Self::new()
|
||||
}
|
||||
}
|
||||
@@ -208,63 +275,119 @@ impl DeviceLoginView {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
/// 第三方轨(authlib-injector)登录请求。
|
||||
///
|
||||
/// **凭据红线**:`password` 只是「用户这一次输入的请求值」,从页面流到 host 的
|
||||
/// `AuthlibAuth::authenticate` 调用栈里,host 取走即弃——不入账户库、不进日志、
|
||||
/// 不进任何回程 DTO。所以本类型只有一个方向(UI → host),回程只有 [`AuthlibLoginView`]。
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AuthlibRequest {
|
||||
/// 认证服务器地址(用户原样输入;host 侧 `normalize_api_root` 规范化)
|
||||
pub server: String,
|
||||
/// 登录名(邮箱或用户名)
|
||||
pub login: String,
|
||||
/// 密码(只在本调用栈里出现)
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kind_round_trips_through_stable_ids() {
|
||||
for kind in [
|
||||
AccountKind::Offline,
|
||||
AccountKind::Microsoft,
|
||||
AccountKind::Authlib,
|
||||
] {
|
||||
assert_eq!(AccountKind::parse(kind.as_str()), kind);
|
||||
assert!(!kind.label().is_empty());
|
||||
impl AuthlibRequest {
|
||||
/// 构造。
|
||||
pub fn new(
|
||||
server: impl Into<String>,
|
||||
login: impl Into<String>,
|
||||
password: impl Into<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
server: server.into(),
|
||||
login: login.into(),
|
||||
password: password.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_login_view_never_carries_tokens() {
|
||||
let view = DeviceLoginView::waiting("ABCD-EFGH", "https://www.microsoft.com/link", 900, "");
|
||||
assert_eq!(view.phase, DeviceLoginPhase::Waiting);
|
||||
assert!(view.phase.busy());
|
||||
assert!(view.qr_link.contains("ABCD-EFGH"));
|
||||
let json = serde_json::to_string(&view).expect("序列化");
|
||||
for forbidden in ["token", "secret", "password", "refresh"] {
|
||||
assert!(
|
||||
!json.to_lowercase().contains(forbidden),
|
||||
"视图里不该出现 {forbidden}"
|
||||
);
|
||||
/// 可打印形态(日志/诊断/错误路径用):**密码恒被抹掉**。
|
||||
#[must_use]
|
||||
pub fn redacted(&self) -> String {
|
||||
format!(
|
||||
"server={} login={} password={REDACTED}",
|
||||
redact(&self.server),
|
||||
redact(&self.login)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// 第三方轨登录的可见状态(阶段复用 [`DeviceLoginPhase`]:都是「申请 → 等待 → 成/败」)。
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AuthlibLoginView {
|
||||
/// 阶段
|
||||
pub phase: DeviceLoginPhase,
|
||||
/// 认证服务器地址(规范化后的 API 根)
|
||||
pub server: String,
|
||||
/// 服务发现里的服务器名(校验通过后才有;空 = 没校验/没名字)
|
||||
pub server_name: String,
|
||||
/// 状态/错误文案(已脱敏)
|
||||
pub message: String,
|
||||
/// 成功后入库的账户展示名
|
||||
pub account_name: String,
|
||||
}
|
||||
|
||||
impl Default for AuthlibLoginView {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthlibLoginView {
|
||||
/// 空闲态。
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Idle,
|
||||
server: String::new(),
|
||||
server_name: String::new(),
|
||||
message: String::new(),
|
||||
account_name: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 进行中(校验服务器地址 / 换 token)。
|
||||
pub fn running(server: &str, message: &str) -> Self {
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Requesting,
|
||||
server: server.to_string(),
|
||||
server_name: String::new(),
|
||||
message: redact(message),
|
||||
account_name: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 地址校验通过(服务发现拿到了服务器名)。
|
||||
pub fn reachable(server: &str, server_name: &str) -> Self {
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Waiting,
|
||||
server: server.to_string(),
|
||||
server_name: redact(server_name),
|
||||
message: format!("服务器可用:{}", server_name.trim()),
|
||||
account_name: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn browser_waiting_shows_a_copyable_link_instead_of_a_user_code() {
|
||||
let view = DeviceLoginView::browser_waiting(
|
||||
"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=x",
|
||||
300,
|
||||
Some("拉起浏览器失败"),
|
||||
);
|
||||
assert_eq!(view.phase, DeviceLoginPhase::Waiting);
|
||||
assert!(view.phase.busy());
|
||||
assert!(view.user_code.is_empty(), "授权码流没有用户码");
|
||||
assert_eq!(view.qr_link, view.verification_uri);
|
||||
assert!(view.message.contains("浏览器"));
|
||||
assert!(view.message.contains("拉起浏览器失败"), "{}", view.message);
|
||||
let json = serde_json::to_string(&view).expect("序列化");
|
||||
for forbidden in ["refresh", "mc-access"] {
|
||||
assert!(!json.contains(forbidden), "视图里不该出现 {forbidden}");
|
||||
/// 失败态(文案在入口就脱敏)。
|
||||
pub fn failed(message: impl Into<String>) -> Self {
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Failed,
|
||||
message: redact(&message.into()),
|
||||
..Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn succeeded_and_failed_clear_the_code() {
|
||||
let ok = DeviceLoginView::succeeded("Steve");
|
||||
assert_eq!(ok.phase, DeviceLoginPhase::Succeeded);
|
||||
assert!(ok.user_code.is_empty());
|
||||
let bad = DeviceLoginView::failed("网络超时");
|
||||
assert_eq!(bad.phase, DeviceLoginPhase::Failed);
|
||||
assert_eq!(bad.message, "网络超时");
|
||||
/// 成功态。
|
||||
pub fn succeeded(server_name: &str, account_name: impl Into<String>) -> Self {
|
||||
let account_name = account_name.into();
|
||||
Self {
|
||||
phase: DeviceLoginPhase::Succeeded,
|
||||
server_name: server_name.to_string(),
|
||||
message: format!("已登录:{account_name}"),
|
||||
account_name,
|
||||
..Self::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
//! 凭据红线的**形状脱敏**:把 JWT / 不透明 token / `token=...` 这类串从任何要上屏的文案里抹掉。
|
||||
//!
|
||||
//! ## 为什么还要第二道脱敏器
|
||||
//!
|
||||
//! `core::launch::redact` 是**按已知明文值**替换的(拿着会话里的 token 原串去抹)。这里要挡的是
|
||||
//! **没登记过的**字符串:服务端在错误响应里回显一个 token、HTTP 库把 header 拼进错误文案、
|
||||
//! 某段调试串被画到面板上。两者互补,缺一不可:
|
||||
//!
|
||||
//! | 场景 | `core::launch::redact` | 本模块 |
|
||||
//! |---|---|---|
|
||||
//! | 已知会话 token 出现在日志里 | ✅ | ❌(不知道明文) |
|
||||
//! | 服务端回包里的陌生 JWT 被画到错误面板 | ❌ | ✅ |
|
||||
//!
|
||||
//! 走形状识别(三段 base64url / 超长不透明串 / 敏感键名)就够拦住「一眼能看出是凭据」的串,
|
||||
//! 而普通文案(URL、用户码 `ABCD-EFGH`、服务器名)**一个字都不动**——脱敏器本身不能变成噪音源。
|
||||
|
||||
/// 脱敏占位符(**故意刺眼**:宁可难看,也不许静默把凭据画到屏幕上)。
|
||||
pub const REDACTED: &str = "⟪redacted⟫";
|
||||
|
||||
/// 把一段要上屏的文案里的凭据形状串抹掉(空白与其余字符**原样保留**)。
|
||||
#[must_use]
|
||||
pub fn redact(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for piece in text.split_inclusive(char::is_whitespace) {
|
||||
let split = piece.find(char::is_whitespace).unwrap_or(piece.len());
|
||||
let (chunk, space) = piece.split_at(split);
|
||||
out.push_str(&redact_chunk(chunk));
|
||||
out.push_str(space);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// 单个「词」的脱敏:① 敏感键名的值 ② 整个词本身就是凭据形状。
|
||||
fn redact_chunk(chunk: &str) -> String {
|
||||
if let Some((key, _value)) = chunk.split_once('=') {
|
||||
if is_sensitive_key(key) || looks_opaque(chunk) {
|
||||
return format!("{key}={REDACTED}");
|
||||
}
|
||||
}
|
||||
if looks_opaque(chunk) {
|
||||
return REDACTED.to_string();
|
||||
}
|
||||
chunk.to_string()
|
||||
}
|
||||
|
||||
/// 键名是否属于「它的值一定是凭据」(`accessToken` / `refresh_token` / `password` …)。
|
||||
fn is_sensitive_key(key: &str) -> bool {
|
||||
let key = key
|
||||
.trim_matches(|c: char| !c.is_alphanumeric() && c != '_' && c != '-')
|
||||
.to_lowercase();
|
||||
[
|
||||
"token",
|
||||
"secret",
|
||||
"password",
|
||||
"passwd",
|
||||
"authorization",
|
||||
"bearer",
|
||||
"session",
|
||||
]
|
||||
.iter()
|
||||
.any(|needle| key.contains(needle))
|
||||
}
|
||||
|
||||
/// 词的形状是否像凭据:① 三段 base64url(JWT)② 长度 ≥40 的不透明串。
|
||||
fn looks_opaque(chunk: &str) -> bool {
|
||||
let trimmed = chunk.trim_matches(|c: char| {
|
||||
!c.is_ascii_alphanumeric() && c != '_' && c != '-' && c != '.' && c != '+'
|
||||
});
|
||||
looks_like_jwt(trimmed) || (trimmed.len() >= 40 && trimmed.chars().all(is_base64url))
|
||||
}
|
||||
|
||||
/// 是否是 JWT 形状:`header.payload.signature`,三段都非空且够长、全是 base64url 字符。
|
||||
///
|
||||
/// 公开是为了单测能直接钉这条判据(UI 侧「注入 JWT 必须被抹掉」的测试也用它)。
|
||||
#[must_use]
|
||||
pub fn looks_like_jwt(token: &str) -> bool {
|
||||
let parts: Vec<&str> = token.split('.').collect();
|
||||
parts.len() == 3
|
||||
&& parts
|
||||
.iter()
|
||||
.all(|part| part.len() >= 8 && part.chars().all(is_base64url))
|
||||
}
|
||||
|
||||
/// base64url 字母表(含 `=` 填充与 `+`/`/` 的宽容分支)。
|
||||
fn is_base64url(c: char) -> bool {
|
||||
c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '+' | '/' | '=' | '.')
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// 标准 JWT 形状(2 段 header/payload + 1 段签名)。
|
||||
const JWT: &str =
|
||||
"eyJhbGciOiJSUzI1NiIsIng1dCI6IkFCQyJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJlLXZhbHVl";
|
||||
|
||||
#[test]
|
||||
fn the_jwt_shape_judgement_is_not_a_length_heuristic() {
|
||||
assert!(looks_like_jwt(JWT));
|
||||
assert!(!looks_like_jwt("not.a.jwt"), "短段不算 JWT");
|
||||
assert!(!looks_like_jwt("a.b.c.d"), "四段不算 JWT");
|
||||
assert!(!looks_like_jwt("ABCD-EFGH"), "设备码不是 JWT");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_jwts_and_sensitive_pairs_are_redacted() {
|
||||
assert_eq!(redact(JWT), REDACTED);
|
||||
assert_eq!(
|
||||
redact(&format!("Bearer {JWT}")),
|
||||
format!("Bearer {REDACTED}")
|
||||
);
|
||||
assert_eq!(
|
||||
redact(&format!("accessToken={JWT}")),
|
||||
format!("accessToken={REDACTED}")
|
||||
);
|
||||
// 敏感键名 + 短值(真实 refresh token 常常很短,靠键名兜)
|
||||
assert_eq!(redact("password=hunter2"), format!("password={REDACTED}"));
|
||||
assert_eq!(
|
||||
redact("refresh_token=abc"),
|
||||
format!("refresh_token={REDACTED}")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordinary_copy_is_left_byte_for_byte() {
|
||||
for text in [
|
||||
"在浏览器打开 https://www.microsoft.com/link 并输入 ABCD-EFGH",
|
||||
"服务器不可达:dns error",
|
||||
"HTTP 403 Forbidden — 该服务器要求白名单",
|
||||
"",
|
||||
] {
|
||||
assert_eq!(redact(text), text, "普通文案被改了:{text}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitespace_and_line_breaks_survive() {
|
||||
let text = format!("第一行 token={JWT}\n\n第二行 缩进");
|
||||
let out = redact(&text);
|
||||
assert!(out.contains("\n\n"), "{out}");
|
||||
assert!(out.contains(" 缩进"), "{out}");
|
||||
assert!(!out.contains(JWT), "{out}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
//! 账户 DTO 的契约测试(凭据红线的主要断言都在这里)。
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn kind_round_trips_through_stable_ids() {
|
||||
for kind in [
|
||||
AccountKind::Offline,
|
||||
AccountKind::Microsoft,
|
||||
AccountKind::Authlib,
|
||||
] {
|
||||
assert_eq!(AccountKind::parse(kind.as_str()), kind);
|
||||
assert!(!kind.label().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_login_view_never_carries_tokens() {
|
||||
let view = DeviceLoginView::waiting("ABCD-EFGH", "https://www.microsoft.com/link", 900, "");
|
||||
assert_eq!(view.phase, DeviceLoginPhase::Waiting);
|
||||
assert!(view.phase.busy());
|
||||
assert!(view.qr_link.contains("ABCD-EFGH"));
|
||||
let json = serde_json::to_string(&view).expect("序列化");
|
||||
for forbidden in ["token", "secret", "password", "refresh"] {
|
||||
assert!(
|
||||
!json.to_lowercase().contains(forbidden),
|
||||
"视图里不该出现 {forbidden}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn browser_waiting_shows_a_copyable_link_instead_of_a_user_code() {
|
||||
let view = DeviceLoginView::browser_waiting(
|
||||
"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=x",
|
||||
300,
|
||||
Some("拉起浏览器失败"),
|
||||
);
|
||||
assert_eq!(view.phase, DeviceLoginPhase::Waiting);
|
||||
assert!(view.phase.busy());
|
||||
assert!(view.user_code.is_empty(), "授权码流没有用户码");
|
||||
assert_eq!(view.qr_link, view.verification_uri);
|
||||
assert!(view.message.contains("浏览器"));
|
||||
assert!(view.message.contains("拉起浏览器失败"), "{}", view.message);
|
||||
let json = serde_json::to_string(&view).expect("序列化");
|
||||
for forbidden in ["refresh", "mc-access"] {
|
||||
assert!(!json.contains(forbidden), "视图里不该出现 {forbidden}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn succeeded_and_failed_clear_the_code() {
|
||||
let ok = DeviceLoginView::succeeded("Steve");
|
||||
assert_eq!(ok.phase, DeviceLoginPhase::Succeeded);
|
||||
assert!(ok.user_code.is_empty());
|
||||
let bad = DeviceLoginView::failed("网络超时");
|
||||
assert_eq!(bad.phase, DeviceLoginPhase::Failed);
|
||||
assert_eq!(bad.message, "网络超时");
|
||||
}
|
||||
|
||||
/// 凭据红线(审核项):JWT 形状串从**任何**入口进来都必须被抹掉。
|
||||
///
|
||||
/// 抓得住:`redact` 被摘掉、判据被放宽成「长度 ≥40 就行」、或者某个构造器绕过它。
|
||||
/// 抓不住:真实凭据的明文值(那是 `core::launch::redact` 的活儿,按已知值替换)。
|
||||
#[test]
|
||||
fn jwt_shaped_text_is_redacted_at_every_entry_point() {
|
||||
let jwt =
|
||||
"eyJhbGciOiJSUzI1NiIsIng1dCI6IkFCQyJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJlLXZhbHVl";
|
||||
|
||||
let device = DeviceLoginView::failed(format!("登录失败:accessToken={jwt}"));
|
||||
assert!(!device.message.contains(jwt), "{}", device.message);
|
||||
assert!(device.message.contains(REDACTED), "{}", device.message);
|
||||
|
||||
let authlib = AuthlibLoginView::failed(format!("服务器回包异常 {jwt}"));
|
||||
assert!(!authlib.message.contains(jwt), "{}", authlib.message);
|
||||
assert!(authlib.message.contains(REDACTED), "{}", authlib.message);
|
||||
|
||||
let ready = AuthlibLoginView::reachable("https://littleskin.cn/", "LittleSkin");
|
||||
assert!(!ready.message.contains(jwt));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authlib_request_never_prints_the_password() {
|
||||
let request = AuthlibRequest::new(
|
||||
"https://littleskin.cn/api/yggdrasil/",
|
||||
"me@example.com",
|
||||
"hunter2",
|
||||
);
|
||||
let shown = request.redacted();
|
||||
assert!(!shown.contains("hunter2"), "{shown}");
|
||||
assert!(shown.contains("littleskin"), "{shown}");
|
||||
assert!(shown.contains(REDACTED), "{shown}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authlib_view_carries_the_server_name_and_no_tokens() {
|
||||
let view = AuthlibLoginView::reachable("https://littleskin.cn/api/yggdrasil/", "LittleSkin");
|
||||
assert_eq!(view.phase, DeviceLoginPhase::Waiting);
|
||||
assert_eq!(view.server_name, "LittleSkin");
|
||||
assert!(view.message.contains("LittleSkin"));
|
||||
|
||||
let ok = AuthlibLoginView::succeeded("LittleSkin", "Steve");
|
||||
assert_eq!(ok.phase, DeviceLoginPhase::Succeeded);
|
||||
assert_eq!(ok.account_name, "Steve");
|
||||
let json = serde_json::to_string(&ok).expect("序列化");
|
||||
for forbidden in ["accessToken", "clientToken", "refresh", "password"] {
|
||||
assert!(!json.contains(forbidden), "视图里不该出现 {forbidden}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_view_serializes_without_credentials() {
|
||||
let view = AccountView {
|
||||
id: "account:a".to_string(),
|
||||
name: "Steve".to_string(),
|
||||
kind: AccountKind::Authlib,
|
||||
profile_id: "0f8f".to_string(),
|
||||
login_name: "me@example.com".to_string(),
|
||||
created: "2026-10-31 20:12".to_string(),
|
||||
active: true,
|
||||
server_url: "https://littleskin.cn/api/yggdrasil/".to_string(),
|
||||
skin: Some(SkinImage {
|
||||
png: vec![1, 2, 3],
|
||||
source: "textures".to_string(),
|
||||
}),
|
||||
};
|
||||
let json = serde_json::to_string(&view).expect("序列化");
|
||||
for forbidden in ["token", "secret", "password", "refresh"] {
|
||||
assert!(
|
||||
!json.to_lowercase().contains(forbidden),
|
||||
"账户视图里不该出现 {forbidden}"
|
||||
);
|
||||
}
|
||||
assert!(json.contains("\"png\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn monogram_is_stable_and_never_empty() {
|
||||
let mut view = AccountView {
|
||||
id: "account:a".to_string(),
|
||||
name: " steve".to_string(),
|
||||
kind: AccountKind::Microsoft,
|
||||
profile_id: "0f8f".to_string(),
|
||||
login_name: String::new(),
|
||||
created: String::new(),
|
||||
active: false,
|
||||
server_url: String::new(),
|
||||
skin: None,
|
||||
};
|
||||
assert_eq!(view.monogram(), "S");
|
||||
let tone = view.monogram_tone();
|
||||
assert!(tone < 6);
|
||||
view.profile_id = "0f8f".to_string();
|
||||
assert_eq!(view.monogram_tone(), tone, "同一个人每次同一个色块");
|
||||
view.name = String::new();
|
||||
assert_eq!(view.monogram(), "?", "空名字也要有字,不能画白块");
|
||||
}
|
||||
@@ -9,10 +9,12 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::dialogs::{DialogId, DialogRequest};
|
||||
use crate::dto::CreateRequest;
|
||||
use crate::dto::{
|
||||
AccountView, AgentEvent, ContentInstallRequest, ContentQuery, ContentResults, DeviceLoginView,
|
||||
DiagnosisReport, InstallProgressView, InstanceId, InstanceRuntime, InstanceSummary,
|
||||
JavaPageView, LoaderKind, LogLine, LogOutcome, LogSession, NoticeView, PackCommand, PackEvent,
|
||||
SessionId, SettingGroupView, VersionCategory, VersionSummary,
|
||||
// 主线(M109 的 Content* / M113 的 Pack*)与 M114 的 Authlib* 的并集。
|
||||
AccountView, AgentEvent, AuthlibLoginView, AuthlibRequest, ContentInstallRequest, ContentQuery,
|
||||
ContentResults, DeviceLoginView, DiagnosisReport, InstallProgressView, InstanceId,
|
||||
InstanceRuntime, InstanceSummary, JavaPageView, LoaderKind, LogLine, LogOutcome, LogSession,
|
||||
NoticeView, PackCommand, PackEvent, SessionId, SettingGroupView, VersionCategory,
|
||||
VersionSummary,
|
||||
};
|
||||
use crate::toast::Toast;
|
||||
|
||||
@@ -68,6 +70,8 @@ pub enum CoreEvent {
|
||||
AccountsChanged(Vec<AccountView>),
|
||||
/// 微软登录状态刷新
|
||||
DeviceLoginChanged(DeviceLoginView),
|
||||
/// 第三方轨(authlib-injector)登录/校验状态刷新
|
||||
AuthlibLoginChanged(AuthlibLoginView),
|
||||
/// Java 页快照(发行版/回退链/运行时)
|
||||
JavaChanged(JavaPageView),
|
||||
/// 安装进度(`None` = 清掉进度面板)
|
||||
@@ -168,6 +172,12 @@ pub enum AppCommand {
|
||||
StartBrowserLogin,
|
||||
/// 取消/关掉微软登录面板(已发出的请求会在收尾时丢弃结果)
|
||||
CancelDeviceLogin,
|
||||
/// 加一个第三方皮肤站(authlib-injector)账户:校验服务器 + 密码登录
|
||||
///
|
||||
/// **凭据红线**:`request.password` 只是这一次的请求值,host 用完即弃,绝不进回程 DTO。
|
||||
AddAuthlibAccount(AuthlibRequest),
|
||||
/// 只校验第三方皮肤站地址(服务发现):结果走 `AuthlibLoginChanged`
|
||||
ProbeAuthlibServer(String),
|
||||
/// 安装指定发行版的托管 JDK(`temurin` / `oracle` / `microsoft` / `zulu`)
|
||||
InstallJava(String),
|
||||
/// 写一条设置(`key` 必须是 core schema 里的键)
|
||||
@@ -246,6 +256,8 @@ mod tests {
|
||||
login_name: String::new(),
|
||||
created: "2026-10-31 20:12".to_string(),
|
||||
active: true,
|
||||
server_url: String::new(),
|
||||
skin: None,
|
||||
}]));
|
||||
let text = serde_json::to_string(&account).expect("序列化");
|
||||
let back: AppEvent = serde_json::from_str(&text).expect("反序列化");
|
||||
@@ -273,5 +285,29 @@ mod tests {
|
||||
login
|
||||
);
|
||||
assert!(DeviceLoginPhase::Waiting.busy());
|
||||
|
||||
// 第三方轨(M114):请求进、状态出,两边都不带 token 字段
|
||||
let authlib = AppCommand::AddAuthlibAccount(AuthlibRequest::new(
|
||||
"https://littleskin.cn/api/yggdrasil/",
|
||||
"me@example.com",
|
||||
"hunter2",
|
||||
));
|
||||
let text = serde_json::to_string(&authlib).expect("序列化");
|
||||
assert_eq!(
|
||||
serde_json::from_str::<AppCommand>(&text).expect("反序列化"),
|
||||
authlib
|
||||
);
|
||||
|
||||
let view = AppEvent::Core(CoreEvent::AuthlibLoginChanged(AuthlibLoginView::reachable(
|
||||
"https://littleskin.cn/api/yggdrasil/",
|
||||
"LittleSkin",
|
||||
)));
|
||||
let text = serde_json::to_string(&view).expect("序列化");
|
||||
let back: AppEvent = serde_json::from_str(&text).expect("反序列化");
|
||||
assert_eq!(back, view);
|
||||
assert!(
|
||||
!text.to_lowercase().contains("token"),
|
||||
"回程视图里不该有 token"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,10 @@ mod pack;
|
||||
mod settings;
|
||||
mod versions;
|
||||
|
||||
pub use accounts::{AccountKind, AccountView, DeviceLoginPhase, DeviceLoginView};
|
||||
pub use accounts::{
|
||||
looks_like_jwt, redact, AccountKind, AccountView, AuthlibLoginView, AuthlibRequest,
|
||||
DeviceLoginPhase, DeviceLoginView, SkinImage, REDACTED,
|
||||
};
|
||||
pub use agent::{AgentEvent, ChatMessage, ChatRole, ConfirmRequest, Sensitivity, ToolCallView};
|
||||
pub use content::{
|
||||
loader_facet, ContentHit, ContentInstallRequest, ContentKind, ContentQuery, ContentResults,
|
||||
|
||||
Reference in new issue
Block a user