Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/cli/src/lib/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { retrieveApiIndex } from "@sentry/api";
import { array, nullable, object, string } from "valibot";

import { getControlSiloUrl } from "../sentry-client.js";
import { apiRequestToRegion } from "./infrastructure.js";
import { getSdkClientConfig } from "../sentry-sdk-client.js";
import { unwrapValidatedResult } from "./infrastructure.js";

const AuthStatusSchema = object({
auth: nullable(
Expand All @@ -14,8 +16,14 @@ const AuthStatusSchema = object({
export async function getCurrentAuthScopes(): Promise<
readonly string[] | null
> {
const { data } = await apiRequestToRegion(getControlSiloUrl(), "", {
schema: AuthStatusSchema,
const result = await retrieveApiIndex({
...getSdkClientConfig(getControlSiloUrl()),
});
const data = unwrapValidatedResult(
result,
"Failed to read token scopes",
AuthStatusSchema,
"/",
);
return data.auth?.scopes ?? null;
}
60 changes: 35 additions & 25 deletions packages/cli/test/lib/api/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,63 @@
import { beforeEach, describe, expect, test, vi } from "vitest";

vi.mock("../../../src/lib/api/infrastructure.js", () => ({
apiRequestToRegion: vi.fn(),
const { retrieveApiIndexMock } = vi.hoisted(() => ({
retrieveApiIndexMock: vi.fn(),
}));

vi.mock("@sentry/api", async (importOriginal) => ({
...(await importOriginal<typeof import("@sentry/api")>()),
retrieveApiIndex: retrieveApiIndexMock,
}));

vi.mock("../../../src/lib/sentry-client.js", async (importOriginal) => ({
...(await importOriginal<
typeof import("../../../src/lib/sentry-client.js")
>()),
getControlSiloUrl: () => "https://sentry.io",
}));

import { getCurrentAuthScopes } from "../../../src/lib/api/auth.js";
import { apiRequestToRegion } from "../../../src/lib/api/infrastructure.js";
import { ApiError } from "../../../src/lib/errors.js";

describe("getCurrentAuthScopes", () => {
beforeEach(() => vi.mocked(apiRequestToRegion).mockReset());
beforeEach(() => retrieveApiIndexMock.mockReset());

test("reads effective token scopes from the API index", async () => {
vi.mocked(apiRequestToRegion).mockResolvedValue({
data: { auth: { scopes: ["org:read", "team:admin"] } },
headers: new Headers(),
retrieveApiIndexMock.mockResolvedValue({
data: {
version: "0",
auth: { scopes: ["org:read", "team:admin"] },
user: null,
},
response: new Response(),
});

await expect(getCurrentAuthScopes()).resolves.toEqual([
"org:read",
"team:admin",
]);
expect(apiRequestToRegion).toHaveBeenCalledWith(
expect.any(String),
"",
expect.objectContaining({ schema: expect.any(Object) }),
expect(retrieveApiIndexMock).toHaveBeenCalledWith(
expect.objectContaining({ baseUrl: "https://sentry.io" }),
);
});

test("returns null when the API index reports no authenticated token", async () => {
vi.mocked(apiRequestToRegion).mockResolvedValue({
data: { auth: null },
headers: new Headers(),
retrieveApiIndexMock.mockResolvedValue({
data: { version: "0", auth: null, user: null },
response: new Response(),
});

await expect(getCurrentAuthScopes()).resolves.toBeNull();
});

test("preserves a 401 from an invalid bearer", async () => {
const error = new Error("401 Unauthorized");
vi.mocked(apiRequestToRegion).mockImplementationOnce(async () => {
throw error;
test("reports a 401 from an invalid bearer as an ApiError", async () => {
retrieveApiIndexMock.mockResolvedValue({
error: { detail: "Invalid token" },
response: new Response(null, { status: 401 }),
});

let thrown: unknown;
try {
await getCurrentAuthScopes();
} catch (caught) {
thrown = caught;
}
expect(thrown).toBe(error);
const thrown = await getCurrentAuthScopes().catch((error) => error);
expect(thrown).toBeInstanceOf(ApiError);
expect(thrown).toMatchObject({ status: 401 });
});
});
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ catalog:
"@modelcontextprotocol/server": 2.4.0
"@radix-ui/react-accordion": ^1.2.20
"@radix-ui/react-slot": ^1.2.3
"@sentry/api": 1.53.0
"@sentry/api": 1.55.0
"@sentry/cloudflare": 11.6.0
"@sentry/core": 11.6.0
"@sentry/node": 11.6.0
Expand Down Expand Up @@ -168,7 +168,7 @@ minimumReleaseAgeExclude:
- "@oxlint/binding-win32-x64-msvc@1.87.0"
- oxfmt@0.72.0
- oxlint@1.87.0
- "@sentry/api@1.53.0"
- "@sentry/api@1.55.0"
- miniflare@5.20261101.0-beta
- wrangler@4.156.1
- "@sentry/symbolic@14.0.0-alpha.5"
Expand Down
Loading