Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

3.0 KB
Raw Blame
import {
  createBasicAuthHeader,
  createOrgProjectAndApiKey,
  hashSecretKey,
} from "@langfuse/shared/src/server";
import { prisma } from "@langfuse/shared/src/db";
import { OrganizationId, ProjectId } from "@langfuse/shared/rbac";

import { Authenticator } from "@/src/features/apiKey/authenticator";
import { authorize } from "@/src/features/rbac/authorize";
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";

describe("policy authenticate() composition", () => {
  let fixture: Awaited<ReturnType<typeof createOrgProjectAndApiKey>>;

  const basicHeader = () =>
    createBasicAuthHeader(fixture.publicKey, fixture.secretKey);

  const backfillFastHash = async () => {
    await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
      basicHeader(),
    );
  };

  const auth = (authorization: string) =>
    new Authenticator().authenticate({ headers: { authorization } });

  beforeEach(async () => {
    fixture = await createOrgProjectAndApiKey({ plan: "Hobby" });
  });

  it("resolves a project context from Basic auth", async () => {
    const result = await auth(basicHeader());
    expect(result.success).toBe(true);
    if (!result.success) return;
    expect(result.context.principal.kind).toBe("apiKey");
    expect(
      authorize(
        result.context,
        OrganizationId(fixture.orgId),
        "traces:read",
        ProjectId(fixture.projectId),
      ).success,
    ).toBe(true);
  });

  it("authenticates a private key over Bearer with full project scope", async () => {
    await backfillFastHash();
    const result = await auth(`Bearer ${fixture.secretKey}`);
    expect(result.success).toBe(true);
    if (!result.success) return;
    expect(
      authorize(
        result.context,
        OrganizationId(fixture.orgId),
        "traces:read",
        ProjectId(fixture.projectId),
      ).success,
    ).toBe(true);
  });

  it("authenticates a public key over Bearer with scores-only scope", async () => {
    const result = await auth(`Bearer ${fixture.publicKey}`);
    expect(result.success).toBe(true);
    if (!result.success) return;
    expect(
      authorize(
        result.context,
        OrganizationId(fixture.orgId),
        "scores:save",
        ProjectId(fixture.projectId),
      ).success,
    ).toBe(true);
    expect(
      authorize(
        result.context,
        OrganizationId(fixture.orgId),
        "traces:read",
        ProjectId(fixture.projectId),
      ).success,
    ).toBe(false);
  });

  it("401s an unknown token", async () => {
    const result = await auth("Bearer not-a-real-key");
    expect(result.success).toBe(false);
  });

  it("authenticates a NULL fast-hash key over Basic via the slow-hash path", async () => {
    await prisma.apiKey.update({
      where: { publicKey: fixture.publicKey },
      data: {
        hashedSecretKey: await hashSecretKey(fixture.secretKey),
        fastHashedSecretKey: null,
      },
    });
    expect((await auth(basicHeader())).success).toBe(true);
  });
});