()
| 1989 | * token's org (network error, missing profile data), validation fails. |
| 1990 | */ |
| 1991 | export async function validateForceLoginOrg(): Promise<OrgValidationResult> { |
| 1992 | // `claude ssh` remote: real auth lives on the local machine and is injected |
| 1993 | // by the proxy. The placeholder token can't be validated against the profile |
| 1994 | // endpoint. The local side already ran this check before establishing the session. |
| 1995 | if (process.env.ANTHROPIC_UNIX_SOCKET) { |
| 1996 | return { valid: true } |
| 1997 | } |
| 1998 | |
| 1999 | if (!isAnthropicAuthEnabled()) { |
| 2000 | return { valid: true } |
| 2001 | } |
| 2002 | |
| 2003 | const requiredOrgUuid = |
| 2004 | getSettingsForSource('policySettings')?.forceLoginOrgUUID |
| 2005 | if (!requiredOrgUuid) { |
| 2006 | return { valid: true } |
| 2007 | } |
| 2008 | |
| 2009 | // Ensure the access token is fresh before hitting the profile endpoint. |
| 2010 | // No-op for env-var tokens (refreshToken is null). |
| 2011 | await checkAndRefreshOAuthTokenIfNeeded() |
| 2012 | |
| 2013 | const tokens = getClaudeAIOAuthTokens() |
| 2014 | if (!tokens) { |
| 2015 | return { valid: true } |
| 2016 | } |
| 2017 | |
| 2018 | // Always fetch the authoritative org UUID from the profile endpoint. |
| 2019 | // Even keychain-sourced tokens verify server-side: the cached org UUID |
| 2020 | // in ~/.claude.json is user-writable and cannot be trusted. |
| 2021 | const { source } = getAuthTokenSource() |
| 2022 | const isEnvVarToken = |
| 2023 | source === 'CLAUDE_CODE_OAUTH_TOKEN' || |
| 2024 | source === 'CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR' |
| 2025 | |
| 2026 | const profile = await getOauthProfileFromOauthToken(tokens.accessToken) |
| 2027 | if (!profile) { |
| 2028 | // Fail closed — we can't verify the org |
| 2029 | return { |
| 2030 | valid: false, |
| 2031 | message: |
| 2032 | `Unable to verify organization for the current authentication token.\n` + |
| 2033 | `This machine requires organization ${requiredOrgUuid} but the profile could not be fetched.\n` + |
| 2034 | `This may be a network error, or the token may lack the user:profile scope required for\n` + |
| 2035 | `verification (tokens from 'claude setup-token' do not include this scope).\n` + |
| 2036 | `Try again, or obtain a full-scope token via 'claude auth login'.`, |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | const tokenOrgUuid = profile.organization.uuid |
| 2041 | if (tokenOrgUuid === requiredOrgUuid) { |
| 2042 | return { valid: true } |
| 2043 | } |
| 2044 | |
| 2045 | if (isEnvVarToken) { |
| 2046 | const envVarName = |
| 2047 | source === 'CLAUDE_CODE_OAUTH_TOKEN' |
| 2048 | ? 'CLAUDE_CODE_OAUTH_TOKEN' |
no test coverage detected