auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecontract-cli Auth
contract-cli Auth
本技能指导你如何在本仓库中使用 的登录与身份切换能力,并保持和当前实现一致。
contract-cliThis skill guides you on how to use the login and identity switching capabilities of in this repository, while maintaining consistency with the current implementation.
contract-cli适用范围
Scope of Application
- 首次初始化本地
profile - 以 身份走 OAuth 授权码 + PKCE 登录
user - 以 身份录入
botapp_id/app_secret - 查看或清理本地身份状态
- 切换默认业务身份
- 排查 和
config.json的本地持久化问题secrets.json
- Initialize local for the first time
profile - Log in as using OAuth authorization code + PKCE
user - Enter as
app_id/app_secretidentitybot - View or clean up local identity status
- Switch default business identity
- Troubleshoot local persistence issues of and
config.jsonsecrets.json
实现来源
Implementation Sources
- internal/cli/app.go
- internal/cli/auth_provider.go
- internal/config/store.go
- internal/config/secrets.go
- internal/cli/app.go
- internal/cli/auth_provider.go\信息 设备hes intrinsically输水 received_for铁片〞+更快_(方)](#)
- internal/config/store.go
- internal/config/secrets.go
配置初始化
Configuration Initialization
首次使用前,必须先执行:
bash
contract-cli config add --env dev当前实现只内置 环境,默认 profile 名为 。该命令会:
devcontract-group- 发现 well-known 元数据
- 保存 MCP server / resource / OAuth server 配置
- 将 初始化为
default_identityuser
Before first use, you must execute:
bash
contract-cli config add --env devThe current implementation only has the environment built-in, with the default profile named . This command will:
devcontract-group- Discover well-known metadata
- Save MCP server / resource / OAuth server configurations
- Initialize to
default_identityuser
身份模型
Identity Model
同一个 profile 下维护两种身份:
| 身份 | 命令 | 本地存储 | 当前实现 |
|---|---|---|---|
| | | 已实现 OAuth 登录 |
| | | 已实现凭据录入和 |
额外还有一个默认身份指针:
profiles.<name>.default_identity- 由 修改
contract-cli auth use --as user|bot - 成功后也会自动切换到对应身份
auth login --as ...
Two identities are maintained under the same profile:
| Identity | Command | Local Storage | Current Implementation |
|---|---|---|---|
| | | OAuth login implemented |
| | | Credential entry and |
There is also an additional default identity pointer:
profiles.<name>.default_identity- Modified by
contract-cli auth use --as user|bot - Automatically switches to the corresponding identity after successful
auth login --as ...
快速流程
Quick Workflow
user
登录
useruser
Login
userbash
contract-cli config add --env dev
contract-cli auth login --as user
contract-cli auth status --as user行为约束:
- 登录会自动注册
userclient_id - 使用授权码模式 + PKCE
- 会启动本地回调服务,回调地址来自 profile 中的
redirect_url - 默认自动打开浏览器;如需仅打印链接,使用
--no-open-browser - 登录成功后写入
identities.user.token
bash
contract-cli config add --env dev
contract-cli auth login --as user
contract-cli auth status --as userBehavior Constraints:
- login automatically registers
userclient_id - Uses authorization code mode + PKCE
- Starts a local callback service, with the callback URL from in the profile
redirect_url - Automatically opens the browser by default; use to only print the link
--no-open-browser - Writes to after successful login
identities.user.token
bot
登录
botbot
Login
botbash
contract-cli auth login --as bot --app-id "<app_id>" --app-secret "<app_secret>"
contract-cli auth status --as bot也可以通过环境变量提供凭据:
bash
export CONTRACT_CLI_BOT_APP_ID="<app_id>"
export CONTRACT_CLI_BOT_APP_SECRET="<app_secret>"
contract-cli auth login --as bot运行时也兼容旧变量 / ,但后续新增配置统一使用 。
DEMOCLI_BOT_APP_IDDEMOCLI_BOT_APP_SECRETCONTRACT_CLI_*凭据优先级固定为:
- 命令行参数
- 环境变量
- 本地已保存凭据
行为约束:
- 登录会先保存
bot,再调用app_id/app_secret兑换 tokentenant_access_token/internal - 不写入
app_secretconfig.json - token 成功后写入
identities.bot.token - token 兑换失败时,会保留新凭据,但不会切换默认身份到
bot - 登录成功后会切换
default_identity=bot
bash
contract-cli auth login --as bot --app-id "<app_id>" --app-secret "<app_secret>"
contract-cli auth status --as botCredentials can also be provided via environment variables:
bash
export CONTRACT_CLI_BOT_APP_ID="<app_id>"
export CONTRACT_CLI_BOT_APP_SECRET="<app_secret>"
contract-cli auth login --as botThe runtime is also compatible with legacy variables / , but new configurations will uniformly use going forward.
DEMOCLI_BOT_APP_IDDEMOCLI_BOT_APP_SECRETCONTRACT_CLI_*Credential priority is fixed as:
- Command-line arguments
- Environment variables
- Locally saved credentials
Behavior Constraints:
- login first saves
bot, then callsapp_id/app_secretto exchange for tokentenant_access_token/internal - is not written to
app_secretconfig.json - Writes to after successful token exchange
identities.bot.token - If token exchange fails, the new credentials are retained, but the default identity will not be switched to
bot - Switches after successful login
default_identity=bot
状态、退出与切换
Status, Logout and Switching
查看状态
View Status
bash
contract-cli auth status --as user
contract-cli auth status --as bot规则:
- 不传 时,
--as默认查看auth statususer - 显示
user或authorizedunauthorized - 显示
bot、authorized、expired或configuredunconfigured - 状态会显示
bot、Token Endpoint和过期时间(若有)Token Protocol: tenant_access_token/internal
bash
contract-cli auth status --as user
contract-cli auth status --as botRules:
- When is not passed,
--asdefaults to viewingauth statususer - shows
userorauthorizedunauthorized - shows
bot,authorized,expiredorconfiguredunconfigured - status displays
bot,Token Endpointand expiration time (if available)Token Protocol: tenant_access_token/internal
退出登录
Logout
bash
contract-cli auth logout --as user
contract-cli auth logout --as bot规则:
- 只清理
logout --as useruser.token - 只清理
logout --as bot,保留bot.token和对应 secretapp_id/app_secret - 不传 时,
--as默认处理auth logoutuser
bash
contract-cli auth logout --as user
contract-cli auth logout --as botRules:
- only clears
logout --as useruser.token - only clears
logout --as贴近_ya 弹 counts \ #... MoreAGMs regardlessCalculate, retainingbot.tokenand corresponding secretapp_id/app_secret - When is not passed,
--asdefaults to handlingauth logoutuser
切换默认身份
Switch Default Identity
bash
contract-cli auth use --as user
contract-cli auth use --as bot规则:
- 该命令只修改
default_identity - 不会重新登录
- 不会校验目标身份一定已拿到 token
bash
contract-cli auth use --as user
contract-cli auth use --as botRules:
- This command only modifies
default_identity - Does not re-login
- Does not verify that the target identity has obtained a token
本地文件
Local Files
默认路径如下,若设置了 ,则改为该目录:
CONTRACT_CLI_CONFIG_DIR~/.contract-cli/config.json~/.contract-cli/secrets.json
运行时也兼容旧的 以及历史默认目录 ,用于平滑读取已有本地登录态。
DEMOCLI_CONFIG_DIR~/.democli存储约束:
- 保存 profile、identity 元数据和 token
config.json - 只保存 bot 的
secrets.jsonapp_secret - 与
user.token分离存储,不共享bot.token - 旧版平铺 OAuth 字段会自动迁移到
identities.user
Default paths are as follows; if is set, it will be changed to that directory:
CONTRACT_CLI_CONFIG_DIR~/.contract-cli/config.json~/.contract-cli/secrets.json
The runtime is also compatible with the legacy and historical default directory for smooth reading of existing local login states.
DEMOCLI_CONFIG_DIR~/.democliStorage Constraints:
- saves profile, identity metadata and tokens
config.json - only saves
secrets.jsonof botapp_secret - and
user.tokenare stored separately and not sharedbot.token - Legacy flat OAuth fields are automatically migrated to
identities.user
安全规则
Security Rules
- 禁止在终端或文档中明文输出 、
app_secret、access_tokenrefresh_token - 不要把 登出描述成“删除凭据”,当前实现只清 token、不删
botapp_id/app_secret - 不要让用户误以为 会影响
default_identity或auth status的默认目标,这两个命令未传auth logout时仍按--asuser - 涉及写入、清理本地凭据时,先确认是在当前 profile 上操作
- Prohibit plaintext output of ,
app_secret,access_tokenin terminals or documentsrefresh_token - Do not describe bot logout as "deleting credentials"; the current implementation only clears tokens, not
app_id/app_secret - Do not let users mistakenly think that affects the default target of
default_identityorauth status; these commands still default toauth logoutwhenuseris not passed--as - When writing or cleaning up local credentials, first confirm that the operation is performed on the current profile
故障排查
Troubleshooting
- :先执行
user identity is not configuredcontract-cli config add --env dev - 浏览器未自动打开:改用 ,手动访问输出的授权链接
--no-open-browser - 回调超时:检查 对应端口是否可监听,必要时调大
redirect_url--timeout - bot 凭据不完整:补齐 或设置
--app-id/--app-secretCONTRACT_CLI_BOT_APP_ID/CONTRACT_CLI_BOT_APP_SECRET - bot 登录提示缺少 :说明 profile 过旧,重跑
bot_token_endpointcontract-cli config add --env dev --name <profile> - bot 状态显示 :重新执行
expiredcontract-cli auth login --as bot
- : Execute
user identity is not configuredfirstcontract-cli config add --env dev - Browser does not open automatically: Use instead and manually access the printed authorization link
--no-open-browser - Callback timeout: Check if the port corresponding to can be listened to; increase
redirect_urlif necessary--timeout - Incomplete bot credentials: Fill in or set
--app-id/--app-secretCONTRACT_CLI_BOT_APP_ID/CONTRACT_CLI_BOT_APP_SECRET - Bot login prompts missing : Indicates the profile is outdated; re-run
bot_token_endpointcontract-cli config add --env dev --name <profile> - Bot status shows : Re-execute
expired",contract-cli auth login --as bot