fix(anthropic): forward OAuth bearer tokens via Authorization header#1599
Open
YupeiChen94WiseRock wants to merge 1 commit intoPortkey-AI:mainfrom
Open
Conversation
Anthropic supports two mutually exclusive auth schemes: - API keys (sk-ant-*): X-API-Key header - OAuth tokens (Claude Max / enterprise SSO): Authorization: Bearer header Previously all credentials were forwarded as X-API-Key, causing OAuth token auth to always fail. Detect the token type by prefix and choose the correct header accordingly.
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
Fixes #1598
Anthropic uses two mutually exclusive authentication schemes:
sk-ant-*): must be sent asX-API-Key: <key>Authorization: Bearer <token>Previously,
src/providers/anthropic/api.tsalways forwarded credentials asX-API-Key, causing OAuth token authentication to always fail.Root cause
When a client sends
Authorization: Bearer <oauth_token>,handlerUtils.tsstrips theBearerprefix and stores the raw token asapiKey. The Anthropic provider then places it inX-API-Key, which Anthropic rejects for OAuth tokens.Fix
Detect the credential type by prefix in
src/providers/anthropic/api.tsand choose the correct header:Test plan
sk-ant-*API key continue to useX-API-Key(no regression)Authorization: Bearerare sent to Anthropic withAuthorization: Bearer <token>ANTHROPIC_BASE_URL=https://api.portkey.aiand noANTHROPIC_API_KEY(OAuth/Max plan) authenticates successfully without mid-session failures