feat: add OpenRouter OAuth PKCE authentication
Closes #7766
Summary
Add support for authenticating with OpenRouter using OAuth PKCE flow, allowing users to log in with their OpenRouter account instead of manually entering an API key.
Changes
- Add
OpenRouterAuthPlugininpackages/opencode/src/plugin/openrouter.ts - Register plugin in
INTERNAL_PLUGINSarray - Add hint text for OpenRouter in provider selection
Implementation
The plugin follows the same pattern as CodexAuthPlugin:
- Starts a local HTTP server on port 3000 to receive the OAuth callback
- Generates PKCE code verifier and challenge (S256)
- Opens browser to OpenRouter authorization URL
- Exchanges authorization code for API key via POST to
/api/v1/auth/keys - Stores the key as
type: "api"(OpenRouter returns a permanent API key, not refresh tokens)
Reference: https://openrouter.ai/docs/guides/overview/auth/oauth
Testing
- Run
opencode auth login - Select OpenRouter
- Select "OpenRouter Account"
- Browser opens to OpenRouter authorization page
- After authorization, callback is received and API key is stored
Thanks for your contribution!
This PR doesn't have a linked issue. All PRs must reference an existing issue.
Please:
- Open an issue describing the bug/feature (if one doesn't exist)
- Add
Fixes #<number>orCloses #<number>to this PR description
See CONTRIBUTING.md for details.
The following comment was made by an LLM, it may be inaccurate:
No duplicate PRs found
I've confirmed this works end to end. The OAuth flow completes successfully, the API key is stored, and requests work with the obtained credentials.
I understand that Zen is the recommended approach for accessing multiple models through a single curated API, and it makes sense as the primary path since the models are tested and benchmarked specifically for coding agents. That said, OpenRouter still has a significant user base who prefer it for various reasons (existing credits, specific model access, etc.), so having OAuth support brings it in line with how other providers handle authentication and improves the overall experience for those users.