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

+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 之前(防止本地时钟/单位抄错)。