Skip to content

feat: add anthropic oauth authentication to claude code sdk#2

Merged
caffeinum merged 10 commits into
mainfrom
feat/auth-anthropic
Aug 15, 2025
Merged

feat: add anthropic oauth authentication to claude code sdk#2
caffeinum merged 10 commits into
mainfrom
feat/auth-anthropic

Conversation

@caffeinum
Copy link
Copy Markdown
Owner

@caffeinum caffeinum commented Aug 15, 2025

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:

  • programmatic authentication in ci/cd environments
  • authentication in environments where cli isn't available
  • direct token management for advanced use cases

implementation

uses @openauthjs/openauth library for secure oauth pkce flow implementation.

oauth endpoints

  • authorization: https://claude.ai/oauth/authorize with code=true parameter
  • token exchange: https://console.anthropic.com/v1/oauth/token
  • redirect uri: https://console.anthropic.com/oauth/code/callback

api

// namespace api for oauth operations
AuthAnthropic.authorize(mode: "max" | "console")
AuthAnthropic.exchange(code, verifier)
AuthAnthropic.refresh(refreshToken)

// validation helpers for credentials
validateCredentials(data) // validates oauth credentials
isValidCredentials(data) // type guard for credentials

usage example

import { AuthAnthropic, validateCredentials } from '@instantlyeasy/claude-code-sdk-ts';

// start oauth flow
const { url, verifier } = await AuthAnthropic.authorize("max");
console.log('authorize at:', url);

// exchange code for tokens
const tokens = await AuthAnthropic.exchange(authCode, verifier);

// refresh when needed
const refreshed = await AuthAnthropic.refresh(tokens.refresh);

files changed

  • src/auth/anthropic.ts - core oauth implementation (85 lines)
  • src/auth/validation.ts - zod validation schemas
  • examples/auth-example.ts - working authentication example
  • package.json - added @openauthjs/openauth and zod dependencies
  • README.md - authentication documentation

testing

✅ oauth url generation with proper pkce parameters
✅ correct endpoint configuration
✅ validation schemas working
✅ example successfully authenticates

🤖 generated with claude code

caffeinum and others added 6 commits August 11, 2025 21:42
- 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>
@caffeinum caffeinum changed the title feat/auth anthropic feat: Add programmatic authentication support via OAuth flow Aug 15, 2025
caffeinum and others added 2 commits August 14, 2025 18:55
- 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>
@caffeinum caffeinum changed the title feat: Add programmatic authentication support via OAuth flow feat: simplified oauth authentication using @openauthjs/openauth Aug 15, 2025
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>
@caffeinum caffeinum changed the title feat: simplified oauth authentication using @openauthjs/openauth feat: add anthropic claude max oauth flow Aug 15, 2025
@caffeinum caffeinum changed the title feat: add anthropic claude max oauth flow feat: add anthropic claude max oauth flow (based on opencode) Aug 15, 2025
- 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>
@caffeinum caffeinum changed the title feat: add anthropic claude max oauth flow (based on opencode) feat: add anthropic oauth authentication to claude code sdk Aug 15, 2025
@caffeinum caffeinum force-pushed the feat/auth-anthropic branch from d864cfc to 6b4daa7 Compare August 15, 2025 02:54
@caffeinum caffeinum merged commit 2692294 into main Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant