From 104a08c75a388ca3c1432f5dd2d31516c81243cc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 4 Jan 2026 12:26:47 -0600 Subject: [PATCH] Add OpenRouter OAuth --- docs/OAUTH.md | 44 ++++++- src/AppRouter.tsx | 2 + src/components/AddAIProviderDialog.tsx | 89 +++++++++++++- src/hooks/useOAuth.ts | 2 +- src/hooks/useOpenRouterOAuth.ts | 164 +++++++++++++++++++++++++ src/pages/AISettings.tsx | 14 ++- src/pages/OpenRouterOAuth.tsx | 96 +++++++++++++++ 7 files changed, 403 insertions(+), 8 deletions(-) create mode 100644 src/hooks/useOpenRouterOAuth.ts create mode 100644 src/pages/OpenRouterOAuth.tsx diff --git a/docs/OAUTH.md b/docs/OAUTH.md index a165759e..87c890f1 100644 --- a/docs/OAUTH.md +++ b/docs/OAUTH.md @@ -15,6 +15,7 @@ The OAuth system consists of three main components: - **GitHub** - Git repository hosting (with PKCE support) - **Netlify** - Static site deployment - **Vercel** - Static site deployment +- **OpenRouter** - AI model routing (with PKCE support) ## File Structure @@ -24,11 +25,13 @@ src/ │ ├── useOAuth.ts # Generic OAuth hook (core logic) │ ├── useGitHubOAuth.ts # GitHub-specific configuration │ ├── useNetlifyOAuth.ts # Netlify-specific configuration -│ └── useVercelOAuth.ts # Vercel-specific configuration +│ ├── useVercelOAuth.ts # Vercel-specific configuration +│ └── useOpenRouterOAuth.ts # OpenRouter-specific configuration ├── pages/ │ ├── GitHubOAuth.tsx # GitHub OAuth callback page │ ├── NetlifyOAuth.tsx # Netlify OAuth callback page -│ └── VercelOAuth.tsx # Vercel OAuth callback page +│ ├── VercelOAuth.tsx # Vercel OAuth callback page +│ └── OpenRouterOAuth.tsx # OpenRouter OAuth callback page └── AppRouter.tsx # Routes for OAuth callbacks ``` @@ -190,6 +193,7 @@ const config: OAuthConfig = { - ✅ GitHub - Supports PKCE (enabled) - ❌ Netlify - Doesn't support PKCE (disabled) - ❌ Vercel - Doesn't support PKCE (disabled) +- ✅ OpenRouter - Requires PKCE (enabled) ### Scope Parameter @@ -199,6 +203,42 @@ The `scope` parameter is optional in the OAuth configuration. Some providers (li - ✅ GitHub - Uses scopes (e.g., `repo workflow`) - ❌ Netlify - No scope parameter (permissions set in app settings) - ✅ Vercel - Uses scopes (e.g., `deployments:write projects:read`) +- ❌ OpenRouter - No scope parameter (uses PKCE for authorization) + +### OpenRouter Custom OAuth Implementation + +OpenRouter uses a custom OAuth flow that differs from the standard OAuth 2.0 specification. Instead of using the generic `useOAuth` hook, it has a dedicated `useOpenRouterOAuth` hook with the following key differences: + +1. **No Client Credentials**: OpenRouter doesn't require client ID or client secret - it uses PKCE only +2. **Authorization URL**: Uses `callback_url` parameter instead of `redirect_uri` +3. **Token Exchange**: Posts to `/api/v1/auth/keys` with a simplified request body +4. **Response Format**: Returns `{ key }` instead of standard `{ access_token }` +5. **PKCE Required**: Must use PKCE with S256 code challenge method +6. **No User Info**: Doesn't provide a user info endpoint after authentication +7. **No Environment Variables**: No configuration needed - works out of the box + +**Authorization URL Format:** +``` +https://openrouter.ai/auth?callback_url=&code_challenge=&code_challenge_method=S256 +``` + +**Token Exchange Request:** +```json +{ + "code": "", + "code_verifier": "", + "code_challenge_method": "S256" +} +``` + +**Token Exchange Response:** +```json +{ + "key": "sk-or-v1-..." +} +``` + +This custom implementation is fully contained in `useOpenRouterOAuth.ts` and doesn't rely on the generic OAuth hook. Unlike other providers, OpenRouter's OAuth doesn't require any environment variables or application registration - it works immediately without configuration. ## Adding a New OAuth Provider diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index dd0f5e75..04d2723d 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -18,6 +18,7 @@ import AboutSettings from "./pages/AboutSettings"; import GitHubOAuth from "./pages/GitHubOAuth"; import NetlifyOAuth from "./pages/NetlifyOAuth"; import VercelOAuth from "./pages/VercelOAuth"; +import OpenRouterOAuth from "./pages/OpenRouterOAuth"; import { NIP19Page } from "./pages/NIP19Page"; import NotFound from "./pages/NotFound"; import { ProjectView } from "./pages/ProjectView"; @@ -39,6 +40,7 @@ export function AppRouter() { } /> } /> } /> + } /> }> } /> } /> diff --git a/src/components/AddAIProviderDialog.tsx b/src/components/AddAIProviderDialog.tsx index f653bc8b..127428e3 100644 --- a/src/components/AddAIProviderDialog.tsx +++ b/src/components/AddAIProviderDialog.tsx @@ -1,6 +1,6 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { Bot } from 'lucide-react'; +import { Bot, ChevronDown } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { ExternalInput } from '@/components/ui/external-input'; @@ -12,15 +12,31 @@ import { DialogTitle, DialogFooter, } from '@/components/ui/dialog'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; import { ExternalFavicon } from '@/components/ExternalFavicon'; import { Link } from 'react-router-dom'; import type { PresetProvider } from '@/lib/aiProviderPresets'; +interface OAuthHook { + isOAuthConfigured: boolean; + isLoading: boolean; + error: string | null; + initiateOAuth: () => void; +} + interface AddAIProviderDialogProps { open: boolean; onOpenChange: (open: boolean) => void; preset: PresetProvider; isLoggedIntoNostr: boolean; + oauthHook?: OAuthHook | null; + forceManualEntry?: boolean; + onSetForceManualEntry?: (force: boolean) => void; onAdd: (apiKey: string) => void; } @@ -29,6 +45,9 @@ export function AddAIProviderDialog({ onOpenChange, preset, isLoggedIntoNostr, + oauthHook, + forceManualEntry = false, + onSetForceManualEntry, onAdd, }: AddAIProviderDialogProps) { const { t } = useTranslation(); @@ -36,16 +55,27 @@ export function AddAIProviderDialog({ const [termsAgreed, setTermsAgreed] = useState(false); const showNostrLoginRequired = preset.nostr && !isLoggedIntoNostr; + const isOAuthConfigured = oauthHook?.isOAuthConfigured ?? false; + const isOAuthLoading = oauthHook?.isLoading ?? false; + const oauthError = oauthHook?.error ?? null; const handleAdd = () => { onAdd(apiKey); setApiKey(''); - setTermsAgreed(false); onOpenChange(false); }; + // Reset state when dialog closes + const handleOpenChange = (open: boolean) => { + if (!open) { + setApiKey(''); + setTermsAgreed(false); + } + onOpenChange(open); + }; + return ( - +
@@ -73,6 +103,57 @@ export function AddAIProviderDialog({
+ ) : isOAuthConfigured && !forceManualEntry ? ( + // Show OAuth button if OAuth is configured and not forced to manual +
+
+ + + + + + + onSetForceManualEntry?.(true)}> + Enter API key + + + +
+ {oauthError && ( +

+ {oauthError} +

+ )} +
) : (
{!preset.nostr && preset.apiKeysURL && ( @@ -123,7 +204,7 @@ export function AddAIProviderDialog({ )}
- {!showNostrLoginRequired && ( + {!showNostrLoginRequired && !(isOAuthConfigured && !forceManualEntry) && (