From a55f9e916c1ed4f71bc1327157d610c74dae0c26 Mon Sep 17 00:00:00 2001 From: clh02467605 Date: Tue, 1 Dec 2026 18:13:26 +0800 Subject: [PATCH 1/2] refactor(auth): update authentication methods and documentation for Aliyun OAuth - Changed recommended authentication method from console login to Aliyun OAuth across INSTALL.md, README.md, and README.zh.md. - Enhanced instructions for OAuth login, including support for headless environments and cross-device authorization. - Updated UI elements and error messages to reflect the new OAuth workflow. - Adjusted tests to validate the new login method and ensure compatibility with existing commands. - Improved documentation to clarify the use of OAuth and console login options. --- INSTALL.md | 38 ++- README.md | 17 +- README.zh.md | 33 +- packages/cli/README.md | 17 +- packages/cli/README.zh.md | 33 +- .../cli/tests/e2e/aliyun-compat.e2e.test.ts | 4 +- .../commands/src/commands/auth/console-ui.ts | 45 ++- packages/commands/src/commands/auth/login.ts | 62 ++-- packages/commands/src/commands/auth/status.ts | 10 +- .../commands/src/commands/config/ui-html.ts | 95 ++++-- .../commands/src/commands/config/ui-i18n.ts | 18 +- packages/commands/src/commands/config/ui.ts | 10 +- .../commands/tests/auth-console-ui.test.ts | 11 +- packages/commands/tests/e2e/auth.e2e.test.ts | 2 + packages/runtime/src/error-handler.ts | 17 +- packages/runtime/src/output/banner.ts | 14 +- .../tests/command-reference-output.test.ts | 9 +- packages/runtime/tests/error-handler.test.ts | 2 +- packages/runtime/tests/root-help.test.ts | 4 +- skills/bailian-cli/SKILL.md | 2 +- skills/bailian-cli/reference/auth.md | 79 ++--- skills/bailian-cli/reference/index.md | 284 +++++++++--------- skills/bailian-finetune/reference/index.md | 2 +- skills/bailian-gen/reference/index.md | 2 +- .../bailian-managed-agent/reference/index.md | 2 +- skills/bailian-memory/reference/index.md | 2 +- skills/bailian-protocol/SKILL.md | 17 +- skills/bailian-protocol/assets/setup.md | 63 +++- skills/bailian-sandbox/reference/index.md | 2 +- tools/generate-reference.ts | 2 +- 30 files changed, 580 insertions(+), 318 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index d159d23fb..a3c93d8cc 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -76,16 +76,44 @@ which bl # Windows: where.exe bl ## 3. 鉴权(安装后必做才能调 API) -### 推荐:浏览器登录(控制台会话) +### 推荐:阿里云 OAuth -1. 执行 `bl auth status --output json`,判断是否已配置。 -2. 若未配置,在**用户本机终端**执行 `bl auth login --console`(国际站执行 `bl auth login --console --console-site international`);需要时会自动创建普通 API Key。 -3. 如果使用 Token Plan 等订阅计划,不能使用 `--console`,请执行 `bl auth login --api-key `。 +1. 执行 `bl auth status --output json`,判断是否已配置所需凭证。 +2. 若未配置,在**用户本机终端**执行 `bl auth login --oauth`(国际站执行 `bl auth login --oauth --console-site international`)。 +3. 已有普通 API Key,或使用 Token Plan 等订阅计划时,执行 `bl auth login --api-key `。 4. 登录成功后执行 `bl auth status --output json` 确认;汇报时只使用 masked 字段,**禁止**回显完整凭据。 +5. SSH / 容器 / CI 等无浏览器或无人值守场景,见下方「无浏览器环境鉴权」。 + +### 兼容:控制台浏览器登录 + +仍可用 `bl auth login --console`(国际站加 `--console-site international`);需要时会自动创建普通 API Key。订阅计划不能走 `--console`,请用 `--api-key`。 + +### 无浏览器环境鉴权 + +两种用法,按场景选择: + +1. **无人值守(管控 / OpenAPI)**:通过环境变量注入静态 AK/SK,不打开浏览器。 + +```bash +export BAILIAN_ACCESS_KEY_ID= +export BAILIAN_ACCESS_KEY_SECRET= +# 可选:export BAILIAN_SECURITY_TOKEN= +# 也可使用 ALIBABA_CLOUD_ACCESS_KEY_ID / ALIBABA_CLOUD_ACCESS_KEY_SECRET / ALIBABA_CLOUD_SECURITY_TOKEN +bl auth status --output json +``` + +2. **OAuth 跨设备授权**:本机无浏览器时,在原终端运行 `bl auth login --oauth --no-browser`,在有浏览器的设备打开打印出的授权链接并完成授权,再把地址栏中**完整回调 URL**(含 `code` 与 `state`)粘回同一个 CLI 会话。这不是设备码登录,也不是无人值守 CI 鉴权。 + +```bash +bl auth login --oauth --no-browser +# 国际站:bl auth login --oauth --no-browser --console-site international +``` + +AccessKey 浏览器引导页也可用 `--no-browser` 只打印链接与后续配置命令:`bl auth login --open-api --no-browser`。 ### Agent 安全约束 -- **禁止**把真实 API Key 写入仓库、日志、Skill、聊天记录的可公开部分。 +- **禁止**把真实 API Key / AccessKey 写入仓库、日志、Skill、聊天记录的可公开部分。 - CI / 非交互环境:显式传入必填参数并使用 `--output json` 获取机器可读结果;如需纯文本输出,设置 `NO_COLOR=1`。通过密钥管理或环境变量注入,勿在脚本中硬编码 Key。 --- diff --git a/README.md b/README.md index a19e81a9b..6d77a5ffe 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ Synchronous Batch Chat is non-streaming; explicit realtime/international/Token P ## Authentication -### Aliyun OAuth +### Aliyun OAuth (Recommended) `bl auth login --oauth` uses authorization code + PKCE, exchanges OAuth credentials for STS, and uses STS to obtain the Bailian Console token and sign OpenAPI requests. `--console-site ` selects the site (default `domestic`). The official IAM application ID and the authorization/token endpoints are built in for both sites, so a first online login needs no endpoint flags: @@ -226,7 +226,7 @@ Supported OAuth and OpenAPI routes are selected automatically. Confirm the pre-r Aliyun OAuth credentials are stored as **plaintext** in a separate `oauth/.json` file under the CLI config directory, not in the main config. Writes are atomic; the cache directory and files use `0700` and `0600` on Unix, and Windows requires private ACLs. No Keychain or system credential service is used. Keep this directory private and out of shared backups. Sign in again if you used the previous encrypted format; it is not migrated. OAuth refresh and STS renewal are shared across Console and OpenAPI calls. `auth status` only describes configured credentials; it does not validate or refresh them. Full `auth logout` or `auth logout --aliyun` revokes the RT; partial `--console`/`--open-api` logout disables only that domain. OAuth login preserves existing API keys and does not provision inference keys, initialize workspaces, modify hosts/proxies, or activate paid services. -### Console Browser Login (Recommended) +### Console Browser Login (Compatible) Console capability commands (model list, flow-agent list, workspace, usage queries, rate-limit increases, direct console calls) can use this browser login or Aliyun OAuth above. Browser login can create an ordinary API key when needed outside pre-release. Token Plan and other subscription plans cannot use `--console` for inference; use the API Key login below. @@ -286,14 +286,20 @@ Get an ordinary key from the [DashScope Console](https://bailian.console.aliyun. ### Alibaba Cloud OpenAPI AK/SK -Token Plan seat and member management and Console token refresh require OpenAPI credentials: Alibaba Cloud AccessKey/STS credentials or an Aliyun OAuth session. Online, run the AccessKey setup guide or get keys from the [RAM Console](https://ram.console.aliyun.com/manage/ak). The browser guide is not available in pre-release; use explicit pre-release AccessKey credentials or Aliyun OAuth instead. +Token Plan seat and member management and Console token refresh require OpenAPI credentials: Alibaba Cloud AccessKey/STS credentials or an Aliyun OAuth session. Online, run the AccessKey setup guide (`bl auth login --open-api`), open the console guide pages ([China](https://bailian.console.aliyun.com/aksk-guide) / [International](https://modelstudio.console.alibabacloud.com/aksk-guide)), or get keys from the [RAM Console](https://ram.console.aliyun.com/manage/ak). The browser guide is not available in pre-release; use explicit pre-release AccessKey credentials or Aliyun OAuth instead. For unattended control-plane use without a browser, inject `BAILIAN_ACCESS_KEY_ID` / `BAILIAN_ACCESS_KEY_SECRET` (or `ALIBABA_CLOUD_*`); for interactive OAuth on a headless host, use `bl auth login --oauth --no-browser` and paste the full callback URL from another device. > Recommended: create a RAM sub-account with minimum privileges instead of using the root account's AK/SK. ```bash -# Open the setup guide (when finished, paste your AccessKey back in the terminal) +# Open the setup guide (prints the guide URL and a paste-ready follow-up command) bl auth login --open-api +# Print the guide URL and follow-up command without opening a browser +bl auth login --open-api --no-browser + +# International guide page +bl auth login --open-api --console-site international --no-browser + # If you already have an AccessKey, configure it directly bl auth login --open-api --access-key-id LTAI5t... --access-key-secret ... ``` @@ -344,4 +350,5 @@ Memory documentation (Chinese): [capability matrix](https://github.com/modelstud | Aliyun Model Studio Console | https://bailian.console.aliyun.com/?spm=a2ty07.github | | Get API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | | Get Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | -| Get AccessKey | https://ram.console.aliyun.com/manage/ak | +| Get AccessKey (setup guide) | https://bailian.console.aliyun.com/aksk-guide | +| Get AccessKey (RAM) | https://ram.console.aliyun.com/manage/ak | diff --git a/README.zh.md b/README.zh.md index 6a21d4eb5..37dce552f 100644 --- a/README.zh.md +++ b/README.zh.md @@ -190,7 +190,7 @@ bl text chat --batch --model qwen-plus --message "你好" --timeout 3600 ## 认证方式 -### 阿里云 OAuth +### 阿里云 OAuth(推荐) `bl auth login --oauth` 使用授权码与 PKCE 获取 OAuth 凭据,再置换 STS;STS 同时用于获取百炼 Console token 和签名调用 OpenAPI。`--console-site ` 选择站点(缺省 `domestic`)。正式 IAM 应用 ID 与授权/换票端点已按站点内置,线上首次登录无需任何端点参数: @@ -225,7 +225,7 @@ BAILIAN_PRE=1 bl auth status 阿里云 OAuth 凭据以**明文**存放在配置目录下独立的 `oauth/.json` 中,不写入主配置。文件采用原子写入;Unix 上目录和文件分别为 `0700` 和 `0600`,Windows 要求私有 ACL。不使用钥匙串或系统凭据服务,请勿共享或备份到不受保护的位置。旧加密会话不迁移,需要重新登录。Console 与 OpenAPI 共享 OAuth/STS 续期。`auth status` 仅展示当前配置,不在线验证或刷新;完整 `auth logout` 或 `auth logout --aliyun` 会撤销 RT,部分 `--console`/`--open-api` 退出仅停用对应域。OAuth 登录保留已有模型 API Key,不创建推理密钥、不初始化工作空间、不改 hosts/代理,也不自动开通付费服务。 -### 控制台浏览器登录(推荐) +### 控制台浏览器登录(兼容) 控制台能力命令(模型列表、应用列表、工作空间、用量查询、限流提额、控制台直调)可使用此浏览器登录或上面的阿里云 OAuth。非预发环境下,浏览器登录可按需创建普通 API Key。Token Plan 等订阅计划推理不能使用 `--console`,请使用下面的 API Key 登录方式。 @@ -285,14 +285,20 @@ bl auth login --api-key ### 阿里云 OpenAPI AK/SK -Token Plan 的席位与成员管理及控制台换票需要 OpenAPI 凭据,可使用阿里云 AccessKey/STS 或阿里云 OAuth 会话。线上可运行配置引导,或前往 [RAM 控制台](https://ram.console.aliyun.com/manage/ak) 获取 AccessKey。预发不提供浏览器引导,请显式提供预发 AccessKey 或使用阿里云 OAuth。 +Token Plan 的席位与成员管理及控制台换票需要 OpenAPI 凭据,可使用阿里云 AccessKey/STS 或阿里云 OAuth 会话。线上可运行配置引导(`bl auth login --open-api`),打开控制台引导页([中国站](https://bailian.console.aliyun.com/aksk-guide) / [国际站](https://modelstudio.console.alibabacloud.com/aksk-guide)),或前往 [RAM 控制台](https://ram.console.aliyun.com/manage/ak) 获取 AccessKey。预发不提供浏览器引导,请显式提供预发 AccessKey 或使用阿里云 OAuth。无人值守的管控场景可通过环境变量注入 `BAILIAN_ACCESS_KEY_ID` / `BAILIAN_ACCESS_KEY_SECRET`(或 `ALIBABA_CLOUD_*`);本机无浏览器时的交互式 OAuth 使用 `bl auth login --oauth --no-browser`,在其它设备授权后粘回完整回调 URL。 > 建议:创建 RAM 子账号并授予最小权限,避免使用主账号 AK/SK。 ```bash -# 打开引导页(按照引导完成操作后,回到命令行粘贴 AccessKey) +# 打开引导页(会打印引导地址与可粘贴的后续配置命令) bl auth login --open-api +# 只打印引导地址与后续命令,不自动打开浏览器 +bl auth login --open-api --no-browser + +# 国际站引导页 +bl auth login --open-api --console-site international --no-browser + # 若已有 AccessKey,直接配置 bl auth login --open-api --access-key-id LTAI5t... --access-key-secret ... ``` @@ -335,12 +341,13 @@ bl update Memory 用户文档:[能力矩阵](https://github.com/modelstudioai/cli/blob/main/docs/memory/cli-matrix.md) · [命令指南](https://github.com/modelstudioai/cli/blob/main/docs/memory/memory-cli-guide.md)。 -| 资源 | 地址 | -| :---------------------- | :-------------------------------------------------------------------------------------------------- | -| 阿里云百炼 CLI 官方主页 | https://bailian.console.aliyun.com/cli?spm=a2ty07.github | -| DashScope API 文档 | https://help.aliyun.com/zh/model-studio/ | -| 通义千问模型列表 | https://help.aliyun.com/zh/model-studio/getting-started/models | -| 阿里云百炼控制台 | https://bailian.console.aliyun.com/?spm=a2ty07.github | -| 获取 API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | -| 获取 Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | -| 获取 AccessKey | https://ram.console.aliyun.com/manage/ak | +| 资源 | 地址 | +| :------------------------- | :-------------------------------------------------------------------------------------------------- | +| 阿里云百炼 CLI 官方主页 | https://bailian.console.aliyun.com/cli?spm=a2ty07.github | +| DashScope API 文档 | https://help.aliyun.com/zh/model-studio/ | +| 通义千问模型列表 | https://help.aliyun.com/zh/model-studio/getting-started/models | +| 阿里云百炼控制台 | https://bailian.console.aliyun.com/?spm=a2ty07.github | +| 获取 API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | +| 获取 Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | +| 获取 AccessKey(配置引导) | https://bailian.console.aliyun.com/aksk-guide | +| 获取 AccessKey(RAM) | https://ram.console.aliyun.com/manage/ak | diff --git a/packages/cli/README.md b/packages/cli/README.md index a19e81a9b..6d77a5ffe 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -191,7 +191,7 @@ Synchronous Batch Chat is non-streaming; explicit realtime/international/Token P ## Authentication -### Aliyun OAuth +### Aliyun OAuth (Recommended) `bl auth login --oauth` uses authorization code + PKCE, exchanges OAuth credentials for STS, and uses STS to obtain the Bailian Console token and sign OpenAPI requests. `--console-site ` selects the site (default `domestic`). The official IAM application ID and the authorization/token endpoints are built in for both sites, so a first online login needs no endpoint flags: @@ -226,7 +226,7 @@ Supported OAuth and OpenAPI routes are selected automatically. Confirm the pre-r Aliyun OAuth credentials are stored as **plaintext** in a separate `oauth/.json` file under the CLI config directory, not in the main config. Writes are atomic; the cache directory and files use `0700` and `0600` on Unix, and Windows requires private ACLs. No Keychain or system credential service is used. Keep this directory private and out of shared backups. Sign in again if you used the previous encrypted format; it is not migrated. OAuth refresh and STS renewal are shared across Console and OpenAPI calls. `auth status` only describes configured credentials; it does not validate or refresh them. Full `auth logout` or `auth logout --aliyun` revokes the RT; partial `--console`/`--open-api` logout disables only that domain. OAuth login preserves existing API keys and does not provision inference keys, initialize workspaces, modify hosts/proxies, or activate paid services. -### Console Browser Login (Recommended) +### Console Browser Login (Compatible) Console capability commands (model list, flow-agent list, workspace, usage queries, rate-limit increases, direct console calls) can use this browser login or Aliyun OAuth above. Browser login can create an ordinary API key when needed outside pre-release. Token Plan and other subscription plans cannot use `--console` for inference; use the API Key login below. @@ -286,14 +286,20 @@ Get an ordinary key from the [DashScope Console](https://bailian.console.aliyun. ### Alibaba Cloud OpenAPI AK/SK -Token Plan seat and member management and Console token refresh require OpenAPI credentials: Alibaba Cloud AccessKey/STS credentials or an Aliyun OAuth session. Online, run the AccessKey setup guide or get keys from the [RAM Console](https://ram.console.aliyun.com/manage/ak). The browser guide is not available in pre-release; use explicit pre-release AccessKey credentials or Aliyun OAuth instead. +Token Plan seat and member management and Console token refresh require OpenAPI credentials: Alibaba Cloud AccessKey/STS credentials or an Aliyun OAuth session. Online, run the AccessKey setup guide (`bl auth login --open-api`), open the console guide pages ([China](https://bailian.console.aliyun.com/aksk-guide) / [International](https://modelstudio.console.alibabacloud.com/aksk-guide)), or get keys from the [RAM Console](https://ram.console.aliyun.com/manage/ak). The browser guide is not available in pre-release; use explicit pre-release AccessKey credentials or Aliyun OAuth instead. For unattended control-plane use without a browser, inject `BAILIAN_ACCESS_KEY_ID` / `BAILIAN_ACCESS_KEY_SECRET` (or `ALIBABA_CLOUD_*`); for interactive OAuth on a headless host, use `bl auth login --oauth --no-browser` and paste the full callback URL from another device. > Recommended: create a RAM sub-account with minimum privileges instead of using the root account's AK/SK. ```bash -# Open the setup guide (when finished, paste your AccessKey back in the terminal) +# Open the setup guide (prints the guide URL and a paste-ready follow-up command) bl auth login --open-api +# Print the guide URL and follow-up command without opening a browser +bl auth login --open-api --no-browser + +# International guide page +bl auth login --open-api --console-site international --no-browser + # If you already have an AccessKey, configure it directly bl auth login --open-api --access-key-id LTAI5t... --access-key-secret ... ``` @@ -344,4 +350,5 @@ Memory documentation (Chinese): [capability matrix](https://github.com/modelstud | Aliyun Model Studio Console | https://bailian.console.aliyun.com/?spm=a2ty07.github | | Get API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | | Get Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | -| Get AccessKey | https://ram.console.aliyun.com/manage/ak | +| Get AccessKey (setup guide) | https://bailian.console.aliyun.com/aksk-guide | +| Get AccessKey (RAM) | https://ram.console.aliyun.com/manage/ak | diff --git a/packages/cli/README.zh.md b/packages/cli/README.zh.md index 6a21d4eb5..37dce552f 100644 --- a/packages/cli/README.zh.md +++ b/packages/cli/README.zh.md @@ -190,7 +190,7 @@ bl text chat --batch --model qwen-plus --message "你好" --timeout 3600 ## 认证方式 -### 阿里云 OAuth +### 阿里云 OAuth(推荐) `bl auth login --oauth` 使用授权码与 PKCE 获取 OAuth 凭据,再置换 STS;STS 同时用于获取百炼 Console token 和签名调用 OpenAPI。`--console-site ` 选择站点(缺省 `domestic`)。正式 IAM 应用 ID 与授权/换票端点已按站点内置,线上首次登录无需任何端点参数: @@ -225,7 +225,7 @@ BAILIAN_PRE=1 bl auth status 阿里云 OAuth 凭据以**明文**存放在配置目录下独立的 `oauth/.json` 中,不写入主配置。文件采用原子写入;Unix 上目录和文件分别为 `0700` 和 `0600`,Windows 要求私有 ACL。不使用钥匙串或系统凭据服务,请勿共享或备份到不受保护的位置。旧加密会话不迁移,需要重新登录。Console 与 OpenAPI 共享 OAuth/STS 续期。`auth status` 仅展示当前配置,不在线验证或刷新;完整 `auth logout` 或 `auth logout --aliyun` 会撤销 RT,部分 `--console`/`--open-api` 退出仅停用对应域。OAuth 登录保留已有模型 API Key,不创建推理密钥、不初始化工作空间、不改 hosts/代理,也不自动开通付费服务。 -### 控制台浏览器登录(推荐) +### 控制台浏览器登录(兼容) 控制台能力命令(模型列表、应用列表、工作空间、用量查询、限流提额、控制台直调)可使用此浏览器登录或上面的阿里云 OAuth。非预发环境下,浏览器登录可按需创建普通 API Key。Token Plan 等订阅计划推理不能使用 `--console`,请使用下面的 API Key 登录方式。 @@ -285,14 +285,20 @@ bl auth login --api-key ### 阿里云 OpenAPI AK/SK -Token Plan 的席位与成员管理及控制台换票需要 OpenAPI 凭据,可使用阿里云 AccessKey/STS 或阿里云 OAuth 会话。线上可运行配置引导,或前往 [RAM 控制台](https://ram.console.aliyun.com/manage/ak) 获取 AccessKey。预发不提供浏览器引导,请显式提供预发 AccessKey 或使用阿里云 OAuth。 +Token Plan 的席位与成员管理及控制台换票需要 OpenAPI 凭据,可使用阿里云 AccessKey/STS 或阿里云 OAuth 会话。线上可运行配置引导(`bl auth login --open-api`),打开控制台引导页([中国站](https://bailian.console.aliyun.com/aksk-guide) / [国际站](https://modelstudio.console.alibabacloud.com/aksk-guide)),或前往 [RAM 控制台](https://ram.console.aliyun.com/manage/ak) 获取 AccessKey。预发不提供浏览器引导,请显式提供预发 AccessKey 或使用阿里云 OAuth。无人值守的管控场景可通过环境变量注入 `BAILIAN_ACCESS_KEY_ID` / `BAILIAN_ACCESS_KEY_SECRET`(或 `ALIBABA_CLOUD_*`);本机无浏览器时的交互式 OAuth 使用 `bl auth login --oauth --no-browser`,在其它设备授权后粘回完整回调 URL。 > 建议:创建 RAM 子账号并授予最小权限,避免使用主账号 AK/SK。 ```bash -# 打开引导页(按照引导完成操作后,回到命令行粘贴 AccessKey) +# 打开引导页(会打印引导地址与可粘贴的后续配置命令) bl auth login --open-api +# 只打印引导地址与后续命令,不自动打开浏览器 +bl auth login --open-api --no-browser + +# 国际站引导页 +bl auth login --open-api --console-site international --no-browser + # 若已有 AccessKey,直接配置 bl auth login --open-api --access-key-id LTAI5t... --access-key-secret ... ``` @@ -335,12 +341,13 @@ bl update Memory 用户文档:[能力矩阵](https://github.com/modelstudioai/cli/blob/main/docs/memory/cli-matrix.md) · [命令指南](https://github.com/modelstudioai/cli/blob/main/docs/memory/memory-cli-guide.md)。 -| 资源 | 地址 | -| :---------------------- | :-------------------------------------------------------------------------------------------------- | -| 阿里云百炼 CLI 官方主页 | https://bailian.console.aliyun.com/cli?spm=a2ty07.github | -| DashScope API 文档 | https://help.aliyun.com/zh/model-studio/ | -| 通义千问模型列表 | https://help.aliyun.com/zh/model-studio/getting-started/models | -| 阿里云百炼控制台 | https://bailian.console.aliyun.com/?spm=a2ty07.github | -| 获取 API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | -| 获取 Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | -| 获取 AccessKey | https://ram.console.aliyun.com/manage/ak | +| 资源 | 地址 | +| :------------------------- | :-------------------------------------------------------------------------------------------------- | +| 阿里云百炼 CLI 官方主页 | https://bailian.console.aliyun.com/cli?spm=a2ty07.github | +| DashScope API 文档 | https://help.aliyun.com/zh/model-studio/ | +| 通义千问模型列表 | https://help.aliyun.com/zh/model-studio/getting-started/models | +| 阿里云百炼控制台 | https://bailian.console.aliyun.com/?spm=a2ty07.github | +| 获取 API Key | https://bailian.console.aliyun.com/cn-beijing/?spm=a2ty07.github&tab=app#/api-key | +| 获取 Token Plan API Key | https://bailian.console.aliyun.com/cn-beijing?spm=a2ty07.github&tab=plan#/efm/subscription/overview | +| 获取 AccessKey(配置引导) | https://bailian.console.aliyun.com/aksk-guide | +| 获取 AccessKey(RAM) | https://ram.console.aliyun.com/manage/ak | diff --git a/packages/cli/tests/e2e/aliyun-compat.e2e.test.ts b/packages/cli/tests/e2e/aliyun-compat.e2e.test.ts index be6f0fe52..30101302e 100644 --- a/packages/cli/tests/e2e/aliyun-compat.e2e.test.ts +++ b/packages/cli/tests/e2e/aliyun-compat.e2e.test.ts @@ -120,7 +120,7 @@ test.each(["en-US", "zh-CN"])( const root = await runCompatible(["--help"], language); expect(root.exitCode, root.stderr).toBe(0); expect(root.stderr).toContain("aliyun bl [flags]"); - expect(root.stderr).toContain("aliyun bl auth login --console"); + expect(root.stderr).toContain("aliyun bl auth login --oauth"); }, ); @@ -199,7 +199,7 @@ test.each(["text", "json"])( const result = await runCompatible(["app", "list", "--output", output]); expect(result.exitCode, result.stderr).toBe(3); const diagnostic = output === "json" ? JSON.parse(result.stderr).error.hint : result.stderr; - expect(diagnostic).toContain("aliyun bl auth login --console"); + expect(diagnostic).toContain("aliyun bl auth login --oauth"); expect(diagnostic).not.toMatch(/(?; @@ -41,13 +46,23 @@ export function makeAuthUiBridge(ctx: { identity: Identity; settings: Settings; authStore: AuthStore; - output: Pick; + output: Pick; localize?: (text: LocalizedText) => string; }): AuthUiBridge { const { identity, settings, authStore, localize, output } = ctx; const pre = settings.preControlPlane !== undefined; - const warnLoginFailure = (error: unknown) => { + const warnLoginFailure = (kind: "oauth" | "console", error: unknown) => { const message = error instanceof Error ? error.message : String(error); + if (kind === "oauth") { + output.warning({ + message: { + "en-US": `OAuth login failed: ${message}. Try console browser login (compatible) or auth login --api-key.`, + "zh-CN": `OAuth 登录失败:${message}。可改用控制台浏览器登录(兼容)或 auth login --api-key。`, + }, + code: "OAUTH_LOGIN_FAILED", + }); + return; + } output.warning({ message: { "en-US": `Console login failed: ${message}`, @@ -78,6 +93,10 @@ export function makeAuthUiBridge(ctx: { return { authenticated: methods.apiKey || methods.console || methods.openapi, methods, + modelApiKey: { + configured: methods.apiKey, + ...(a.apiKey ? { masked: maskToken(a.apiKey.token) } : {}), + }, primary, ...(pre ? { environment: "pre" as const } : {}), region: @@ -89,6 +108,22 @@ export function makeAuthUiBridge(ctx: { masked, }; }, + startOAuthLogin() { + // Keep launch Profile and environment bound for the entire callback. The UI only polls for completion. + try { + void runAliyunOAuthLogin({ + identity, + settings, + authStore, + localize, + output, + }).catch((error) => warnLoginFailure("oauth", error)); + } catch (error) { + // Endpoint preflight failures are synchronous; never tell the UI login was started. + warnLoginFailure("oauth", error); + throw error; + } + }, startConsoleLogin() { const origin = pre ? undefined : resolveConsoleOrigin(authStore.describe().console?.site); // Keep launch Profile and environment bound for the entire callback. The UI only polls for completion. @@ -99,10 +134,10 @@ export function makeAuthUiBridge(ctx: { { needApiKey: !pre && !authStore.stored().apiKey, }, - ).catch(warnLoginFailure); + ).catch((error) => warnLoginFailure("console", error)); } catch (error) { // Endpoint preflight failures are synchronous; never tell the UI login was started. - warnLoginFailure(error); + warnLoginFailure("console", error); throw error; } }, diff --git a/packages/commands/src/commands/auth/login.ts b/packages/commands/src/commands/auth/login.ts index 3b38c5fd1..4b9c426d6 100644 --- a/packages/commands/src/commands/auth/login.ts +++ b/packages/commands/src/commands/auth/login.ts @@ -21,7 +21,11 @@ import { runConsoleLogin, runConsoleTokenLogin, } from "./login-console.ts"; -import { resolveAkskGuideUrl, runOpenApiGuide } from "./login-openapi-guide.ts"; +import { + openApiPasteCommand, + resolveAkskGuideUrl, + runOpenApiGuide, +} from "./login-openapi-guide.ts"; const LOGIN_MODE_HINT = "Choose exactly one login mode: --api-key, --console, --open-api, or --oauth. / 请恰好选择一种登录方式:--api-key、--console、--open-api 或 --oauth。"; @@ -64,13 +68,13 @@ function previewPreLogin( export default defineCommand({ description: { "en-US": - "Authenticate with API key, console browser login, OpenAPI AK/SK, or Aliyun OAuth (credentials can coexist)", + "Authenticate with Aliyun OAuth (recommended), API key, console browser login (compatible), or OpenAPI AK/SK (credentials can coexist)", "zh-CN": - "使用 API Key、控制台浏览器登录、OpenAPI AK/SK 或阿里云 OAuth 进行认证(多种凭证可共存)", + "使用阿里云 OAuth(推荐)、API Key、控制台浏览器登录(兼容)或 OpenAPI AK/SK 进行认证(多种凭证可共存)", }, auth: "none", usageArgs: - "--api-key | --console [--no-browser | --token-stdin --console-region ] | --open-api [--no-browser] [--console-site ] [--access-key-id --access-key-secret ] | --oauth [--console-site ] [--console-region ] [--no-browser]", + "--oauth [--console-site ] [--console-region ] [--no-browser] | --api-key | --console [--no-browser | --token-stdin --console-region ] | --open-api [--no-browser] [--console-site ] [--access-key-id --access-key-secret ]", flags: { apiKey: { type: "string", @@ -92,9 +96,9 @@ export default defineCommand({ type: "switch", description: { "en-US": - "Sign in via browser (China by default; use --console-site international for the international site). Outside pre-release, creates an ordinary API key if needed; subscription plans require --api-key", + "Compatible: sign in via console browser callback (China by default; use --console-site international for the international site). Outside pre-release, creates an ordinary API key if needed; subscription plans require --api-key", "zh-CN": - "通过浏览器登录(默认中国站;国际站使用 --console-site international)。非预发环境下按需创建普通 API Key;订阅计划必须使用 --api-key 登录", + "兼容:通过控制台浏览器回调登录(默认中国站;国际站使用 --console-site international)。非预发环境下按需创建普通 API Key;订阅计划必须使用 --api-key 登录", }, }, noBrowser: { @@ -141,9 +145,9 @@ export default defineCommand({ type: "switch", description: { "en-US": - "Sign in with Aliyun OAuth (domestic by default; use --console-site international for the international site). Exchanges a console session with automatic renewal", + "Recommended: sign in with Aliyun OAuth (domestic by default; use --console-site international for the international site). Exchanges a console session with automatic renewal", "zh-CN": - "通过阿里云 OAuth 登录(默认中国站;国际站使用 --console-site international)。换取控制台会话并自动续期", + "推荐:通过阿里云 OAuth 登录(默认中国站;国际站使用 --console-site international)。换取控制台会话并自动续期", }, }, ...ALIYUN_OAUTH_LOGIN_FLAGS, @@ -165,19 +169,25 @@ export default defineCommand({ }, }, exampleArgs: [ + "--oauth", + "--oauth --console-site international", + "--oauth --no-browser", + "--api-key sk-xxxxx", "--console", "--console --no-browser --console-site international", "--console --token-stdin --console-site international --console-region ap-southeast-1", - "--api-key sk-xxxxx", "--open-api", "--open-api --no-browser --console-site international", "--open-api --access-key-id LTAIxxxxx --access-key-secret xxxxx", "--open-api --access-key-id LTAIxxxxx --access-key-secret xxxxx --console-site international", - "--oauth", - "--oauth --console-site international", - "--oauth --no-browser", ], notes: [ + { + "en-US": + "Recommended login order: --oauth, then --api-key for model credentials, --console as a compatible console browser path, then --open-api for AK/SK. Command auth domains (API Key / Console / OpenAPI) describe what a command needs at runtime; they are not the same as the recommended login method.", + "zh-CN": + "推荐登录顺序:--oauth,其次用 --api-key 配置模型凭证,--console 作为兼容的控制台浏览器登录,再用 --open-api 配置 AK/SK。命令鉴权域(API Key / Console / OpenAPI)描述的是运行时需要的凭证类型,不等于推荐的登录方式。", + }, { "en-US": "With --oauth outside pre-release, first login requires an explicit client ID and a confirmed authorization URL. In pre-release, endpoint overrides must match the trusted endpoints, and the CLI gateway must be configured. With --no-browser, paste the complete callback URL into this login session; this is not device authorization.", @@ -190,6 +200,18 @@ export default defineCommand({ "zh-CN": "IAM 重定向 URI 登记为 http://127.0.0.1:12345/oauth/callback。NativeApp 回环端口可随机,协议、主机和路径必须一致。", }, + { + "en-US": + "Headless environments: (1) Unattended console/OpenAPI control-plane use — set BAILIAN_ACCESS_KEY_ID and BAILIAN_ACCESS_KEY_SECRET (or ALIBABA_CLOUD_*). (2) Interactive OAuth without a local browser — run auth login --oauth --no-browser, authorize on another device, and paste the full callback URL back into this session.", + "zh-CN": + "无浏览器环境:(1) 无人值守的管控 / OpenAPI 场景 — 通过环境变量注入 BAILIAN_ACCESS_KEY_ID 与 BAILIAN_ACCESS_KEY_SECRET(或 ALIBABA_CLOUD_*)。(2) 本机无浏览器的交互式 OAuth — 运行 auth login --oauth --no-browser,在其它设备授权后把完整回调 URL 粘回本次登录会话。", + }, + { + "en-US": + "With --open-api and no AccessKey flags, the CLI prints the AccessKey setup guide URL (China: /aksk-guide on bailian.console.aliyun.com; international: /aksk-guide on modelstudio.console.alibabacloud.com) and a paste-ready follow-up command. Add --no-browser to print both without opening a browser.", + "zh-CN": + "使用 --open-api 且不带 AccessKey 参数时,CLI 会打印 AccessKey 配置引导地址(中国站:bailian.console.aliyun.com/aksk-guide;国际站:modelstudio.console.alibabacloud.com/aksk-guide)以及可粘贴的后续配置命令。加 --no-browser 则只打印、不自动打开浏览器。", + }, ], validate: (flags) => { const apiKeyMode = hasValue(flags.apiKey); @@ -410,12 +432,17 @@ export default defineCommand({ ); } const guideUrl = resolveAkskGuideUrl(site); + // Global `--config` lives in settings, not command flags; recoveryConfigName + // is set for an explicit flag or an active named profile. + const pasteConfig = settings.recoveryConfigName?.trim() || undefined; + const paste = openApiPasteCommand(ctx.identity, { site, config: pasteConfig }); if (settings.dryRun) { ctx.output.result( { action: "auth.login", method: "open-api-guide", url: guideUrl, + paste_command: paste, no_browser: !!flags.noBrowser, config_file: store.path, }, @@ -423,21 +450,18 @@ export default defineCommand({ text: ctx.localize( flags.noBrowser ? { - "en-US": `Would print the AccessKey setup URL without opening a browser:\n${guideUrl}`, - "zh-CN": `将打印 AccessKey 配置地址,不自动打开浏览器:\n${guideUrl}`, + "en-US": `Would print the AccessKey setup URL and follow-up command without opening a browser:\n${guideUrl}\n${paste}`, + "zh-CN": `将打印 AccessKey 配置地址与后续命令,不自动打开浏览器:\n${guideUrl}\n${paste}`, } : { - "en-US": `Would open the AccessKey setup URL in your browser:\n${guideUrl}`, - "zh-CN": `将在浏览器中打开 AccessKey 配置地址:\n${guideUrl}`, + "en-US": `Would open the AccessKey setup URL in your browser and print the follow-up command:\n${guideUrl}\n${paste}`, + "zh-CN": `将在浏览器中打开 AccessKey 配置地址并打印后续命令:\n${guideUrl}\n${paste}`, }, ), }, ); return; } - // Global `--config` lives in settings, not command flags; recoveryConfigName - // is set for an explicit flag or an active named profile. - const pasteConfig = settings.recoveryConfigName?.trim() || undefined; const { url, browserOpened } = await runOpenApiGuide(deps, { site, config: pasteConfig, diff --git a/packages/commands/src/commands/auth/status.ts b/packages/commands/src/commands/auth/status.ts index f4be4788b..b1f1e3f96 100644 --- a/packages/commands/src/commands/auth/status.ts +++ b/packages/commands/src/commands/auth/status.ts @@ -70,6 +70,7 @@ export default defineCommand({ const environmentLine = pre ? localize({ "en-US": "Control plane: pre-release", "zh-CN": "控制面:预发" }) : undefined; + const oauthLogin = formatCommand(identity, "auth login --oauth"); const apiKeyLogin = formatCommand(identity, "auth login --api-key "); const consoleLogin = formatCommand(identity, "auth login --console"); const openApiGuide = formatCommand(identity, "auth login --open-api"); @@ -83,8 +84,8 @@ export default defineCommand({ "zh-CN": `模型命令使用独立凭证。要配置模型 API Key,请运行 ${apiKeyLogin}`, } : { - "en-US": `No model API key is configured. Model commands need: ${consoleLogin}, or ${apiKeyLogin}`, - "zh-CN": `尚未配置模型 API Key。模型命令需要:${consoleLogin},或 ${apiKeyLogin}`, + "en-US": `No model API key is configured. Recommended: ${apiKeyLogin}. OAuth (${oauthLogin}) does not create a model API key.`, + "zh-CN": `尚未配置模型 API Key。推荐:${apiKeyLogin}。OAuth(${oauthLogin})不会创建推理 Key。`, }, ); const openApiHint = openapi @@ -110,8 +111,9 @@ export default defineCommand({ config_file: configFile, message: "Not authenticated.", hint: [ + `Aliyun OAuth: ${oauthLogin} (recommended)`, `API key (model): ${apiKeyLogin} or DASHSCOPE_API_KEY`, - `Console gateway: ${consoleLogin}`, + `Console gateway: ${consoleLogin} (compatible)`, `OpenAPI (AK/SK): ${pre ? openApiLogin : `${openApiGuide} (browser guide; or paste --access-key-id / --access-key-secret)`}`, `Get API Key: ${API_KEY_PAGE}`, ].join("\n"), @@ -166,7 +168,7 @@ export default defineCommand({ ); } else { lines.push( - ` Console gateway: not configured (run ${formatCommand(identity, "auth login --console")})`, + ` Console gateway: not configured (recommended: ${oauthLogin}; compatible: ${consoleLogin})`, ); } if (openapi) { diff --git a/packages/commands/src/commands/config/ui-html.ts b/packages/commands/src/commands/config/ui-html.ts index a0dd930e3..1396b9e9e 100644 --- a/packages/commands/src/commands/config/ui-html.ts +++ b/packages/commands/src/commands/config/ui-html.ts @@ -99,16 +99,21 @@ const PAGE_HTML = ` /* Account (top-right) */ .account { position: fixed; top: 18px; right: 22px; z-index: 40; } + .acct-login-actions { display: inline-flex; align-items: center; gap: 8px; } + .acct-login-actions[hidden] { display: none; } .acct-login { display: inline-flex; align-items: center; justify-content: center; gap: 7px; font: inherit; font-size: 14px; font-weight: 700; padding: 9px 22px; border-radius: var(--r-pill); border: none; background: var(--ink); color: #fff; cursor: pointer; box-shadow: var(--shadow-lift); transition: transform var(--t-fast), box-shadow var(--t-fast), background var(--t-fast); } .acct-login:hover { transform: translateY(-1px); box-shadow: 0 8px 22px rgba(0,0,0,.24); } .acct-login[disabled] { opacity: .65; cursor: default; transform: none; box-shadow: var(--shadow-lift); } + .acct-login-secondary { font-size: 12px; font-weight: 600; padding: 8px 14px; background: #fff; color: var(--ink); + border: 1px solid var(--line); box-shadow: none; } + .acct-login-secondary:hover { border-color: var(--blue); color: var(--blue); background: #fff; box-shadow: none; } .avatar { width: 40px; height: 40px; border-radius: 50%; border: 2px solid #fff; cursor: pointer; padding: 0; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; color: #fff; box-shadow: var(--shadow-lift); } .avatar svg { width: 20px; height: 20px; } .avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; display: block; } - .acct-login[hidden], .avatar[hidden] { display: none; } + .avatar[hidden] { display: none; } .acct-menu { position: absolute; top: 50px; right: 0; width: 270px; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow-lift); padding: 14px; } .acct-menu-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; } @@ -475,7 +480,10 @@ const PAGE_HTML = `