-
Notifications
You must be signed in to change notification settings - Fork 87
added browser auth with fallback #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sreedharsreeram
wants to merge
2
commits into
main
Choose a base branch
from
feat/browser-auth-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import { execFile } from "node:child_process" | ||
| import { randomBytes } from "node:crypto" | ||
| import * as fs from "node:fs" | ||
| import { | ||
| createServer, | ||
| type IncomingMessage, | ||
| type ServerResponse, | ||
| } from "node:http" | ||
| import type { AddressInfo } from "node:net" | ||
| import { arch, homedir, hostname, platform } from "node:os" | ||
| import * as path from "node:path" | ||
|
|
||
| const CONFIG_DIR = path.join(homedir(), ".openclaw") | ||
| const CONFIG_FILE = path.join(CONFIG_DIR, "openclaw.json") | ||
| const AUTH_BASE_URL = | ||
| process.env.SUPERMEMORY_AUTH_URL || | ||
| "https://console.supermemory.ai/auth/agent-connect" | ||
| const AUTH_TIMEOUT = Number(process.env.SUPERMEMORY_AUTH_TIMEOUT) || 60_000 | ||
| const API_URL = process.env.SUPERMEMORY_API_URL || "https://api.supermemory.ai" | ||
|
|
||
| export async function validateApiKey( | ||
| apiKey: string, | ||
| ): Promise<{ valid: boolean; error?: string }> { | ||
| try { | ||
| const res = await fetch(`${API_URL}/v3/session`, { | ||
| headers: { Authorization: `Bearer ${apiKey}` }, | ||
| signal: AbortSignal.timeout(8000), | ||
| }) | ||
| if (res.status === 401 || res.status === 403) { | ||
| return { valid: false, error: "Invalid API key" } | ||
| } | ||
| return { valid: true } | ||
| } catch { | ||
| return { valid: true } // network error — don't block, key format was already checked | ||
| } | ||
| } | ||
|
|
||
| export function saveApiKey(apiKey: string): void { | ||
| let config: Record<string, unknown> = {} | ||
| if (fs.existsSync(CONFIG_FILE)) { | ||
| try { | ||
| config = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8")) | ||
| } catch { | ||
| config = {} | ||
| } | ||
| } | ||
|
|
||
| if (!config.plugins) config.plugins = {} | ||
| const plugins = config.plugins as Record<string, unknown> | ||
| if (!plugins.entries) plugins.entries = {} | ||
| const entries = plugins.entries as Record<string, unknown> | ||
|
|
||
| const existing = | ||
| (entries["openclaw-supermemory"] as Record<string, unknown>) ?? {} | ||
| entries["openclaw-supermemory"] = { | ||
| ...existing, | ||
| enabled: true, | ||
| config: { | ||
| ...((existing.config as Record<string, unknown>) ?? {}), | ||
| apiKey, | ||
| }, | ||
| } | ||
|
|
||
| if (!fs.existsSync(CONFIG_DIR)) { | ||
| fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 }) | ||
| } | ||
| fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { | ||
| mode: 0o600, | ||
| }) | ||
| } | ||
|
|
||
| function openBrowser(url: string): void { | ||
| const onError = (err: Error | null) => { | ||
| if (err) console.error("Failed to open browser:", err.message) | ||
| } | ||
| if (process.platform === "win32") { | ||
| execFile("explorer.exe", [url], onError) | ||
| } else if (process.platform === "darwin") { | ||
| execFile("open", [url], onError) | ||
| } else { | ||
| execFile("xdg-open", [url], onError) | ||
| } | ||
| } | ||
|
|
||
| export interface AuthResult { | ||
| success: boolean | ||
| apiKey?: string | ||
| error?: string | ||
| } | ||
|
|
||
| export function startAuthFlow(): Promise<AuthResult> { | ||
| return new Promise((resolve) => { | ||
| let resolved = false | ||
| const stateToken = randomBytes(16).toString("hex") | ||
|
|
||
| const server = createServer((req: IncomingMessage, res: ServerResponse) => { | ||
| if (resolved) return | ||
|
|
||
| const url = new URL(req.url || "/", "http://localhost") | ||
|
|
||
| if (url.pathname === "/callback") { | ||
| const callbackState = url.searchParams.get("state") | ||
| if (callbackState !== stateToken) { | ||
| res.writeHead(403, { "Content-Type": "text/html" }) | ||
| res.end(errorHtml("Invalid state token")) | ||
| return | ||
| } | ||
|
|
||
| const apiKey = | ||
| url.searchParams.get("apikey") || url.searchParams.get("api_key") | ||
|
|
||
| if (apiKey?.startsWith("sm_")) { | ||
| // Validate the key before saving | ||
| validateApiKey(apiKey).then(({ valid, error: validationError }) => { | ||
| if (!valid) { | ||
| res.writeHead(400, { | ||
| "Content-Type": "text/html", | ||
| "Referrer-Policy": "no-referrer", | ||
| }) | ||
| res.end(errorHtml(validationError || "Invalid API key")) | ||
| resolved = true | ||
| clearTimeout(timer) | ||
| server.close() | ||
| resolve({ | ||
| success: false, | ||
| error: validationError || "Invalid API key", | ||
| }) | ||
| return | ||
| } | ||
| saveApiKey(apiKey) | ||
| res.writeHead(200, { | ||
| "Content-Type": "text/html", | ||
| "Referrer-Policy": "no-referrer", | ||
| }) | ||
| res.end(successHtml) | ||
| resolved = true | ||
| clearTimeout(timer) | ||
| server.close() | ||
| resolve({ success: true, apiKey }) | ||
| }) | ||
| } else { | ||
| res.writeHead(400, { | ||
| "Content-Type": "text/html", | ||
| "Referrer-Policy": "no-referrer", | ||
| }) | ||
| res.end(errorHtml("No API key received")) | ||
| resolved = true | ||
| clearTimeout(timer) | ||
| server.close() | ||
| resolve({ success: false, error: "No API key received" }) | ||
| } | ||
| } else { | ||
| res.writeHead(404) | ||
| res.end("Not Found") | ||
| } | ||
| }) | ||
|
|
||
| server.on("error", (err: Error) => { | ||
| if (!resolved) { | ||
| resolved = true | ||
| clearTimeout(timer) | ||
| resolve({ success: false, error: err.message }) | ||
|
sreedharsreeram marked this conversation as resolved.
|
||
| } | ||
| }) | ||
|
|
||
| // Listen on an ephemeral port; embed state token in callback URL so the | ||
| // console redirects it back and the CSRF check passes. | ||
| server.listen(0, "127.0.0.1", () => { | ||
| const { port } = server.address() as AddressInfo | ||
| const callbackUrl = `http://localhost:${port}/callback?state=${stateToken}` | ||
| const params = new URLSearchParams({ | ||
| callback: callbackUrl, | ||
| client: "openclaw", | ||
| hostname: hostname(), | ||
| os: `${platform()}-${arch()}`, | ||
| cwd: process.cwd(), | ||
| cli_version: "1.0.0", | ||
| }) | ||
| const authUrl = `${AUTH_BASE_URL}?${params.toString()}` | ||
|
|
||
| console.log("Opening browser for authentication...") | ||
| console.log(`If it doesn't open, visit: ${authUrl}`) | ||
| openBrowser(authUrl) | ||
| }) | ||
|
|
||
| const timer = setTimeout(() => { | ||
| if (!resolved) { | ||
| resolved = true | ||
| server.close() | ||
| resolve({ success: false, error: "Authentication timed out" }) | ||
| } | ||
| }, AUTH_TIMEOUT) | ||
| }) | ||
| } | ||
|
|
||
| const successHtml = `<!DOCTYPE html> | ||
| <html> | ||
| <head><title>Success</title></head> | ||
| <body style="font-family: system-ui; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; background: #0a0a0a; color: #fafafa;"> | ||
| <div style="text-align: center;"> | ||
| <h1 style="color: #22c55e;">✓ Connected!</h1> | ||
| <p>You can close this window and return to your terminal.</p> | ||
| </div> | ||
| </body> | ||
| </html>` | ||
|
|
||
| function errorHtml(message: string): string { | ||
| return `<!DOCTYPE html> | ||
| <html> | ||
| <head><title>Error</title></head> | ||
| <body style="font-family: system-ui; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; background: #0a0a0a; color: #fafafa;"> | ||
| <div style="text-align: center;"> | ||
| <h1 style="color: #ef4444;">✗ Connection Failed</h1> | ||
| <p>${message}. Please try again.</p> | ||
| </div> | ||
| </body> | ||
| </html>` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.