()
| 2201 | * token's org (network error, missing profile data), validation fails. |
| 2202 | */ |
| 2203 | export async function validateForceLoginOrg(): Promise<OrgValidationResult> { |
| 2204 | // `ncode ssh` remote: real auth lives on the local machine and is injected |
| 2205 | // by the proxy. The placeholder token can't be validated against the profile |
| 2206 | // endpoint. The local side already ran this check before establishing the session. |
| 2207 | if (process.env.ANTHROPIC_UNIX_SOCKET) { |
| 2208 | return { valid: true } |
| 2209 | } |
| 2210 | |
| 2211 | if (!isAnthropicAuthEnabled()) { |
| 2212 | return { valid: true } |
| 2213 | } |
| 2214 | |
| 2215 | const requiredOrgUuid = |
| 2216 | getSettingsForSource('policySettings')?.forceLoginOrgUUID |
| 2217 | if (!requiredOrgUuid) { |
| 2218 | return { valid: true } |
| 2219 | } |
| 2220 | |
| 2221 | // Ensure the access token is fresh before hitting the profile endpoint. |
| 2222 | // No-op for env-var tokens (refreshToken is null). |
| 2223 | await checkAndRefreshOAuthTokenIfNeeded() |
| 2224 | |
| 2225 | const tokens = getClaudeAIOAuthTokens() |
| 2226 | if (!tokens) { |
| 2227 | return { valid: true } |
| 2228 | } |
| 2229 | |
| 2230 | // Always fetch the authoritative org UUID from the profile endpoint. |
| 2231 | // Even keychain-sourced tokens verify server-side: the cached org UUID |
| 2232 | // in ~/.ncode.json is user-writable and cannot be trusted. |
| 2233 | const { source } = getAuthTokenSource() |
| 2234 | const isEnvVarToken = |
| 2235 | source === 'NCODE_OAUTH_TOKEN' || |
| 2236 | source === 'CLAUDE_CODE_OAUTH_TOKEN' || |
| 2237 | source === 'CLAUDE_CODE_SESSION_ACCESS_TOKEN' || |
| 2238 | source === 'NCODE_OAUTH_TOKEN_FILE_DESCRIPTOR' || |
| 2239 | source === 'CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR' |
| 2240 | |
| 2241 | if (source === 'CLAUDE_SESSION_INGRESS_TOKEN_FILE') { |
| 2242 | return { valid: true } |
| 2243 | } |
| 2244 | |
| 2245 | const profile = await getOauthProfileFromOauthToken(tokens.accessToken) |
| 2246 | if (!profile) { |
| 2247 | // Fail closed — we can't verify the org |
| 2248 | return { |
| 2249 | valid: false, |
| 2250 | message: |
| 2251 | `Unable to verify organization for the current authentication token.\n` + |
| 2252 | `This machine requires organization ${requiredOrgUuid} but the profile could not be fetched.\n` + |
| 2253 | `This may be a network error, or the token may lack the user:profile scope required for\n` + |
| 2254 | `verification (tokens from 'code setup-token' do not include this scope).\n` + |
| 2255 | `Try again, or obtain a full-scope token via 'code auth login'.`, |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | const tokenOrgUuid = profile.organization.uuid |
| 2260 | if (tokenOrgUuid === requiredOrgUuid) { |
nothing calls this directly
no test coverage detected