()
| 120 | /** Whether we are supporting direct 1P auth. */ |
| 121 | // this code is closely related to getAuthTokenSource |
| 122 | export function isAnthropicAuthEnabled(): boolean { |
| 123 | // --bare: API-key-only, never OAuth. |
| 124 | if (isBareMode()) return false |
| 125 | |
| 126 | // `claude ssh` remote: ANTHROPIC_UNIX_SOCKET tunnels API calls through a |
| 127 | // local auth-injecting proxy. The launcher sets CLAUDE_CODE_OAUTH_TOKEN as a |
| 128 | // placeholder iff the local side is a subscriber (so the remote includes the |
| 129 | // oauth-2025 beta header to match what the proxy will inject). The remote's |
| 130 | // ~/.claude settings (apiKeyHelper, settings.env.ANTHROPIC_API_KEY) MUST NOT |
| 131 | // flip this — they'd cause a header mismatch with the proxy and a bogus |
| 132 | // "invalid x-api-key" from the API. See src/ssh/sshAuthProxy.ts. |
| 133 | if (process.env.ANTHROPIC_UNIX_SOCKET) { |
| 134 | return !!process.env.CLAUDE_CODE_OAUTH_TOKEN |
| 135 | } |
| 136 | |
| 137 | const is3P = getAPIProvider() !== 'firstParty' |
| 138 | |
| 139 | // Check if user has configured an external API key source |
| 140 | // This allows externally-provided API keys to work (without requiring proxy configuration) |
| 141 | const settings = getSettings_DEPRECATED() || {} |
| 142 | const apiKeyHelper = settings.apiKeyHelper |
| 143 | const hasExternalAuthToken = |
| 144 | getConfiguredProviderAuthToken() || |
| 145 | process.env.ANTHROPIC_AUTH_TOKEN || |
| 146 | apiKeyHelper || |
| 147 | process.env.CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR |
| 148 | const configuredProviderApiKey = getConfiguredProviderApiKey() |
| 149 | |
| 150 | // Check if API key is from an external source (not managed by /login) |
| 151 | const { source: apiKeySource } = getAnthropicApiKeyWithSource({ |
| 152 | skipRetrievingKeyFromApiKeyHelper: true, |
| 153 | }) |
| 154 | const hasExternalApiKey = |
| 155 | apiKeySource === 'ANTHROPIC_API_KEY' || |
| 156 | apiKeySource === 'apiKeyHelper' || |
| 157 | !!configuredProviderApiKey |
| 158 | |
| 159 | // Disable Anthropic auth if: |
| 160 | // 1. Using 3rd party services (Bedrock/Vertex/Foundry) |
| 161 | // 2. User has an external API key (regardless of proxy configuration) |
| 162 | // 3. User has an external auth token (regardless of proxy configuration) |
| 163 | // this may cause issues if users have complex proxy / gateway "client-side creds" auth scenarios, |
| 164 | // e.g. if they want to set X-Api-Key to a gateway key but use Anthropic OAuth for the Authorization |
| 165 | // if we get reports of that, we should probably add an env var to force OAuth enablement |
| 166 | const shouldDisableAuth = |
| 167 | isGitHubOpenAICompatibleProviderTokenActive() || |
| 168 | is3P || |
| 169 | (hasExternalAuthToken && !isManagedOAuthContext()) || |
| 170 | (hasExternalApiKey && !isManagedOAuthContext()) |
| 171 | |
| 172 | return !shouldDisableAuth |
| 173 | } |
| 174 | |
| 175 | /** Where the auth token is being sourced from, if any. */ |
| 176 | // this code is closely related to isAnthropicAuthEnabled |
no test coverage detected