agent-auth-broker 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,605 @@
1
+ # Agent Auth Broker
2
+
3
+ AI Agent 的集中式凭证管理与授权代理服务。Agent 不直接持有任何 API Key 或 OAuth Token,而是通过 Broker 发起调用,Broker 负责权限校验、凭证注入、执行代理和审计日志。
4
+
5
+ ## 核心思路
6
+
7
+ ```
8
+ Agent (Claude / OpenClaw)
9
+ ↓ broker_call(connector, action, params)
10
+ MCP Server
11
+ ↓ 验证 Agent Token
12
+ Broker Core(权限检查 → 凭证解密 → 执行)
13
+ ↓ Bearer token 注入
14
+ 第三方 API(GitHub 等)
15
+ ```
16
+
17
+ **优势:**
18
+ - Agent 永远不接触真实凭证,token 泄露风险为零
19
+ - 细粒度权限控制,精确到操作级别(如只允许 `github:list_repos`,不允许 `github:create_issue`)
20
+ - 完整的不可篡改审计日志
21
+ - 支持参数级约束(如只能操作特定 org 下的仓库)
22
+
23
+ ---
24
+
25
+ ## 项目结构
26
+
27
+ ```
28
+ agent-auth-broker/
29
+ ├── apps/
30
+ │ ├── web/ # Next.js 14 — Admin UI + Broker API
31
+ │ │ └── prisma/schema.prisma
32
+ │ ├── mcp-server/ # MCP Server(stdio 传输)
33
+ │ │ └── skills/broker-auth/ # OpenClaw / Claude Code Skill 文件
34
+ │ └── cli/ # CLI 工具 — broker init/serve/validate
35
+ ├── packages/
36
+ │ ├── local-runtime/ # 纯本地运行时(YAML 驱动,无需数据库)
37
+ │ ├── core/ # 核心业务逻辑(数据库模式)
38
+ │ ├── connectors/ # 第三方服务适配器
39
+ │ ├── crypto/ # AES-256-GCM 加密工具
40
+ │ └── shared-types/ # 共享类型定义
41
+ ├── package.json # pnpm monorepo 根
42
+ └── turbo.json
43
+ ```
44
+
45
+ ---
46
+
47
+ ## 三种运行模式
48
+
49
+ | 模式 | 适用场景 | 外部依赖 | 配置方式 |
50
+ |------|---------|---------|---------|
51
+ | **File Mode** | 个人开发者、单 Agent | 无 | `broker.yaml` + 环境变量 |
52
+ | **Local Mode** | 小团队本地开发 | PostgreSQL | `.env` + 数据库 |
53
+ | **Remote Mode** | 生产 / 多用户 | PostgreSQL + Web Server | Web UI 管理 |
54
+
55
+ ---
56
+
57
+ ## 安装
58
+
59
+ ### 方式一:npm 全局安装(推荐)
60
+
61
+ ```bash
62
+ npm install -g agent-auth-broker
63
+ broker --version
64
+ ```
65
+
66
+ ### 方式二:npx 直接使用(无需安装)
67
+
68
+ ```bash
69
+ npx agent-auth-broker init
70
+ npx agent-auth-broker serve
71
+ ```
72
+
73
+ ### 方式三:从源码构建
74
+
75
+ ```bash
76
+ git clone <repo-url>
77
+ cd agent-auth-broker
78
+ pnpm install
79
+ pnpm build
80
+ # 使用 node 直接运行
81
+ node apps/cli/dist/index.js --version
82
+ ```
83
+
84
+ ---
85
+
86
+ ## 快速开始:File Mode(推荐)
87
+
88
+ 最轻量的接入方式,只需一个 YAML 配置文件和环境变量,无需数据库、无需 Web Server。
89
+
90
+ ### 1. 初始化配置
91
+
92
+ ```bash
93
+ broker init
94
+ # 或使用 npx:npx agent-auth-broker init
95
+ # 或从源码:node apps/cli/dist/index.js init
96
+ ```
97
+
98
+ 生成的 `broker.yaml`:
99
+
100
+ ```yaml
101
+ version: "1"
102
+
103
+ agents:
104
+ - id: my-agent
105
+ name: My AI Agent
106
+
107
+ credentials:
108
+ - id: github-main
109
+ connector: github
110
+ token: ${GITHUB_TOKEN} # 引用环境变量,凭证不落盘
111
+
112
+ policies:
113
+ - agent: my-agent
114
+ credential: github-main
115
+ actions:
116
+ - "*" # 允许所有操作
117
+
118
+ audit:
119
+ enabled: true
120
+ output: stdout # stdout 或 file
121
+ ```
122
+
123
+ ### 2. 设置环境变量
124
+
125
+ ```bash
126
+ export GITHUB_TOKEN="ghp_your_personal_access_token"
127
+ ```
128
+
129
+ ### 3. 验证和诊断
130
+
131
+ ```bash
132
+ broker validate # 验证配置文件格式
133
+ broker diagnose # 诊断凭证连接(会尝试调用 GitHub API)
134
+ ```
135
+
136
+ ### 4. 配置 MCP Server
137
+
138
+ 在 `claude_desktop_config.json` 或 `.claude/settings.json` 中:
139
+
140
+ ```json
141
+ {
142
+ "mcpServers": {
143
+ "auth-broker": {
144
+ "command": "node",
145
+ "args": ["/path/to/agent-auth-broker/apps/mcp-server/dist/index.js"],
146
+ "env": {
147
+ "BROKER_CONFIG": "/path/to/broker.yaml",
148
+ "GITHUB_TOKEN": "ghp_your_token"
149
+ }
150
+ }
151
+ }
152
+ }
153
+ ```
154
+
155
+ 或使用 CLI 的 `serve` 命令启动:
156
+
157
+ ```json
158
+ {
159
+ "mcpServers": {
160
+ "auth-broker": {
161
+ "command": "node",
162
+ "args": ["/path/to/agent-auth-broker/apps/cli/dist/index.js", "serve"],
163
+ "env": {
164
+ "GITHUB_TOKEN": "ghp_your_token"
165
+ }
166
+ }
167
+ }
168
+ }
169
+ ```
170
+
171
+ **触发条件:** `BROKER_CONFIG` 环境变量已设置,且 `BROKER_URL` 和 `DATABASE_URL` 未设置。
172
+
173
+ ---
174
+
175
+ ## broker.yaml 配置详解
176
+
177
+ ### 凭证配置
178
+
179
+ 支持两种凭证存储方式:
180
+
181
+ **方式一:环境变量引用(推荐)**
182
+
183
+ 凭证通过 `${ENV_VAR}` 语法引用环境变量,明文不写入配置文件:
184
+
185
+ ```yaml
186
+ credentials:
187
+ - id: github-main
188
+ connector: github
189
+ token: ${GITHUB_TOKEN}
190
+ ```
191
+
192
+ **方式二:AES-256-GCM 加密存储**
193
+
194
+ 适合凭证需要持久化的场景,需要配置 `encryption_key`:
195
+
196
+ ```yaml
197
+ version: "1"
198
+ encryption_key: ${BROKER_MASTER_KEY} # 主加密密钥(64 位十六进制)
199
+
200
+ credentials:
201
+ - id: github-main
202
+ connector: github
203
+ encrypted: "base64-encrypted-string" # 加密后的密文
204
+ ```
205
+
206
+ ### 权限策略
207
+
208
+ ```yaml
209
+ policies:
210
+ - agent: my-agent
211
+ credential: github-main
212
+ actions:
213
+ - "*" # 允许所有操作
214
+ # 或使用 scope 组(自动展开为多个操作):
215
+ # actions:
216
+ # - "github:read" # → list_repos, get_repo, list_issues, get_issue, list_prs, get_file, search_code
217
+ # - "github:write" # → create_issue, comment_issue, create_pr
218
+ # 或精确指定:
219
+ # actions:
220
+ # - "github:list_repos"
221
+ # - "github:create_issue"
222
+
223
+ # 可选:参数约束
224
+ # param_constraints:
225
+ # repo:
226
+ # pattern: "^myorg/.*" # repo 参数必须以 myorg/ 开头
227
+
228
+ # 可选:速率限制(滑动窗口算法)
229
+ # rate_limit:
230
+ # max_calls: 100
231
+ # window_seconds: 3600
232
+
233
+ # 可选:策略过期时间
234
+ # expires_at: "2025-12-31T23:59:59Z"
235
+ ```
236
+
237
+ ### Scope 组
238
+
239
+ 预定义的 scope 组可简化权限配置,一个 scope 名称自动展开为多个具体操作:
240
+
241
+ | Scope | 展开为 |
242
+ |-------|--------|
243
+ | `github:read` | `list_repos`, `get_repo`, `list_issues`, `get_issue`, `list_prs`, `get_file`, `search_code` |
244
+ | `github:write` | `create_issue`, `comment_issue`, `create_pr` |
245
+
246
+ ### 审计日志
247
+
248
+ ```yaml
249
+ audit:
250
+ enabled: true
251
+ output: stdout # 输出到 stderr(适合 MCP stdio 模式)
252
+ # output: file # 输出到文件
253
+ # file: ./broker-audit.log
254
+ ```
255
+
256
+ ---
257
+
258
+ ## CLI 命令参考
259
+
260
+ 所有命令支持 `-c, --config <path>` 指定配置文件路径,默认从当前目录向上查找 `broker.yaml`。
261
+
262
+ ```bash
263
+ # 初始化
264
+ broker init # 生成 broker.yaml 模板
265
+ broker init --force # 覆盖已有配置
266
+
267
+ # 验证和诊断
268
+ broker validate # 校验配置文件格式
269
+ broker diagnose # 检查环境变量和凭证连接
270
+
271
+ # Agent 管理
272
+ broker agent create <id> [-n name] # 创建 Agent
273
+ broker agent list # 列出所有 Agent
274
+ broker agent remove <id> # 移除 Agent
275
+
276
+ # Token 管理
277
+ broker token generate <agent-id> # 生成 Agent Token(仅显示一次)
278
+ broker token generate <agent-id> --force # 覆盖已有 token
279
+ broker token revoke <agent-id> # 撤销 Token
280
+ broker token list # 列出所有 Agent 的 Token 状态
281
+
282
+ # 凭证管理
283
+ broker credential add <connector> --env <VAR> # 添加环境变量引用凭证
284
+ broker credential add <connector> --token <val> # 直接指定 token(不推荐)
285
+ broker credential list # 列出所有凭证
286
+ broker credential remove <id> # 移除凭证
287
+
288
+ # 策略管理
289
+ broker policy set <agent> <credential> [--actions "*"] # 设置策略
290
+ broker policy list # 列出所有策略
291
+ broker policy remove <agent> <credential> # 移除策略
292
+
293
+ # 测试操作
294
+ broker test <connector> <action> # 测试 connector 操作
295
+ broker test github list_repos # 示例:列出 GitHub 仓库
296
+ broker test github list_issues -p '{"repo":"owner/repo"}' # 带参数
297
+ broker test github create_issue --dry-run # 仅权限检查,不实际调用
298
+
299
+ # 启动 MCP Server
300
+ broker serve # stdio 模式(支持配置热重载)
301
+ broker serve --agent <id> # 指定 Agent ID
302
+
303
+ # Web UI(File Mode 可视化管理)
304
+ broker ui # 启动 Web UI(默认端口 3200)
305
+ broker ui --port 8080 # 自定义端口
306
+ ```
307
+
308
+ > **提示:** 未全局安装时使用 `node apps/cli/dist/index.js` 替代 `broker`。
309
+
310
+ ### Token 认证
311
+
312
+ File Mode 支持通过 Token 认证 Agent 身份:
313
+
314
+ 1. 生成 Token:`broker token generate my-agent`
315
+ 2. Token 的 SHA-256 哈希存入 `broker.yaml` 的 `token_hash` 字段
316
+ 3. 在 MCP 配置中设置环境变量 `BROKER_AGENT_TOKEN` 传入 token
317
+ 4. MCP Server 启动时自动通过 hash 比对确定 Agent 身份
318
+
319
+ 未设置 `BROKER_AGENT_TOKEN` 时,退回到 `--agent` 参数或默认使用第一个 Agent。
320
+
321
+ ### 配置热重载
322
+
323
+ `broker serve` 运行时会自动监视 `broker.yaml` 文件变更,修改配置后无需重启 MCP Server:
324
+
325
+ - 使用 `fs.watch` 监视文件,300ms 防抖
326
+ - 重载失败时保留旧配置并输出错误日志
327
+ - 进程退出时自动清理 watcher
328
+
329
+ ### File Mode Web UI
330
+
331
+ `broker ui` 启动一个轻量级 Web 界面(默认 `http://localhost:3200`),方便不熟悉命令行的用户管理 `broker.yaml`:
332
+
333
+ - 使用 Node.js 内置 `http` 模块,零外部依赖
334
+ - 支持 Agents、Credentials、Policies 的增删操作
335
+ - YAML 预览(token 自动脱敏)
336
+ - 所有变更实时写入 `broker.yaml` 文件
337
+
338
+ ---
339
+
340
+ ## Local Mode 和 Remote Mode
341
+
342
+ 如果需要多用户管理、OAuth 授权流程或 Web UI,可以使用 Local Mode 或 Remote Mode。
343
+
344
+ ### 环境要求
345
+
346
+ - Node.js >= 20
347
+ - pnpm >= 9.15
348
+ - PostgreSQL >= 14
349
+
350
+ ### 安装和配置
351
+
352
+ ```bash
353
+ # 配置环境变量
354
+ cp apps/web/.env.example apps/web/.env
355
+ ```
356
+
357
+ `apps/web/.env` 必填项:
358
+
359
+ ```env
360
+ DATABASE_URL="postgresql://user:password@localhost:5432/agent_auth_broker"
361
+ BROKER_MASTER_KEY="your-64-char-hex-string"
362
+ NEXTAUTH_SECRET="your-nextauth-secret"
363
+ NEXTAUTH_URL="http://localhost:3100"
364
+ GITHUB_CLIENT_ID="your-github-client-id"
365
+ GITHUB_CLIENT_SECRET="your-github-client-secret"
366
+ ```
367
+
368
+ ```bash
369
+ # 初始化数据库
370
+ pnpm db:generate
371
+ pnpm db:push # 开发环境
372
+ # pnpm db:migrate # 生产环境
373
+
374
+ # 构建
375
+ pnpm build
376
+ ```
377
+
378
+ ### 启动 Web 服务
379
+
380
+ ```bash
381
+ pnpm dev:web # 访问 http://localhost:3100
382
+ ```
383
+
384
+ ### 使用流程
385
+
386
+ 1. **注册 Agent**:Admin UI → Agents → 创建 Agent → 复制 Token(`agnt_xxxx`,仅显示一次)
387
+ 2. **连接凭证**:Admin UI → Credentials → 通过 OAuth 连接 → 凭证自动加密存储
388
+ 3. **配置策略**:Admin UI → Agent Policies → 选择凭证、操作列表、参数约束
389
+ 4. **配置 MCP Server**
390
+
391
+ ### MCP Server 配置
392
+
393
+ #### Local Mode(直连数据库)
394
+
395
+ ```json
396
+ {
397
+ "mcpServers": {
398
+ "auth-broker": {
399
+ "command": "node",
400
+ "args": ["/path/to/apps/mcp-server/dist/index.js"],
401
+ "env": {
402
+ "DATABASE_URL": "postgresql://user:pass@localhost:5432/agent_auth_broker",
403
+ "BROKER_MASTER_KEY": "your-64-char-hex-string",
404
+ "BROKER_AGENT_TOKEN": "agnt_xxxxxxxxxxxx"
405
+ }
406
+ }
407
+ }
408
+ }
409
+ ```
410
+
411
+ **触发条件:** `DATABASE_URL` 已设置且 `BROKER_URL` 未设置。
412
+
413
+ #### Remote Mode(HTTP 调用)
414
+
415
+ ```json
416
+ {
417
+ "mcpServers": {
418
+ "auth-broker": {
419
+ "command": "node",
420
+ "args": ["/path/to/apps/mcp-server/dist/index.js"],
421
+ "env": {
422
+ "BROKER_URL": "http://localhost:3100",
423
+ "BROKER_AGENT_TOKEN": "agnt_xxxxxxxxxxxx"
424
+ }
425
+ }
426
+ }
427
+ }
428
+ ```
429
+
430
+ **触发条件:** `BROKER_URL` 已设置(最高优先级)。
431
+
432
+ ### 模式优先级
433
+
434
+ ```
435
+ BROKER_URL → Remote Mode
436
+ ↓ 未设置
437
+ DATABASE_URL → Local Mode
438
+ ↓ 未设置
439
+ BROKER_CONFIG → File Mode
440
+ ```
441
+
442
+ ---
443
+
444
+ ## MCP 工具说明
445
+
446
+ MCP Server 启动后自动暴露以下工具:
447
+
448
+ ### 固定工具
449
+
450
+ | 工具 | 说明 |
451
+ |------|------|
452
+ | `broker_call` | 通用调用入口,指定 connector + action + params |
453
+ | `broker_list_tools` | 列出当前 Agent 被授权的所有工具 |
454
+
455
+ ### 动态命名工具
456
+
457
+ 根据 Agent 的权限策略自动生成,格式为 `{connector}_{action}`,例如:
458
+
459
+ | 工具 | 等价调用 |
460
+ |------|---------|
461
+ | `github_list_repos` | `broker_call({ connector: "github", action: "list_repos" })` |
462
+ | `github_create_issue` | `broker_call({ connector: "github", action: "create_issue", ... })` |
463
+ | `github_list_prs` | `broker_call({ connector: "github", action: "list_prs", ... })` |
464
+
465
+ ---
466
+
467
+ ## GitHub Connector 操作列表
468
+
469
+ | action | 说明 | 必填参数 |
470
+ |--------|------|---------|
471
+ | `list_repos` | 列出已授权用户的仓库 | — |
472
+ | `get_repo` | 获取仓库信息 | `repo`(格式:`owner/repo`)|
473
+ | `list_issues` | 列出仓库的 Issue | `repo` |
474
+ | `get_issue` | 获取单个 Issue 详情 | `repo`, `issue_number` |
475
+ | `create_issue` | 创建 Issue | `repo`, `title` |
476
+ | `comment_issue` | 在 Issue 上添加评论 | `repo`, `issue_number`, `body` |
477
+ | `list_prs` | 列出 Pull Request | `repo` |
478
+ | `create_pr` | 创建 Pull Request | `repo`, `title`, `head`, `base` |
479
+ | `get_file` | 获取文件内容(自动 Base64 解码)| `repo`, `path` |
480
+ | `search_code` | 搜索代码 | `q` |
481
+
482
+ ---
483
+
484
+ ## 权限模型
485
+
486
+ ### 拒绝原因
487
+
488
+ | 错误码 | 含义 |
489
+ |--------|------|
490
+ | `DENIED_AGENT_INACTIVE` | Agent 已被停用 |
491
+ | `DENIED_NO_POLICY` | 该 Agent 没有针对此 connector 的策略 |
492
+ | `DENIED_ACTION_NOT_ALLOWED` | 操作不在允许列表中 |
493
+ | `DENIED_PARAM_CONSTRAINT` | 参数不满足约束条件 |
494
+ | `DENIED_CREDENTIAL_EXPIRED` | 凭证已过期或被撤销 |
495
+
496
+ ### 参数约束示例
497
+
498
+ 在策略的 `paramConstraints` 字段中配置 JSON Schema,限制参数范围:
499
+
500
+ ```json
501
+ {
502
+ "repo": {
503
+ "pattern": "^myorg/.*"
504
+ }
505
+ }
506
+ ```
507
+
508
+ 上述配置表示 `repo` 参数必须以 `myorg/` 开头,否则请求被拒绝。
509
+
510
+ ---
511
+
512
+ ## 加密方案
513
+
514
+ 凭证数据采用 AES-256-GCM **双层加密**:
515
+
516
+ ```
517
+ Master Encryption Key (MEK) — 来自环境变量 BROKER_MASTER_KEY
518
+ └─ 加密 → DEK(Data Encryption Key,每条凭证独立)
519
+ └─ 加密 → 凭证 JSON(含 access_token 等)
520
+ ```
521
+
522
+ - MEK 仅存在于环境变量中,不落盘
523
+ - DEK 加密后存入数据库(`encryptionKeyId` 字段)
524
+ - 凭证明文从不进入日志或响应体
525
+
526
+ ---
527
+
528
+ ## 审计日志
529
+
530
+ 所有操作(包括被拒绝的请求)均记录审计日志,应用层只允许 INSERT,不允许 UPDATE / DELETE。
531
+
532
+ 日志包含:
533
+ - Agent ID、Connector、Action
534
+ - 权限检查结果(`permissionResult`)
535
+ - 脱敏后的请求摘要(敏感字段替换为 `[REDACTED]`)
536
+ - HTTP 状态码、错误信息
537
+ - IP 地址、User-Agent
538
+
539
+ ---
540
+
541
+ ## OpenClaw 集成
542
+
543
+ 将 Skill 文件复制到 OpenClaw 的 rules/skills 目录:
544
+
545
+ ```bash
546
+ cp apps/mcp-server/skills/broker-auth/SKILL.md /path/to/rules/skills/broker-auth/SKILL.md
547
+ ```
548
+
549
+ 在 `openclaw.json` 中注册 MCP Server(选择 Local 或 Remote 模式之一)。
550
+
551
+ ---
552
+
553
+ ## 开发命令
554
+
555
+ ```bash
556
+ # 安装依赖
557
+ pnpm install
558
+
559
+ # 构建所有包
560
+ pnpm build
561
+
562
+ # 仅构建 Web / MCP Server / CLI
563
+ pnpm build:web
564
+ pnpm build:mcp
565
+ pnpm build --filter=agent-auth-broker
566
+
567
+ # 开发模式(热重载)
568
+ pnpm dev
569
+
570
+ # 数据库操作(Local/Remote Mode)
571
+ pnpm db:generate # 生成 Prisma Client
572
+ pnpm db:push # 推送 Schema(开发环境)
573
+ pnpm db:migrate # 创建迁移(生产环境)
574
+
575
+ # CLI(File Mode)
576
+ node apps/cli/dist/index.js init
577
+ node apps/cli/dist/index.js validate
578
+ node apps/cli/dist/index.js diagnose
579
+ node apps/cli/dist/index.js serve
580
+
581
+ # 代码检查
582
+ pnpm lint
583
+ ```
584
+
585
+ ---
586
+
587
+ ## 扩展 Connector
588
+
589
+ 在 `packages/connectors/src/` 下新建目录,实现 `ConnectorAdapter` 接口,然后在 `registry.ts` 中注册:
590
+
591
+ ```typescript
592
+ // packages/connectors/src/feishu/index.ts
593
+ export const feishuConnector: ConnectorAdapter = {
594
+ info: { id: 'feishu', name: '飞书', ... },
595
+ getActions() { return [...] },
596
+ async execute(action, params, credential) { ... },
597
+ }
598
+
599
+ // packages/connectors/src/registry.ts
600
+ import { feishuConnector } from './feishu/index'
601
+ const connectors = new Map([
602
+ ['github', githubConnector],
603
+ ['feishu', feishuConnector], // 注册新 connector
604
+ ])
605
+ ```