Files
Arcrun/mcp/src/oauth/store.ts
T
Claude 7d9d478baa feat(mcp): OAuth 2.1 server for claude.ai remote connector; close plaintext-namespace bearer hole
在 arcrun-mcp worker 實作 MCP Authorization 規範(OAuth 2.1 + PKCE S256),
讓 claude.ai 遠端 connector 安全登入;並修掉「Bearer 明碼 namespace 直接放行」漏洞。

安全模型
- /authorize 同意頁以 owner secret(CF Secrets MCP_OWNER_SECRET)把關,只有 owner 知道 →
  只知 URL 的人走不完 OAuth、拿不到 token。
- access_token 是 /mcp 唯一接受的 bearer(預設);明碼 namespace 舊路徑移除(步驟 5 直接 401)。

實作 endpoint(掛 worker 根路徑)
- RFC 9728 /.well-known/oauth-protected-resource(+/mcp 變體)+ 401 帶
  WWW-Authenticate: Bearer resource_metadata=...
- RFC 8414 /.well-known/oauth-authorization-server(response_types=code, S256, none)
- RFC 7591 /register(public client,無 secret,無狀態不落地)
- GET/POST /authorize(PKCE S256 + owner-secret 閘 + redirect_uri 白名單)
- POST /token(authorization_code + PKCE 驗證 → access_token 綁定 owner namespace)

儲存鐵律
- authorization code / access token → 短效 KV OAUTH_KV(key 用 SHA-256 hash、帶 TTL、code 一次性)
- owner secret / static token → CF Secrets(非 KV、非明碼 var)
- DCR client / refresh token → 不落地(無狀態 / 不實作,避免長效機密進 KV)

相容決策
- 本機 CLI/GUI/Claude Code → 用真祕密 MCP_STATIC_TOKEN(CF Secret)取代舊明碼 namespace
- 官方 SaaS partner-key 路徑行為不變
- ALLOW_PLAINTEXT_NAMESPACE 逃生門預設關(僅遷移期)

驗證:tsc exit 0;vitest 42/42(oauth 22 + partner-auth 10 改測真實 middleware + 既有 10);
wrangler deploy --dry-run 打包過、OAUTH_KV binding 正確識別。設計文件 mcp/OAUTH.md。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d5jDbuqT5Htwv3Q88XXKk
2026-07-07 03:59:00 +00:00

79 lines
3.0 KiB
TypeScript
Raw 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.
// OAuth 短效認證儲存(KV)。儲存鐵律(leo + wiki):
// 只有「取得的暫時性認證」可進 KV,且帶 TTL——authorization code、access token 是也。
// 長效機密(owner secret)走 CF Secrets,不進此處。DCR client 不落地(見 OAUTH.md 相容決策)。
// KV key 一律用 SHA-256 hex(不把 raw code/token 當 key)→ 就算 KV list 也拿不到可用憑證。
import { sha256Hex } from "./crypto.js";
/** authorization code 綁定的資料(一次性;/token 驗證後即刪)。 */
export interface AuthCodeData {
client_id: string;
redirect_uri: string;
code_challenge: string;
code_challenge_method: string;
scope: string;
/** RFC 8707 resource:綁定 token 的目標 MCP serveraudience)。 */
resource: string;
/** 換發後 token 綁定的資料分區(owner namespace)。 */
namespace: string;
}
/** access token 綁定的資料。 */
export interface AccessTokenData {
namespace: string;
client_id: string;
scope: string;
/** token 的目標受眾(= 本 MCP server 的 canonical URI),驗證時比對。 */
aud: string;
/** 過期時間(epoch 秒),與 KV TTL 雙保險。 */
exp: number;
}
const CODE_PREFIX = "oauth:code:";
const TOKEN_PREFIX = "oauth:tok:";
/** authorization code 存活秒數(一次性、極短效)。 */
export const AUTH_CODE_TTL_SECONDS = 600;
/** 存 authorization codeTTL 極短)。回傳 raw code 給 client。 */
export async function putAuthCode(kv: KVNamespace, code: string, data: AuthCodeData): Promise<void> {
const key = CODE_PREFIX + (await sha256Hex(code));
await kv.put(key, JSON.stringify(data), { expirationTtl: AUTH_CODE_TTL_SECONDS });
}
/** 取出並「消費」authorization code(一次性:讀到即刪,防重放)。找不到回 null。 */
export async function consumeAuthCode(kv: KVNamespace, code: string): Promise<AuthCodeData | null> {
const key = CODE_PREFIX + (await sha256Hex(code));
const raw = await kv.get(key);
if (!raw) return null;
await kv.delete(key); // 一次性使用(OAuth 2.1code 用過必失效)
try {
return JSON.parse(raw) as AuthCodeData;
} catch {
return null;
}
}
/** 存 access tokenTTL = ttlSeconds)。回傳 raw token 給 client。 */
export async function putAccessToken(
kv: KVNamespace,
token: string,
data: AccessTokenData,
ttlSeconds: number,
): Promise<void> {
const key = TOKEN_PREFIX + (await sha256Hex(token));
await kv.put(key, JSON.stringify(data), { expirationTtl: ttlSeconds });
}
/** 查 access token → 綁定資料。找不到 / 過期回 null(KV TTL 到期會自動消失,另做 exp 雙檢)。 */
export async function getAccessToken(kv: KVNamespace, token: string): Promise<AccessTokenData | null> {
const key = TOKEN_PREFIX + (await sha256Hex(token));
const raw = await kv.get(key);
if (!raw) return null;
try {
const data = JSON.parse(raw) as AccessTokenData;
if (typeof data.exp === "number" && data.exp * 1000 < Date.now()) return null;
return data;
} catch {
return null;
}
}