fix(auth): M117 微软登录缺 client id 说「未配置」而不是「未支持」

用户实跑 debug 二进制看到「本期未支持」,误以为功能没做;实际是构建/启动时
没注入 Azure 公共客户端 id(登录三轨已实现、白名单已批、真机验签过)。

- core/auth/microsoft/client_id.rs(新):id 的来源判定(env > build,空白值不算配)、
  `ClientIdStatus`/`log_line()` 那行 `[auth] msa client_id=… source=…`、以及
  「未配置 + 怎么配(含可照抄命令)」的共用文案 `MISSING_CLIENT_ID_MESSAGE`。零网络。
- core/auth/error.rs:`AuthError::Unsupported` 不再统一拼「本期未支持:」前缀——该变体
  全仓唯一构造点就是缺 client id,硬拼前缀让「没配置」被读成「没实现」。`code()` 仍是
  `unsupported`,枚举形状与其它分支不动(tower 批的最小改动)。
- core/auth/microsoft/mod.rs:`from_env()` 走 `resolve_client_id()`,失败措辞取自上面那条
  文案,且在网络之前早退(连 HTTP 客户端都不建);导出 `client_id_status()`。
- cli:`map_auth_error` 的 Unsupported hint 给两条能照抄的命令;`accounts` 帮助说清
  「不是功能缺失」与两种注入方式;新增 `msa_client_id_log_line()` 给 GUI 侧出面。
- app-iced:`on_boot` 打一行 `[auth] msa client_id=… source=…`(判据来自 core,不重算)。
- i18n:`LaunchMsaNotConfigured` 中英各一条,golden 按 `Key::ALL` 顺序在
  `LaunchFailedWithCode` 之后追加一行。

本地便利路径(gitignored,随报告交付):`DevDocs/ops/ull-msa-run.sh` 读本地 id 文件
或 `azure-msa-app.md` 注入环境变量再起 GUI,脚本本身不含 id。
This commit is contained in:
AskaEth committed 2026-11-05 07:31:55 +08:00
1 parent 7ddcec4941
commit 8780f26308
8 files changed
+380 -43

No files matched your search

