-
-
Notifications
You must be signed in to change notification settings - Fork 70
feat(provider): add Anthropic OAuth provider (Claude Pro/Max) #132
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
Open
theblazehen
wants to merge
5
commits into
Mirrowel:dev
Choose a base branch
from
theblazehen:anthropic-provider
base: dev
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9c663d4
feat(providers): add Anthropic OAuth provider (Claude Pro/Max)
theblazehen 64ffb07
fix(anthropic): URL-encode OAuth authorize parameters
theblazehen 7aa20d4
fix(anthropic): address review feedback
theblazehen a35b586
fix(anthropic): cache thinking signatures server-side for multi-turn
theblazehen 93f16cc
feat(anthropic): inject prompt cache breakpoints for cost savings
theblazehen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Two-step Anthropic OAuth credential setup. | ||
|
|
||
| Step 1 (no args): Generate auth URL + save verifier | ||
| python scripts/setup_anthropic_cred.py | ||
|
|
||
| Step 2 (with code): Exchange code for tokens | ||
| python scripts/setup_anthropic_cred.py "CODE_FROM_BROWSER" | ||
| """ | ||
| import sys | ||
| import os | ||
| import json | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) | ||
|
|
||
| import asyncio | ||
| from pathlib import Path | ||
| from rotator_library.providers.anthropic_auth_base import ( | ||
| _generate_pkce, _build_authorize_url, AnthropicAuthBase | ||
| ) | ||
|
|
||
| STATE_FILE = Path(__file__).parent / ".anthropic_pkce_state.json" | ||
| OAUTH_DIR = Path(__file__).parent / ".." / "oauth_creds" | ||
|
|
||
| async def exchange_code(auth_code: str): | ||
| if not STATE_FILE.exists(): | ||
| print("Error: PKCE state file not found. Please run Step 1 first.") | ||
| sys.exit(1) | ||
| state = json.loads(STATE_FILE.read_text()) | ||
| verifier = state["verifier"] | ||
|
|
||
| auth = AnthropicAuthBase() | ||
| tokens = await auth._exchange_code(auth_code.strip(), verifier) | ||
|
|
||
| import time | ||
| creds = { | ||
| **tokens, | ||
| "email": "anthropic-oauth-user", | ||
| "_proxy_metadata": { | ||
| "email": "anthropic-oauth-user", | ||
| "last_check_timestamp": time.time(), | ||
| "credential_type": "oauth", | ||
| }, | ||
| } | ||
|
|
||
| oauth_dir = OAUTH_DIR.resolve() | ||
| oauth_dir.mkdir(parents=True, exist_ok=True) | ||
| existing = sorted(oauth_dir.glob("anthropic_oauth_*.json")) | ||
| next_num = len(existing) + 1 | ||
| file_path = oauth_dir / f"anthropic_oauth_{next_num}.json" | ||
|
|
||
| file_path.write_text(json.dumps(creds, indent=2)) | ||
| os.chmod(file_path, 0o600) | ||
| STATE_FILE.unlink(missing_ok=True) | ||
|
|
||
| print(f"Credential saved to: {file_path}") | ||
| print(f"Access token prefix: {tokens['access_token'][:20]}...") | ||
|
|
||
| def step1(): | ||
| verifier, challenge = _generate_pkce() | ||
| url = _build_authorize_url(verifier, challenge) | ||
| STATE_FILE.write_text(json.dumps({"verifier": verifier, "challenge": challenge})) | ||
| print("Open this URL in your browser, authorize, then copy the code:\n") | ||
| print(url) | ||
| print(f"\nThen run: python scripts/setup_anthropic_cred.py \"PASTE_CODE_HERE\"") | ||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) > 1: | ||
| asyncio.run(exchange_code(sys.argv[1])) | ||
| else: | ||
| step1() | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will raise a
FileNotFoundErrorif Step 2 is run before Step 1. Consider adding a check:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed in 7aa20d4. Added the existence check before reading.