()
| 589 | * @returns The organization UUID or null if not authenticated |
| 590 | */ |
| 591 | export async function getOrganizationUUID(): Promise<string | null> { |
| 592 | const globalConfig = getGlobalConfig() |
| 593 | const cachedOrgUUID = globalConfig.oauthAccount?.organizationUuid |
| 594 | const session = getCurrentOAuthClientSession() |
| 595 | const accessToken = |
| 596 | session?.headersKind === 'bearer' ? session.accessToken : null |
| 597 | const hasInjectedOAuthToken = Boolean( |
| 598 | process.env.CLAUDE_CODE_OAUTH_TOKEN || |
| 599 | getOAuthTokenFileDescriptorEnvVarName(), |
| 600 | ) |
| 601 | |
| 602 | // Env-injected OAuth tokens can override or outlive cached local account |
| 603 | // state. Revalidate them against the profile endpoint when possible so |
| 604 | // remote-session flows don't blindly reuse a stale organization UUID. |
| 605 | if (accessToken && (hasInjectedOAuthToken || !cachedOrgUUID)) { |
| 606 | const profileOrgUUID = await getOrganizationUUIDFromProfile(accessToken) |
| 607 | if (profileOrgUUID) { |
| 608 | return profileOrgUUID |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (cachedOrgUUID) { |
| 613 | return cachedOrgUUID |
| 614 | } |
| 615 | |
| 616 | // Fall back to fetching from profile (requires user:profile scope) |
| 617 | if (!accessToken || !hasOauthProfileAccess(session)) { |
| 618 | return null |
| 619 | } |
| 620 | return getOrganizationUUIDFromProfile(accessToken) |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Populate the OAuth account info if it has not already been cached in config. |
no test coverage detected