+8 -2
View File
@@ -79,7 +79,13 @@ pub enum AuthError {
/// 冲突的账户名。
account: String,
},
/// 该轨/该能力本期未实现(如 CLI 上的 authlib 密码登录)。
/// 该轨/该能力当前不可用:**未配置**(如缺 Azure client id)或本期未实现
/// (如 CLI 上的 authlib 密码登录)。
///
/// 载荷必须**自带完整的人读句子**(「未配置 + 怎么配」/「未实现 + 为什么」),
/// 本层不再统一拼「本期未支持:」这类前缀——同一层里两种情况完全不同的事,
/// 硬拼一个前缀会让「没配置」被读成「没实现」(M117 用户实跑踩到)。
/// 机读码仍是 `unsupported`([`Self::code`]),调用方分流不受影响。
Unsupported(String),
}
@@ -170,7 +176,7 @@ impl AuthError {
Self::Store { path, detail } => format!("账户数据读写失败({path}):{detail}"),
Self::NotFound { account } => format!("找不到账户 `{account}`"),
Self::Conflict { account } => format!("账户 `{account}` 已存在"),
Self::Unsupported(detail) => format!("本期未支持:{detail}"),
Self::Unsupported(detail) => detail.clone(),
}
}
}
+248
View File
@@ -0,0 +1,248 @@
//! Azure 公共客户端 id 的**配置状态**:从哪来、配没配、启动日志那一行长什么样。
//!
//! ## 为什么单独一块(M117)
//!
//! 这个 id 不进仓库(用户定案),所以它可以来自两处:运行期环境变量 [`CLIENT_ID_ENV`],
//! 或构建期 `option_env!` 注入([`CLIENT_ID_BUILD`])。**两处都没有**时登录是「没配置」,
//! 不是「没实现」——这两件事在用户眼里完全不同,文案与日志都必须分清:
//!
//! | 现象 | 该说什么 |
//! |---|---|
//! | 两处都没配 | [`MISSING_CLIENT_ID_MESSAGE`]:未配置 + 怎么配(含可照抄的命令) |
//! | 配了但登录失败 | 走 `AuthError` 的正常分流(网络/拒绝/无拥有权…),**不许**再提「未实现」 |
//!
//! [`ClientIdStatus::log_line`] 是给启动日志的那一行:`[auth] msa client_id=… source=…`。
//! 有了它,「未配置」与「配了但登录失败」在日志里一眼可分,不用再猜。
//!
//! 本模块**零网络**:只读环境变量与编译期常量,连 HTTP 客户端都不建。
/// 运行期环境变量名(配 client id)。
pub const CLIENT_ID_ENV: &str = "ULL_MSA_CLIENT_ID";
/// 构建期可注入的 client id(`ULL_MSA_CLIENT_ID=<id> cargo build`)。
pub const CLIENT_ID_BUILD: Option<&str> = option_env!("ULL_MSA_CLIENT_ID");
/// 两处都没配时给用户看的那句话(CLI / GUI / 启动日志共用,**不含任何真实 id**)。
///
/// 措辞纪律(M117):说「未配置」,不说「未支持」——登录链路是完整的,缺的只是这一项;
/// 并且句子里带**可照抄的命令**,因为失败横幅往往是用户唯一会看到的那段字。
pub const MISSING_CLIENT_ID_MESSAGE: &str =
"微软登录未配置:缺少 Azure 公共客户端 id(登录功能已实现,只差这一项配置)。\
构建时注入:`ULL_MSA_CLIENT_ID=<id> cargo build`;或启动前设置同名环境变量 `ULL_MSA_CLIENT_ID=<id>`。";
/// client id 从哪来(启动日志与排障用)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClientIdSource {
/// 运行期环境变量 `ULL_MSA_CLIENT_ID`(**优先于**构建期注入)。
Env,
/// 构建期 `option_env!` 注入。
Build,
}
impl ClientIdSource {
/// 启动日志里的机读标签(`env` / `build`)。
#[must_use]
pub const fn tag(self) -> &'static str {
match self {
Self::Env => "env",
Self::Build => "build",
}
}
}
/// client id 的配置状态:配没配 + 从哪来(`None` = 两处都没有)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClientIdStatus {
source: Option<ClientIdSource>,
}
impl ClientIdStatus {
/// 拿到 id 了没(`false` = 需要配置,不是功能缺失)。
#[must_use]
pub const fn is_configured(self) -> bool {
self.source.is_some()
}
/// 来源(`None` = 两处都没有)。
#[must_use]
pub const fn source(self) -> Option<ClientIdSource> {
self.source
}
/// 启动日志那一行:`[auth] msa client_id=configured|missing source=env|build|none`。
///
/// 判据(M117):日后看日志能直接分辨「未配置」与「配置了但登录失败」。
#[must_use]
pub fn log_line(self) -> String {
format!(
"[auth] msa client_id={} source={}",
if self.source.is_some() {
"configured"
} else {
"missing"
},
self.source.map_or("none", ClientIdSource::tag)
)
}
}
/// 当前进程看到的 client id 与来源(**只读环境/编译期常量,不联网**)。
///
/// 优先级:环境变量 > 构建期注入。两者都存在时**不告警**(M117):环境变量就是显式覆盖,
/// 来源如实报 [`ClientIdSource::Env`],日志里看得到。
#[must_use]
pub fn resolve_client_id() -> Option<(String, ClientIdSource)> {
let from_env = std::env::var(CLIENT_ID_ENV)
.ok()
.filter(|value| !value.trim().is_empty());
let from_build = CLIENT_ID_BUILD.filter(|value| !value.trim().is_empty());
match (from_env, from_build) {
(Some(value), _) => Some((value, ClientIdSource::Env)),
(None, Some(value)) => Some((value.to_string(), ClientIdSource::Build)),
(None, None) => None,
}
}
/// 只问「配没配 / 从哪来」——启动日志与 GUI 启动行用它(不建 HTTP 客户端、不联网)。
#[must_use]
pub fn client_id_status() -> ClientIdStatus {
ClientIdStatus {
source: resolve_client_id().map(|(_, source)| source),
}
}
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use super::{
client_id_status, resolve_client_id, ClientIdSource, CLIENT_ID_BUILD, CLIENT_ID_ENV,
MISSING_CLIENT_ID_MESSAGE,
};
use crate::auth::error::AuthError;
use crate::auth::microsoft::MicrosoftAuth;
/// 环境变量是**进程级**的:动它的用例必须串行,否则并行跑会互相摘/设,结论随机。
static ENV_LOCK: Mutex<()> = Mutex::new(());
/// 用例结束后把环境变量还原(panic 也还原:`Drop` 跑得到)。
struct EnvGuard {
saved: Option<String>,
}
impl EnvGuard {
fn clear() -> Self {
let saved = std::env::var(CLIENT_ID_ENV).ok();
std::env::remove_var(CLIENT_ID_ENV);
Self { saved }
}
fn set(value: &str) -> Self {
let saved = std::env::var(CLIENT_ID_ENV).ok();
std::env::set_var(CLIENT_ID_ENV, value);
Self { saved }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
match self.saved.take() {
Some(value) => std::env::set_var(CLIENT_ID_ENV, value),
None => std::env::remove_var(CLIENT_ID_ENV),
}
}
}
#[test]
fn missing_client_id_message_says_unconfigured_not_unimplemented() {
// 抓得住:文案退回「本期未支持」这类说法(用户实跑到的那个 bug),
// 或者只说「需要 id」却不给怎么配(用户下一步就卡在这里)。
// 抓不住:文案好不好读——那要靠 review。
assert!(MISSING_CLIENT_ID_MESSAGE.contains("客户端 id"));
assert!(MISSING_CLIENT_ID_MESSAGE.contains("未配置"));
assert!(!MISSING_CLIENT_ID_MESSAGE.contains("未支持"));
assert!(!MISSING_CLIENT_ID_MESSAGE.contains("未实现"));
assert!(MISSING_CLIENT_ID_MESSAGE.contains(CLIENT_ID_ENV));
assert!(MISSING_CLIENT_ID_MESSAGE.contains("cargo build"));
// 硬约束:这句话进日志、进横幅、进错误,**示例一律是 `<id>` 占位**,不出现真 id。
assert!(MISSING_CLIENT_ID_MESSAGE.contains("<id>"));
}
#[test]
fn from_env_without_client_id_is_a_config_error_not_a_feature_gap() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|error| error.into_inner());
let _guard = EnvGuard::clear();
let result = MicrosoftAuth::from_env();
match result {
Err(AuthError::Unsupported(message)) => {
// 机读码保持 `unsupported`(CLI `--json` 与既有调用方靠它分流),
// 但**人读文案**必须说「未配置」。
assert_eq!(message, MISSING_CLIENT_ID_MESSAGE);
assert!(!message.contains("未支持"));
}
// 构建期注入过 id 时唯一合法(本仓库 CI/本地都不注入,走不到这里)。
Ok(_) => assert!(
CLIENT_ID_BUILD.is_some_and(|value| !value.trim().is_empty()),
"没配 id 却构造成功"
),
Err(other) => panic!("应是 Unsupported,实际 {other:?}"),
}
}
#[test]
fn env_client_id_wins_over_build_and_the_log_says_so() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|error| error.into_inner());
let _guard = EnvGuard::set("0000-test-env-id");
let (id, source) = resolve_client_id().expect("环境变量给了 id");
assert_eq!(id, "0000-test-env-id");
// 两者都存在时环境变量优先,且**不告警**,但来源必须是 env。
assert_eq!(source, ClientIdSource::Env);
let status = client_id_status();
assert!(status.is_configured());
assert_eq!(status.source(), Some(ClientIdSource::Env));
assert_eq!(
status.log_line(),
"[auth] msa client_id=configured source=env"
);
// 有 id 时**不再报配置错误**:只验到「进得了登录流程的入口」为止,
// `from_env` 本身零网络(它只建 reqwest 客户端,不发请求)。
let auth = MicrosoftAuth::from_env().expect("配了 id 就该构造成功");
assert!(auth.endpoints().profile.contains("minecraftservices.com"));
}
#[test]
fn missing_client_id_log_line_is_machine_readable() {
let _lock = ENV_LOCK.lock().unwrap_or_else(|error| error.into_inner());
let _guard = EnvGuard::clear();
if CLIENT_ID_BUILD.is_some_and(|value| !value.trim().is_empty()) {
// 构建期注入过 id:这条没有「缺配置」可言,跳过。
return;
}
let status = client_id_status();
assert!(!status.is_configured());
assert_eq!(status.source(), None);
assert_eq!(
status.log_line(),
"[auth] msa client_id=missing source=none"
);
}
#[test]
fn blank_env_value_counts_as_missing() {
// 抓得住:`ULL_MSA_CLIENT_ID=""`(空串/全空白)被当成配好了,进而拿空 id 去联网。
let _lock = ENV_LOCK.lock().unwrap_or_else(|error| error.into_inner());
let _guard = EnvGuard::set(" ");
if CLIENT_ID_BUILD.is_some_and(|value| !value.trim().is_empty()) {
assert_eq!(
resolve_client_id().map(|(_, source)| source),
Some(ClientIdSource::Build),
"空白环境变量必须让位给构建期注入"
);
} else {
assert!(resolve_client_id().is_none());
}
}
}
+23 -36
View File
@@ -1,7 +1,8 @@
//! 微软轨:device-code 与授权码(浏览器)两条登录流 + 会话/刷新语义
//! (`PlanDoc/07-accounts.md` §1、`analyse/04` §4–§5、`research/08` §3)。
//!
//! 分五块,各管一件事:
//! 分六块,各管一件事:
//! - [`client_id`]:Azure 公共客户端 id 的**配置状态**(来源 / 配没配 / 启动日志那一行,零网络);
//! - [`session`]:`MicrosoftSession`(凭据载荷)与**刷新判定**(纯函数,可脱离网络单测);
//! - [`endpoints`]:端点表(生产 = 微软消费版直连;测试 = 整表指向 mock);
//! - [`flow`]:device-code 发起 / 轮询 / refresh(OAuth 层);
@@ -17,12 +18,17 @@
//! 重定向 URI 登记为 `http://localhost`)。HMCL 把它烘进 jar 的
//! manifest(`hmcl.microsoft.auth.id`),我们同理:二进制里不硬编码,改由
//! `ULL_MSA_CLIENT_ID`(运行期)或构建期 `option_env!` 提供。没有配时
//! [`MicrosoftAuth::from_env`] 返回 [`AuthError::Unsupported`] 并说清要配什么——
//! [`MicrosoftAuth::from_env`] 返回 [`AuthError::Unsupported`],文案是
//! [`client_id::MISSING_CLIENT_ID_MESSAGE`](「未配置」+ 怎么配,见 [`client_id`])——
//! 这比编一个假 id 让用户看到 `invalid_client` 诚实。
//!
//! 措辞纪律(M117):id 缺失是**没配置**,不是「本期未支持」。判定与那行启动日志都在
//! [`client_id`],本模块只消费结论。
pub mod authcode;
pub mod callback;
pub mod chain;
pub mod client_id;
pub mod endpoints;
pub mod flow;
@@ -30,6 +36,12 @@ pub mod session;
pub use authcode::{AuthCodePolicy, AuthCodePrompt, RedirectUri};
pub use callback::CallbackServer;
// client id 的判定与启动日志行留一层 re-export:调用方(CLI / GUI / 启动日志)只认
// `auth::microsoft::*` 这一条路径,不必知道它是单独一块(M117)。
pub use client_id::{
client_id_status, resolve_client_id, ClientIdSource, ClientIdStatus, CLIENT_ID_BUILD,
CLIENT_ID_ENV, MISSING_CLIENT_ID_MESSAGE,
};
pub use endpoints::MicrosoftEndpoints;
pub use flow::{DeviceCodePrompt, PollPolicy};
pub use session::{MicrosoftSession, RefreshDecision, RefreshReason, USER_TYPE_MSA};
@@ -43,11 +55,6 @@ use crate::auth::journal::{AuthFailureEvent, AuthJournal};
use crate::download::config::EngineConfig;
use crate::download::http::build_client;
/// 运行期环境变量名(配 client id)。
pub const CLIENT_ID_ENV: &str = "ULL_MSA_CLIENT_ID";
/// 构建期可注入的 client id(`ULL_MSA_CLIENT_ID=... cargo build`)。
pub const CLIENT_ID_BUILD: Option<&str> = option_env!("ULL_MSA_CLIENT_ID");
/// 微软账户登录/刷新。
#[derive(Debug, Clone)]
pub struct MicrosoftAuth {
@@ -86,17 +93,15 @@ impl MicrosoftAuth {
///
/// # Errors
///
/// 两处都没配时返回 [`AuthError::Unsupported`]。
/// 两处都没配时返回 [`AuthError::Unsupported`],文案是
/// [`MISSING_CLIENT_ID_MESSAGE`](「未配置」+ 怎么配)。这条路径**零网络**:
/// id 判定在网络之前,判断失败就地早退,连 HTTP 客户端都不建。
pub fn from_env() -> AuthResult<Self> {
let client_id = std::env::var(CLIENT_ID_ENV)
.ok()
.filter(|value| !value.trim().is_empty())
.or_else(|| CLIENT_ID_BUILD.map(str::to_string))
.ok_or_else(|| {
AuthError::Unsupported(format!(
"微软登录需要 Azure 公共客户端 id:设置环境变量 {CLIENT_ID_ENV}(或构建时注入)"
))
})?;
let Some((client_id, _source)) = resolve_client_id() else {
return Err(AuthError::Unsupported(
MISSING_CLIENT_ID_MESSAGE.to_string(),
));
};
let mut auth = Self::new(client_id)?;
auth.journal = AuthJournal::portable_default();
Ok(auth)
@@ -340,8 +345,7 @@ pub fn now_ms() -> u64 {
#[cfg(test)]
mod tests {
use super::{now_ms, MicrosoftAuth, CLIENT_ID_ENV};
use crate::auth::error::AuthError;
use super::{now_ms, MicrosoftAuth};
#[test]
fn explicit_client_id_builds() {
@@ -352,23 +356,6 @@ mod tests {
.contains("api.minecraftservices.com"));
}
#[test]
fn missing_client_id_is_explained() {
// 测试进程里也可能被外部配了环境变量,先摘掉再验。
let saved = std::env::var(CLIENT_ID_ENV).ok();
std::env::remove_var(CLIENT_ID_ENV);
let result = MicrosoftAuth::from_env();
if let Some(value) = saved {
std::env::set_var(CLIENT_ID_ENV, value);
}
match result {
Err(AuthError::Unsupported(message)) => assert!(message.contains(CLIENT_ID_ENV)),
// 构建期注入过 client id 时唯一合法,跳过断言。
Ok(_) => assert!(super::CLIENT_ID_BUILD.is_some()),
Err(other) => panic!("应是 Unsupported,实际 {other:?}"),
}
}
#[test]
fn now_ms_is_sane() {
// 2020-01-01 之后、2100 之前(防止本地时钟/单位抄错)。