|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { buildJwtAbility } from "@trigger.dev/rbac"; |
| 3 | + |
| 4 | +const jwtMocks = vi.hoisted(() => ({ |
| 5 | + validatePublicJwtKey: vi.fn<(...args: any[]) => Promise<any>>(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("@internal/tracing", () => ({ |
| 9 | + getMeter: () => ({ |
| 10 | + createCounter: () => ({ add: vi.fn() }), |
| 11 | + createHistogram: () => ({ record: vi.fn() }), |
| 12 | + createObservableGauge: () => ({ addCallback: vi.fn() }), |
| 13 | + }), |
| 14 | +})); |
| 15 | +vi.mock("~/services/rbac.server", () => ({ rbac: { authenticateBearer: vi.fn() } })); |
| 16 | +vi.mock("~/db.server", () => ({ prisma: {}, $replica: {} })); |
| 17 | +vi.mock("~/env.server", () => ({ env: { SESSION_SECRET: "test-session-secret" } })); |
| 18 | +vi.mock("~/models/project.server", () => ({ findProjectByRef: vi.fn() })); |
| 19 | +vi.mock("~/models/runtimeEnvironment.server", () => ({ |
| 20 | + authIncludeBase: {}, |
| 21 | + authIncludeWithParent: {}, |
| 22 | + findEnvironmentByApiKey: vi.fn(), |
| 23 | + findEnvironmentByApiKeyWithResolution: vi.fn(), |
| 24 | + findEnvironmentByPublicApiKey: vi.fn(), |
| 25 | + toAuthenticated: vi.fn(), |
| 26 | +})); |
| 27 | +vi.mock("~/services/personalAccessToken.server", () => ({ |
| 28 | + authenticateApiRequestWithPersonalAccessToken: vi.fn(), |
| 29 | + isPersonalAccessToken: () => false, |
| 30 | +})); |
| 31 | +vi.mock("~/services/organizationAccessToken.server", () => ({ |
| 32 | + authenticateApiRequestWithOrganizationAccessToken: vi.fn(), |
| 33 | + isOrganizationAccessToken: () => false, |
| 34 | +})); |
| 35 | +vi.mock("~/services/realtime/jwtAuth.server", () => ({ |
| 36 | + isPublicJWT: (token: string) => token.startsWith("jwt_"), |
| 37 | + validatePublicJwtKey: jwtMocks.validatePublicJwtKey, |
| 38 | +})); |
| 39 | +vi.mock("~/services/logger.server", () => ({ |
| 40 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn() }, |
| 41 | +})); |
| 42 | + |
| 43 | +import { authenticateApiKey } from "~/services/apiAuth.server"; |
| 44 | + |
| 45 | +const environment = { id: "env_1", apiKey: "tr_prod_abc" }; |
| 46 | + |
| 47 | +function claims(extra: Record<string, unknown> = {}) { |
| 48 | + return { sub: environment.id, pub: true, scopes: ["read:runs"], ...extra }; |
| 49 | +} |
| 50 | + |
| 51 | +async function authenticate(jwtClaims: Record<string, unknown>) { |
| 52 | + jwtMocks.validatePublicJwtKey.mockResolvedValue({ |
| 53 | + ok: true, |
| 54 | + environment, |
| 55 | + claims: jwtClaims, |
| 56 | + }); |
| 57 | + const result = await authenticateApiKey("jwt_token", { allowJWT: true }); |
| 58 | + if (!result) throw new Error("expected authentication to succeed"); |
| 59 | + return result; |
| 60 | +} |
| 61 | + |
| 62 | +describe("PUBLIC_JWT authentication — actor claim", () => { |
| 63 | + beforeEach(() => { |
| 64 | + jwtMocks.validatePublicJwtKey.mockReset(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("surfaces actor when the JWT carries act", async () => { |
| 68 | + const result = await authenticate( |
| 69 | + claims({ act: { sub: "usr_42", client: "dashboard-agent" } }) |
| 70 | + ); |
| 71 | + |
| 72 | + expect(result.actor).toEqual({ sub: "usr_42", client: "dashboard-agent" }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("accepts act without a client", async () => { |
| 76 | + const result = await authenticate(claims({ act: { sub: "usr_42" } })); |
| 77 | + |
| 78 | + expect(result.actor).toEqual({ sub: "usr_42" }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("leaves actor undefined when the JWT has no act", async () => { |
| 82 | + const result = await authenticate(claims()); |
| 83 | + |
| 84 | + expect(result.actor).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("ignores a malformed act rather than failing the request", async () => { |
| 88 | + const result = await authenticate(claims({ act: { client: "dashboard-agent" } })); |
| 89 | + |
| 90 | + expect(result.ok).toBe(true); |
| 91 | + expect(result.actor).toBeUndefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("does not let act widen authorization", async () => { |
| 95 | + // Authorization comes from sub + scopes only. An act claim stuffed with |
| 96 | + // extra scopes is identity data and grants nothing. |
| 97 | + const forged = claims({ |
| 98 | + act: { sub: "usr_42", client: "dashboard-agent", scopes: ["admin"] }, |
| 99 | + }); |
| 100 | + const result = await authenticate(forged); |
| 101 | + |
| 102 | + expect(result.environment.id).toBe(environment.id); |
| 103 | + expect(result.actor).toEqual({ sub: "usr_42", client: "dashboard-agent" }); |
| 104 | + |
| 105 | + const ability = buildJwtAbility(forged.scopes); |
| 106 | + expect(ability.rules).toEqual(buildJwtAbility(["read:runs"]).rules); |
| 107 | + expect(ability.can("read", { type: "runs" })).toBe(true); |
| 108 | + expect(ability.can("write", { type: "runs" })).toBe(false); |
| 109 | + }); |
| 110 | +}); |
0 commit comments