feat: add anthropic oauth authentication to claude code sdk#2
Merged
Conversation
- Implement OAuth PKCE authentication flow based on opencode implementation - Add ClaudeAuth and AnthropicAuth classes for programmatic auth - Store credentials securely in ~/.claude/credentials.json with 600 permissions - Automatic token refresh when expired - Complete auth example with interactive flow - Export auth functions: startAuth, isAuthenticated, getAccessToken, logout - Add comprehensive test coverage for auth functionality - Update README with detailed authentication documentation Resolves instantlyeasy#10 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Co-authored-by: yolocode-ai <yolocodeai@users.noreply.github.com>
- renamed AuthAnthropic.access() to refresh() for clarity - removed storage dependency from refresh method - moved token storage logic to auth-example.ts - refresh now only handles token refresh, not storage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- new example that doesn't use filesystem storage - outputs credentials directly to stdout - demonstrates token refresh functionality - useful for temporary auth or CI/CD environments 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
removed packages that are not actually used in the implementation: - crypto: using node's built-in crypto module - node-fetch: using native fetch api 🤖 generated with [claude code](https://claude.ai/code) co-authored-by: claude <noreply@anthropic.com>
- removed AuthFileStorage class for manual storage handling - kept zod validation schemas as exported helpers - updated auth-example.ts to use manual file operations - exported OAuthCredentials, validateCredentials, isValidCredentials 🤖 generated with [claude code](https://claude.ai/code) co-authored-by: claude <noreply@anthropic.com>
d864cfc to
6b4daa7
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
summary
adds anthropic oauth authentication support to the claude code sdk, enabling programmatic authentication without requiring the claude cli.
why this matters
currently, the sdk only supports authentication through the claude cli (
claude login). this pr adds native oauth support, allowing:implementation
uses @openauthjs/openauth library for secure oauth pkce flow implementation.
oauth endpoints
https://claude.ai/oauth/authorizewithcode=trueparameterhttps://console.anthropic.com/v1/oauth/tokenhttps://console.anthropic.com/oauth/code/callbackapi
usage example
files changed
src/auth/anthropic.ts- core oauth implementation (85 lines)src/auth/validation.ts- zod validation schemasexamples/auth-example.ts- working authentication examplepackage.json- added @openauthjs/openauth and zod dependenciesREADME.md- authentication documentationtesting
✅ oauth url generation with proper pkce parameters
✅ correct endpoint configuration
✅ validation schemas working
✅ example successfully authenticates
🤖 generated with claude code