Files
bear-cli/README.md
alex 870011f8a7 feat: 實作 bear CLI PAT 模式 MVP(login/whoami/token/logout/status)
- 新增 Elixir + Mix escript 專案(Req 0.5+、Jason)
- login --token <PAT>:以 PAT 驗證 /userinfo 後寫入憑證檔(0600)
- whoami:GET /userinfo 顯示身分 claims(支援 --json)
- token:印出 access token 供 pipe(--refresh 在 PAT 模式忽略)
- logout:清除本機憑證;status:純本機判定
- 憑證檔採原子寫入(rename)、0600、O_CREAT|O_EXCL 防 symlink
- 全域選項:--issuer/--config/--json/-v/-h/--version
- 29 個單元測試;mix precommit(compile --warnings-as-errors + format + test)
- 更新 README.md 與 docs/commands.md 反映 PAT 模式 MVP

Device Flow 登入待伺服器端 P1(bear 倉庫)完成後再接。issue #2
2026-08-31 11:14:12 +08:00

122 lines
4.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🐻 Bear CLI
**Bear CLI** 是 [澳特科技](https://alterminal.com)(Alter Technology)單點登入系統 [Bear](https://gitea.alterminal.com/alterminal/bear) 的**終端機(CLI)客戶端**:讓使用者在 Linux 終端機直接以 Bear 帳號登入、查詢身分、取得並續期 Token,作為 SSO 的終端入口。
> **狀態:已實作 PAT 模式 MVP(Elixir + Req)**。`login --token <PAT>`、`whoami`、`token`、`logout`、`status` 已可用;Device Authorization Grant(RFC 8628)登入待伺服器端 P1(bear 倉庫)完成後再接。
> 伺服器端採用的 OIDC **Device Authorization Grant(RFC 8628)** 方向,詳見 bear 倉庫的設計文件 `docs/cli-feature-plan.md`(issue #17)。
---
## 為什麼需要 CLI
Bear 目前所有流程都依賴「瀏覽器」:登入 `/login`、儀表板 Launch、Relying Party 走 Authorization Code Flow + PKCE。但**終端機沒有瀏覽器可互動**,無法完成 `redirect_uri` 回跳,因此需要一個為「無瀏覽器/輸入受限裝置」設計的登入流程,讓 `bear` 指令也能以 OIDC 身分登入並取用 Token。
---
## 指令總覽
| 指令 | 說明 | 階段 |
|------|------|------|
| `bear login` | 啟動 Device Flow 登入(取得授權碼 → 兌換 Token) | P2 |
| `bear whoami` | 顯示目前登入身分(`GET /userinfo`) | P2 |
| `bear token [--refresh]` | 印出 access token(過期自動 refresh),供 pipe 給其他工具 | P2 |
| `bear logout` | 撤銷 refresh token(`POST /revoke`) | P2 |
| `bear status` | 顯示登入狀態與 token 剩餘效期 | P2 |
| `bear apps` | 列出公開應用(需新增公開應用清單 API) | P3 |
完整指令規格(選項、行為、輸出、退出碼)見 [`docs/commands.md`](docs/commands.md)。
---
## 實作狀態(MVP)
目前已實作**個人存取權杖(PAT)模式**的最小可用版本,涵蓋 P2 指令:
| 指令 | 狀態 |
|------|------|
| `bear login --token <PAT>` | ✅ 已實作(PAT 模式;Device Flow 待伺服器端 P1) |
| `bear whoami` | ✅ 已實作(`GET /userinfo`,Bearer PAT) |
| `bear token` | ✅ 已實作(印出 PAT;`--refresh` 在 PAT 模式下無作用) |
| `bear logout` | ✅ 已實作(清除本機憑證;PAT 需至網頁撤銷) |
| `bear status` | ✅ 已實作(純本機判定) |
| `bear apps` | ⏳ P3 暫緩 |
### 建置與執行
```bash
mix deps.get # 安裝依賴(Req、Jason)
mix test # 跑單元測試
mix escript.build # 產生單檔可執行 `bear`
./bear --help
./bear login --token <PAT> --issuer https://alterminal.com
```
> 發行格式目前採 `mix escript`(目標機需 Erlang);self-contained release(bakeware/burrito)仍為待定方向(見 `docs/commands.md` §7)。
---
## 全域選項
| 選項 | 說明 | 預設 |
|------|------|------|
| `--issuer URL` | Bear(OIDC Provider)位址 | `https://alterminal.com` |
| `--config PATH` | 設定檔路徑 | `~/.config/bear/config.json` |
| `--json` | 機器可讀輸出(JSON) | — |
| `-v, --verbose` | 顯示詳細日誌 | — |
| `-h, --help` | 顯示說明 | — |
| `--version` | 顯示版本 | — |
---
## 登入流程(`bear login`)
### PAT 模式(現行)
```bash
$ bear login --token <PAT>
已登入:alice@example.com
```
以個人存取權杖(PAT)直接驗證 `GET /userinfo`,成功後將 token 寫入本機憑證檔(0600)。全程免瀏覽器。
### Device Flow(待伺服器端 P1,示意)
```
$ bear login
正在向 https://alterminal.com 註冊裝置...
請在瀏覽器開啟:https://alterminal.com/device
輸入代碼:ABCD-EFGH
(等待授權中...)
已登入:alice@example.com
```
登入後,CLI 將 refresh token 寫入本機憑證檔(0600),access token 僅做短命快取。
---
## 憑證與設定檔
| 檔 | 路徑 | 內容 | 權限 |
|----|------|------|------|
| 設定 | `~/.config/bear/config.json` | `issuer`、`client_id`、`scope` | 0644 |
| 憑證 | `~/.local/state/bear/credentials.json` | `refresh_token`(優先)+快取的 `access_token`/`expires_at` | **0600** |
---
## 里程碑
| 階段 | 內容 |
|------|------|
| **P1 伺服器:Device Flow** | `device_codes` 資料表、`POST /device_authorization`、`GET/POST /device` 授權頁、token 端點 `device_code` grant、Discovery 更新(於 bear 倉庫) |
| **P2 CLI 登入** | 本倉庫實作 `login`/`whoami`/`token`/`logout`/`status`、憑證儲存 0600 |
| **P3 進階** | `bear apps`(公開應用清單 API)、QR 顯示、系統 keyring、發行二進位 |
---
## 技術方向(規劃)
- **語言**:Elixir(與 Bear 主專案一致),HTTP 一律使用 `Req`。
- **發行**:`mix escript`(單檔可執行)或 `mix release` 自包含二進位(待定)。
- **安全**:device_code 只存 SHA-256 雜湊、user_code 限流、憑證檔 0600、token 一律不入 log/commit。