| 1 | import chalk from "chalk"; |
| 2 | |
| 3 | import { getApiClient } from "../config.js"; |
| 4 | import { safeStderr } from "../init.js"; |
| 5 | import { gracefulExit } from "../util/exit.js"; |
| 6 | |
| 7 | import type { |
| 8 | AuthConfig, |
| 9 | AuthenticatedConfig, |
| 10 | EnvironmentAuthConfig, |
| 11 | } from "./workos.js"; |
| 12 | import { saveAuthConfig } from "./workos.js"; |
| 13 | |
| 14 | /** |
| 15 | * Creates an updated AuthenticatedConfig with a new organization ID |
| 16 | */ |
| 17 | export function createUpdatedAuthConfig( |
| 18 | config: AuthenticatedConfig, |
| 19 | organizationId: string | null | undefined, |
| 20 | ): AuthenticatedConfig { |
| 21 | return { |
| 22 | userId: config.userId, |
| 23 | userEmail: config.userEmail, |
| 24 | accessToken: config.accessToken, |
| 25 | refreshToken: config.refreshToken, |
| 26 | expiresAt: config.expiresAt, |
| 27 | organizationId, |
| 28 | configUri: config.configUri, |
| 29 | modelName: config.modelName, |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Helper function to resolve organization by slug |
| 35 | */ |
| 36 | export async function resolveOrganizationBySlug( |
| 37 | apiClient: ReturnType<typeof getApiClient>, |
| 38 | organizationSlug: string, |
| 39 | ): Promise<{ id: string; slug: string }> { |
| 40 | const resp = await apiClient.listOrganizations(); |
| 41 | const organizations = resp.organizations; |
| 42 | |
| 43 | // Find organization by slug (case-insensitive) |
| 44 | const matchedOrg = organizations.find( |
| 45 | (org) => org?.slug?.toLowerCase() === organizationSlug.toLowerCase(), |
| 46 | ); |
| 47 | |
| 48 | if (matchedOrg) { |
| 49 | return { id: matchedOrg.id, slug: matchedOrg.slug }; |
| 50 | } else { |
| 51 | // Organization not found - show available options |
| 52 | const availableSlugs = organizations |
| 53 | .map((org) => org?.slug) |
| 54 | .filter(Boolean); |
| 55 | const availableOptions = ["personal", ...availableSlugs]; |
| 56 | console.info( |
| 57 | chalk.yellow("Available organizations:"), |
| 58 | availableOptions.join(", "), |
| 59 | ); |
| 60 | |
| 61 | throw new Error(`Organization "${organizationSlug}" not found`); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Handle CLI organization flag for environment auth configs |
| 67 | */ |
| 68 | export async function handleCliOrgForEnvironmentAuth( |
| 69 | authConfig: EnvironmentAuthConfig, |
| 70 | cliOrganizationSlug: string, |
| 71 | isHeadless: boolean, |
| 72 | ): Promise<AuthConfig> { |
| 73 | // Only allow --org flag in headless mode |
| 74 | if (!isHeadless) { |
| 75 | safeStderr( |
| 76 | chalk.red( |
| 77 | "The --org flag is only supported in headless mode (with -p/--print flag)\n", |
| 78 | ), |
| 79 | ); |
| 80 | await gracefulExit(1); |
| 81 | } |
| 82 | |
| 83 | const apiClient = getApiClient(authConfig.accessToken); |
| 84 | const resolvedOrg = await resolveOrganizationBySlug( |
| 85 | apiClient, |
| 86 | cliOrganizationSlug, |
| 87 | ); |
| 88 | |
| 89 | // Return modified environment auth config with organization ID |
| 90 | return { |
| 91 | ...authConfig, |
| 92 | organizationId: resolvedOrg.id, |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Handle CLI organization flag for authenticated configs |
| 98 | */ |
| 99 | export async function handleCliOrgForAuthenticatedConfig( |
| 100 | authenticatedConfig: AuthenticatedConfig, |
| 101 | cliOrganizationSlug: string, |
| 102 | isHeadless: boolean, |
| 103 | ): Promise<AuthConfig> { |
| 104 | // Only allow --org flag in headless mode |
| 105 | if (!isHeadless) { |
| 106 | safeStderr( |
| 107 | chalk.red( |
| 108 | "The --org flag is only supported in headless mode (with -p/--print flag)\n", |
| 109 | ), |
| 110 | ); |
| 111 | await gracefulExit(1); |
| 112 | } |
| 113 | |
| 114 | const apiClient = getApiClient(authenticatedConfig.accessToken); |
| 115 | const resolvedOrg = await resolveOrganizationBySlug( |
| 116 | apiClient, |
| 117 | cliOrganizationSlug, |
| 118 | ); |
| 119 | |
| 120 | const updatedConfig = createUpdatedAuthConfig( |
| 121 | authenticatedConfig, |
| 122 | resolvedOrg.id, |
| 123 | ); |
| 124 | saveAuthConfig(updatedConfig); |
| 125 | return updatedConfig; |
| 126 | } |