|
| 1 | +import type { Plugin, PluginInput } from "@arctic-cli/plugin" |
| 2 | +import { openBrowserUrl } from "../codex-oauth/auth/browser" |
| 3 | + |
| 4 | +export const ArcticAnthropicAuth: Plugin = async (_: PluginInput) => { |
| 5 | + return { |
| 6 | + auth: { |
| 7 | + provider: "anthropic", |
| 8 | + |
| 9 | + methods: [ |
| 10 | + { |
| 11 | + label: "Claude.ai Account (OAuth)", |
| 12 | + type: "oauth" as const, |
| 13 | + async authorize() { |
| 14 | + // Generate OAuth parameters |
| 15 | + const clientId = "9d1c250a-e61b-44d9-88ed-5944d1962f5e" |
| 16 | + const redirectUri = "https://console.anthropic.com/oauth/code/callback" |
| 17 | + const scope = "org:create_api_key user:profile user:inference" |
| 18 | + |
| 19 | + // Generate PKCE challenge |
| 20 | + const codeVerifier = generateCodeVerifier() |
| 21 | + const codeChallenge = await generateCodeChallenge(codeVerifier) |
| 22 | + const state = generateRandomString(64) |
| 23 | + |
| 24 | + // Build authorization URL |
| 25 | + const params = new URLSearchParams({ |
| 26 | + code: "true", |
| 27 | + client_id: clientId, |
| 28 | + response_type: "code", |
| 29 | + redirect_uri: redirectUri, |
| 30 | + scope, |
| 31 | + code_challenge: codeChallenge, |
| 32 | + code_challenge_method: "S256", |
| 33 | + state, |
| 34 | + }) |
| 35 | + |
| 36 | + const url = `https://claude.ai/oauth/authorize?${params.toString()}` |
| 37 | + |
| 38 | + // Open browser automatically |
| 39 | + openBrowserUrl(url) |
| 40 | + |
| 41 | + return { |
| 42 | + url, |
| 43 | + instructions: |
| 44 | + "Opening browser to authenticate with Claude.ai...\n\nIf the browser doesn't open automatically, visit the URL above.", |
| 45 | + method: "code" as const, |
| 46 | + async callback(code: string) { |
| 47 | + if (!code) { |
| 48 | + return { type: "failed" as const, error: "No authorization code provided" } |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + // Exchange code for tokens |
| 53 | + const tokenResponse = await fetch("https://console.anthropic.com/v1/oauth/token", { |
| 54 | + method: "POST", |
| 55 | + headers: { |
| 56 | + "Content-Type": "application/json", |
| 57 | + }, |
| 58 | + body: JSON.stringify({ |
| 59 | + grant_type: "authorization_code", |
| 60 | + code, |
| 61 | + redirect_uri: redirectUri, |
| 62 | + code_verifier: codeVerifier, |
| 63 | + client_id: clientId, |
| 64 | + }), |
| 65 | + }) |
| 66 | + |
| 67 | + if (!tokenResponse.ok) { |
| 68 | + const errorText = await tokenResponse.text() |
| 69 | + console.error("[Anthropic OAuth] Token exchange failed:", tokenResponse.status, errorText) |
| 70 | + return { type: "failed" as const, error: `Token exchange failed: ${tokenResponse.status}` } |
| 71 | + } |
| 72 | + |
| 73 | + const tokenData = await tokenResponse.json() |
| 74 | + |
| 75 | + if (!tokenData.access_token) { |
| 76 | + console.error("[Anthropic OAuth] No access token in response:", tokenData) |
| 77 | + return { type: "failed" as const, error: "No access token received" } |
| 78 | + } |
| 79 | + |
| 80 | + // Calculate expiration timestamp |
| 81 | + const expiresAt = Date.now() + (tokenData.expires_in ?? 3600) * 1000 |
| 82 | + |
| 83 | + return { |
| 84 | + type: "success" as const, |
| 85 | + access: tokenData.access_token, |
| 86 | + refresh: tokenData.refresh_token, |
| 87 | + expires: expiresAt, |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + console.error("[Anthropic OAuth] Error during token exchange:", error) |
| 91 | + return { |
| 92 | + type: "failed" as const, |
| 93 | + error: error instanceof Error ? error.message : "Unknown error", |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + } |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Generate a random string for PKCE code verifier or state |
| 107 | + */ |
| 108 | +function generateRandomString(length: number): string { |
| 109 | + const array = new Uint8Array(length) |
| 110 | + crypto.getRandomValues(array) |
| 111 | + return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("") |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Generate PKCE code verifier (43-128 characters) |
| 116 | + */ |
| 117 | +function generateCodeVerifier(): string { |
| 118 | + const array = new Uint8Array(32) |
| 119 | + crypto.getRandomValues(array) |
| 120 | + return base64UrlEncode(array) |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Generate PKCE code challenge from verifier |
| 125 | + */ |
| 126 | +async function generateCodeChallenge(verifier: string): Promise<string> { |
| 127 | + const encoder = new TextEncoder() |
| 128 | + const data = encoder.encode(verifier) |
| 129 | + const hash = await crypto.subtle.digest("SHA-256", data) |
| 130 | + return base64UrlEncode(new Uint8Array(hash)) |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Base64 URL-safe encoding (no padding) |
| 135 | + */ |
| 136 | +function base64UrlEncode(buffer: Uint8Array): string { |
| 137 | + const base64 = btoa(String.fromCharCode(...buffer)) |
| 138 | + return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "") |
| 139 | +} |
| 140 | + |
| 141 | +export default ArcticAnthropicAuth |
0 commit comments