Changesets · Branches · Tags · Compare
| Changeset | 991ff92b576be6ec7aadeaac6689714ca3b2dfb0bdfbe080ee2453c38881f111 |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-14 12:11 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-14 12:11 -07:00 |
| Parents | 0c09f056c5c7, 02e5305869a6 |
2 files changed
added extensions/cli/src/auth/workos.orgScope.test.ts +58 −0
| @@ -0,0 +1,58 @@ | ||
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; | |
| 2 | + | |
| 3 | +import { ensureOrganization, isEnvironmentAuthConfig } from "./workos.js"; | |
| 4 | + | |
| 5 | +describe("ensureOrganization with API key org scope", () => { | |
| 6 | + const originalEnv = { ...process.env }; | |
| 7 | + const originalFetch = global.fetch; | |
| 8 | + | |
| 9 | + beforeEach(() => { | |
| 10 | + vi.restoreAllMocks(); | |
| 11 | + }); | |
| 12 | + | |
| 13 | + afterEach(() => { | |
| 14 | + process.env = { ...originalEnv }; | |
| 15 | + global.fetch = originalFetch as any; | |
| 16 | + }); | |
| 17 | + | |
| 18 | + it("resolves organizationId via auth/scope for API key auth", async () => { | |
| 19 | + const mockFetch = vi.fn().mockResolvedValue({ | |
| 20 | + ok: true, | |
| 21 | + json: async () => ({ organizationId: "org_123" }), | |
| 22 | + }); | |
| 23 | + (global as any).fetch = mockFetch; | |
| 24 | + | |
| 25 | + const envAuthConfig = { | |
| 26 | + accessToken: "sk_test_api_key", | |
| 27 | + organizationId: null as string | null, | |
| 28 | + } as const; | |
| 29 | + | |
| 30 | + const result = await ensureOrganization(envAuthConfig, true); | |
| 31 | + expect(result).not.toBeNull(); | |
| 32 | + if (result && isEnvironmentAuthConfig(result)) { | |
| 33 | + expect(result.organizationId).toBe("org_123"); | |
| 34 | + } else { | |
| 35 | + throw new Error("Expected environment auth config result"); | |
| 36 | + } | |
| 37 | + expect(mockFetch).toHaveBeenCalledOnce(); | |
| 38 | + }); | |
| 39 | + | |
| 40 | + it("keeps organizationId null if endpoint not available", async () => { | |
| 41 | + const mockFetch = vi.fn().mockResolvedValue({ ok: false }); | |
| 42 | + (global as any).fetch = mockFetch; | |
| 43 | + | |
| 44 | + const envAuthConfig = { | |
| 45 | + accessToken: "sk_test_api_key", | |
| 46 | + organizationId: null as string | null, | |
| 47 | + } as const; | |
| 48 | + | |
| 49 | + const result = await ensureOrganization(envAuthConfig, true); | |
| 50 | + expect(result).not.toBeNull(); | |
| 51 | + if (result && isEnvironmentAuthConfig(result)) { | |
| 52 | + expect(result.organizationId).toBeNull(); | |
| 53 | + } else { | |
| 54 | + throw new Error("Expected environment auth config result"); | |
| 55 | + } | |
| 56 | + expect(mockFetch).toHaveBeenCalledOnce(); | |
| 57 | + }); | |
| 58 | +}); |
modified extensions/cli/src/auth/workos.ts +57 −2
| @@ -540,8 +540,10 @@ | ||
| 540 | 540 | ); |
| 541 | 541 | } |
| 542 | 542 | } else if (isEnvironmentAuthConfig(authConfig)) { |
| 543 | - // No CLI organization slug (or "personal" was specified) - return as-is | |
| 544 | - return authConfig; | |
| 543 | + // No CLI organization slug (or "personal" was specified) | |
| 544 | + // If using API key auth, attempt to resolve its organization scope once | |
| 545 | + const resolved = await resolveOrgScopeForApiKey(authConfig); | |
| 546 | + return resolved; | |
| 545 | 547 | } |
| 546 | 548 | |
| 547 | 549 | // If not authenticated, return as-is |
| @@ -577,6 +579,59 @@ | ||
| 577 | 579 | return autoSelectOrganization(authenticatedConfig); |
| 578 | 580 | } |
| 579 | 581 | |
| 582 | +/** | |
| 583 | + * Attempts to resolve the organization scope for API key auth (environment-based auth). | |
| 584 | + * Calls a lightweight backend endpoint to determine if the API key is an org-scoped key. | |
| 585 | + * If successful, returns a new EnvironmentAuthConfig with organizationId set; otherwise returns the original config. | |
| 586 | + */ | |
| 587 | +async function resolveOrgScopeForApiKey( | |
| 588 | + envConfig: EnvironmentAuthConfig, | |
| 589 | +): Promise<EnvironmentAuthConfig> { | |
| 590 | + // If already set (including explicit null), return as-is | |
| 591 | + if (envConfig.organizationId !== null) { | |
| 592 | + return envConfig; | |
| 593 | + } | |
| 594 | + | |
| 595 | + const accessToken = envConfig.accessToken; | |
| 596 | + if (!accessToken) return envConfig; | |
| 597 | + | |
| 598 | + try { | |
| 599 | + // Prefer a dedicated endpoint for scope discovery. This should return JSON like: | |
| 600 | + // { organizationId: string | null } (aka orgScopeId) | |
| 601 | + const url = new URL("auth/scope", env.apiBase); | |
| 602 | + const resp = await fetch(url, { | |
| 603 | + method: "GET", | |
| 604 | + headers: { | |
| 605 | + Authorization: `Bearer ${accessToken}`, | |
| 606 | + "Content-Type": "application/json", | |
| 607 | + }, | |
| 608 | + }); | |
| 609 | + | |
| 610 | + if (!resp.ok) { | |
| 611 | + // Gracefully ignore if the endpoint isn't available yet (e.g., 404) or other non-2xx errors | |
| 612 | + return envConfig; | |
| 613 | + } | |
| 614 | + | |
| 615 | + const data: any = await resp.json().catch(() => ({})); | |
| 616 | + const orgId = | |
| 617 | + data?.organizationId ?? | |
| 618 | + data?.orgScopeId ?? | |
| 619 | + data?.organization?.id ?? | |
| 620 | + null; | |
| 621 | + | |
| 622 | + if (orgId && typeof orgId === "string") { | |
| 623 | + return { | |
| 624 | + ...envConfig, | |
| 625 | + organizationId: orgId, | |
| 626 | + }; | |
| 627 | + } | |
| 628 | + } catch { | |
| 629 | + // Network or parsing issues: leave config unchanged | |
| 630 | + } | |
| 631 | + | |
| 632 | + return envConfig; | |
| 633 | +} | |
| 634 | + | |
| 580 | 635 | /** |
| 581 | 636 | * Gets the list of available organizations for the user |
| 582 | 637 | */ |