()
| 196 | } |
| 197 | |
| 198 | export function getAuthTokenSource() { |
| 199 | // --bare: API-key-only. apiKeyHelper (from --settings) is the only |
| 200 | // bearer-token-shaped source allowed. OAuth env vars, FD tokens, and |
| 201 | // keychain are ignored. |
| 202 | if (isBareMode()) { |
| 203 | if (getConfiguredApiKeyHelper()) { |
| 204 | return { source: 'apiKeyHelper' as const, hasToken: true } |
| 205 | } |
| 206 | return { source: 'none' as const, hasToken: false } |
| 207 | } |
| 208 | |
| 209 | if (process.env.ANTHROPIC_AUTH_TOKEN && !isManagedOAuthContext()) { |
| 210 | return { source: 'ANTHROPIC_AUTH_TOKEN' as const, hasToken: true } |
| 211 | } |
| 212 | |
| 213 | if (process.env.NCODE_OAUTH_TOKEN) { |
| 214 | return { source: 'NCODE_OAUTH_TOKEN' as const, hasToken: true } |
| 215 | } |
| 216 | if (process.env.CLAUDE_CODE_OAUTH_TOKEN) { |
| 217 | return { source: 'CLAUDE_CODE_OAUTH_TOKEN' as const, hasToken: true } |
| 218 | } |
| 219 | |
| 220 | const sessionAccessToken = getSessionIngressAuthToken() |
| 221 | if (sessionAccessToken) { |
| 222 | return { |
| 223 | source: process.env.CLAUDE_CODE_SESSION_ACCESS_TOKEN |
| 224 | ? ('CLAUDE_CODE_SESSION_ACCESS_TOKEN' as const) |
| 225 | : ('CLAUDE_SESSION_INGRESS_TOKEN_FILE' as const), |
| 226 | hasToken: true, |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Check for OAuth token from file descriptor (or its CCR disk fallback) |
| 231 | const oauthTokenFromFd = getOAuthTokenFromFileDescriptor() |
| 232 | if (oauthTokenFromFd) { |
| 233 | // getOAuthTokenFromFileDescriptor has a disk fallback for CCR subprocesses |
| 234 | // that can't inherit the pipe FD. Distinguish by env var presence so the |
| 235 | // org-mismatch message doesn't tell the user to unset a variable that |
| 236 | // doesn't exist. Call sites fall through correctly — the new source is |
| 237 | // !== 'none' (cli/handlers/auth.ts → oauth_token) and not in the |
| 238 | // isEnvVarToken set (auth.ts:1844 → generic re-login message). |
| 239 | const oauthTokenFdEnvVar = getOAuthTokenFileDescriptorEnvVarName() |
| 240 | if (oauthTokenFdEnvVar) { |
| 241 | return { |
| 242 | source: oauthTokenFdEnvVar as |
| 243 | | 'NCODE_OAUTH_TOKEN_FILE_DESCRIPTOR' |
| 244 | | 'CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR', |
| 245 | hasToken: true, |
| 246 | } |
| 247 | } |
| 248 | return { |
| 249 | source: 'CCR_OAUTH_TOKEN_FILE' as const, |
| 250 | hasToken: true, |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Check if apiKeyHelper is configured without executing it |
| 255 | // This prevents security issues where arbitrary code could execute before trust is established |
no test coverage detected