GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — claude-code-source-code/src/cli/handlers/auth.ts at main · unsignedcpp/claude-code-source-code Expand file tree/ Copy pathauth.tsMore file actions 330 lines (300 loc) · 10.5 KB / Copy pathauth.tsFile metadata and controls330 lines (300 loc) · 10.5 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 /* eslint-disable custom-rules/no-process-exit -- CLI subcommand handler intentionally exits */ import { clearAuthRelatedCaches, performLogout, } from '../../commands/logout/logout.js' import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent, } from '../../services/analytics/index.js' import { getSSLErrorHint } from '../../services/api/errorUtils.js' import { fetchAndStoreClaudeCodeFirstTokenDate } from '../../services/api/firstTokenDate.js' import { createAndStoreApiKey, fetchAndStoreUserRoles, refreshOAuthToken, shouldUseClaudeAIAuth, storeOAuthAccountInfo, } from '../../services/oauth/client.js' import { getOauthProfileFromOauthToken } from '../../services/oauth/getOauthProfile.js' import { OAuthService } from '../../services/oauth/index.js' import type { OAuthTokens } from '../../services/oauth/types.js' import { clearOAuthTokenCache, getAnthropicApiKeyWithSource, getAuthTokenSource, getOauthAccountInfo, getSubscriptionType, isUsing3PServices, saveOAuthTokensIfNeeded, validateForceLoginOrg, } from '../../utils/auth.js' import { saveGlobalConfig } from '../../utils/config.js' import { logForDebugging } from '../../utils/debug.js' import { isRunningOnHomespace } from '../../utils/envUtils.js' import { errorMessage } from '../../utils/errors.js' import { logError } from '../../utils/log.js' import { getAPIProvider } from '../../utils/model/providers.js' import { getInitialSettings } from '../../utils/settings/settings.js' import { jsonStringify } from '../../utils/slowOperations.js' import { buildAccountProperties, buildAPIProviderProperties, } from '../../utils/status.js' /** * Shared post-token-acquisition logic. Saves tokens, fetches profile/roles, * and sets up the local auth state. */ export async function installOAuthTokens(tokens: OAuthTokens): Promise<void> { // Clear old state before saving new credentials await performLogout({ clearOnboarding: false }) // Reuse pre-fetched profile if available, otherwise fetch fresh const profile = tokens.profile ?? (await getOauthProfileFromOauthToken(tokens.accessToken)) if (profile) { storeOAuthAccountInfo({ accountUuid: profile.account.uuid, emailAddress: profile.account.email, organizationUuid: profile.organization.uuid, displayName: profile.account.display_name || undefined, hasExtraUsageEnabled: profile.organization.has_extra_usage_enabled ?? undefined, billingType: profile.organization.billing_type ?? undefined, subscriptionCreatedAt: profile.organization.subscription_created_at ?? undefined, accountCreatedAt: profile.account.created_at, }) } else if (tokens.tokenAccount) { // Fallback to token exchange account data when profile endpoint fails storeOAuthAccountInfo({ accountUuid: tokens.tokenAccount.uuid, emailAddress: tokens.tokenAccount.emailAddress, organizationUuid: tokens.tokenAccount.organizationUuid, }) } const storageResult = saveOAuthTokensIfNeeded(tokens) clearOAuthTokenCache() if (storageResult.warning) { logEvent('tengu_oauth_storage_warning', { warning: storageResult.warning as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, }) } // Roles and first-token-date may fail for limited-scope tokens (e.g. // inference-only from setup-token). They're not required for core auth. await fetchAndStoreUserRoles(tokens.accessToken).catch(err => logForDebugging(String(err), { level: 'error' }), ) if (shouldUseClaudeAIAuth(tokens.scopes)) { await fetchAndStoreClaudeCodeFirstTokenDate().catch(err => logForDebugging(String(err), { level: 'error' }), ) } else { // API key creation is critical for Console users — let it throw. const apiKey = await createAndStoreApiKey(tokens.accessToken) if (!apiKey) { throw new Error( 'Unable to create API key. The server accepted the request but did not return a key.', ) } } await clearAuthRelatedCaches() } export async function authLogin({ email, sso, console: useConsole, claudeai, }: { email?: string sso?: boolean console?: boolean claudeai?: boolean }): Promise<void> { if (useConsole && claudeai) { process.stderr.write( 'Error: --console and --claudeai cannot be used together.\n', ) process.exit(1) } const settings = getInitialSettings() // forceLoginMethod is a hard constraint (enterprise setting) — matches ConsoleOAuthFlow behavior. // Without it, --console selects Console; --claudeai (or no flag) selects claude.ai. const loginWithClaudeAi = settings.forceLoginMethod ? settings.forceLoginMethod === 'claudeai' : !useConsole const orgUUID = settings.forceLoginOrgUUID // Fast path: if a refresh token is provided via env var, skip the browser // OAuth flow and exchange it directly for tokens. const envRefreshToken = process.env.CLAUDE_CODE_OAUTH_REFRESH_TOKEN if (envRefreshToken) { const envScopes = process.env.CLAUDE_CODE_OAUTH_SCOPES if (!envScopes) { process.stderr.write( 'CLAUDE_CODE_OAUTH_SCOPES is required when using CLAUDE_CODE_OAUTH_REFRESH_TOKEN.\n' + 'Set it to the space-separated scopes the refresh token was issued with\n' + '(e.g. "user:inference" or "user:profile user:inference user:sessions:claude_code user:mcp_servers").\n', ) process.exit(1) } const scopes = envScopes.split(/\s+/).filter(Boolean) try { logEvent('tengu_login_from_refresh_token', {}) const tokens = await refreshOAuthToken(envRefreshToken, { scopes }) await installOAuthTokens(tokens) const orgResult = await validateForceLoginOrg() if (!orgResult.valid) { process.stderr.write(orgResult.message + '\n') process.exit(1) } // Mark onboarding complete — interactive paths handle this via // the Onboarding component, but the env var path skips it. saveGlobalConfig(current => { if (current.hasCompletedOnboarding) return current return { ...current, hasCompletedOnboarding: true } }) logEvent('tengu_oauth_success', { loginWithClaudeAi: shouldUseClaudeAIAuth(tokens.scopes), }) process.stdout.write('Login successful.\n') process.exit(0) } catch (err) { logError(err) const sslHint = getSSLErrorHint(err) process.stderr.write( `Login failed: ${errorMessage(err)}\n${sslHint ? sslHint + '\n' : ''}`, ) process.exit(1) } } const resolvedLoginMethod = sso ? 'sso' : undefined const oauthService = new OAuthService() try { logEvent('tengu_oauth_flow_start', { loginWithClaudeAi }) const result = await oauthService.startOAuthFlow( async url => { process.stdout.write('Opening browser to sign in…\n') process.stdout.write(`If the browser didn't open, visit: ${url}\n`) }, { loginWithClaudeAi, loginHint: email, loginMethod: resolvedLoginMethod, orgUUID, }, ) await installOAuthTokens(result) const orgResult = await validateForceLoginOrg() if (!orgResult.valid) { process.stderr.write(orgResult.message + '\n') process.exit(1) } logEvent('tengu_oauth_success', { loginWithClaudeAi }) process.stdout.write('Login successful.\n') process.exit(0) } catch (err) { logError(err) const sslHint = getSSLErrorHint(err) process.stderr.write( `Login failed: ${errorMessage(err)}\n${sslHint ? sslHint + '\n' : ''}`, ) process.exit(1) } finally { oauthService.cleanup() } } export async function authStatus(opts: { json?: boolean text?: boolean }): Promise<void> { const { source: authTokenSource, hasToken } = getAuthTokenSource() const { source: apiKeySource } = getAnthropicApiKeyWithSource() const hasApiKeyEnvVar = !!process.env.ANTHROPIC_API_KEY && !isRunningOnHomespace() const oauthAccount = getOauthAccountInfo() const subscriptionType = getSubscriptionType() const using3P = isUsing3PServices() const loggedIn = hasToken || apiKeySource !== 'none' || hasApiKeyEnvVar || using3P // Determine auth method let authMethod: string = 'none' if (using3P) { authMethod = 'third_party' } else if (authTokenSource === 'claude.ai') { authMethod = 'claude.ai' } else if (authTokenSource === 'apiKeyHelper') { authMethod = 'api_key_helper' } else if (authTokenSource !== 'none') { authMethod = 'oauth_token' } else if (apiKeySource === 'ANTHROPIC_API_KEY' || hasApiKeyEnvVar) { authMethod = 'api_key' } else if (apiKeySource === '/login managed key') { authMethod = 'claude.ai' } if (opts.text) { const properties = [ ...buildAccountProperties(), ...buildAPIProviderProperties(), ] let hasAuthProperty = false for (const prop of properties) { const value = typeof prop.value === 'string' ? prop.value : Array.isArray(prop.value) ? prop.value.join(', ') : null if (value === null || value === 'none') { continue } hasAuthProperty = true if (prop.label) { process.stdout.write(`${prop.label}: ${value}\n`) } else { process.stdout.write(`${value}\n`) } } if (!hasAuthProperty && hasApiKeyEnvVar) { process.stdout.write('API key: ANTHROPIC_API_KEY\n') } if (!loggedIn) { process.stdout.write( 'Not logged in. Run claude auth login to authenticate.\n', ) } } else { const apiProvider = getAPIProvider() const resolvedApiKeySource = apiKeySource !== 'none' ? apiKeySource : hasApiKeyEnvVar ? 'ANTHROPIC_API_KEY' : null const output: Record<string, string | boolean | null> = { loggedIn, authMethod, apiProvider, } if (resolvedApiKeySource) { output.apiKeySource = resolvedApiKeySource } if (authMethod === 'claude.ai') { output.email = oauthAccount?.emailAddress ?? null output.orgId = oauthAccount?.organizationUuid ?? null output.orgName = oauthAccount?.organizationName ?? null output.subscriptionType = subscriptionType ?? null } process.stdout.write(jsonStringify(output, null, 2) + '\n') } process.exit(loggedIn ? 0 : 1) } export async function authLogout(): Promise<void> { try { await performLogout({ clearOnboarding: false }) } catch { process.stderr.write('Failed to log out.\n') process.exit(1) } process.stdout.write('Successfully logged out from your Anthropic account.\n') process.exit(0) } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |