Skip to content

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

39.7 KB
Raw Blame
import { vi } from "vitest";
import {
  OrgEnrichedApiKey,
  createApiKey,
  createBasicAuthHeader,
  createOrgProjectAndApiKey,
  createShaHash,
  generateKeySet,
  getDisplaySecretKey,
  hashSecretKey,
  logger,
} from "@langfuse/shared/src/server";
import { Prisma, type PrismaClient, prisma } from "@langfuse/shared/src/db";
import { env } from "@/src/env.mjs";
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { v4 } from "uuid";
import {
  OrganizationId,
  ProjectId,
  SystemRoleId,
  UserId,
} from "@langfuse/shared/rbac";
import {
  clearRedisKeysByPatternSafely,
  createRedisTestClient,
  ensureRedisReady,
  getRedisKeysByPattern,
  getRedisTtl,
  getRedisValue,
  setRedisValue,
  type RedisTestClient,
} from "@/src/__tests__/server/redis-test-utils";

describe("Authenticate API calls", () => {
  type TestApiKeyFixture = {
    id: string;
    auth: string;
    publicKey: string;
    secretKey: string;
    orgId: string;
    projectId: string;
    note: string;
  };

  let testApiKey: TestApiKeyFixture;
  const cacheKeyPrefix = `api-auth-test:${v4()}:`;

  const createRedisClient = (): RedisTestClient => {
    return createRedisTestClient({
      maxRetriesPerRequest: null,
      keyPrefix: cacheKeyPrefix,
    });
  };

  const getValidAuthHeader = () => testApiKey.auth;

  const getInvalidAuthHeader = () =>
    createBasicAuthHeader(
      testApiKey.publicKey,
      `${testApiKey.secretKey}-wrong`,
    );

  const createMissingAuthHeader = () => {
    const secretKey = `sk-missing-${v4()}`;
    return {
      auth: createBasicAuthHeader(`pk-missing-${v4()}`, secretKey),
      redisKey: `api-key:${createShaHash(secretKey, env.SALT)}`,
    };
  };

  const getApiKeyCacheKeys = async (
    redisClient: RedisTestClient,
    pattern = "api-key*",
  ) => {
    return await getRedisKeysByPattern(redisClient, pattern);
  };

  const clearApiKeyCacheSafely = async (redisClient: RedisTestClient) => {
    await clearRedisKeysByPatternSafely(redisClient, "api-key*");
  };

  beforeEach(async () => {
    const fixture = await createOrgProjectAndApiKey({ plan: "Hobby" });
    const note = "seeded key";
    const createdApiKey = await prisma.apiKey.findUniqueOrThrow({
      where: { publicKey: fixture.publicKey },
    });
    await prisma.apiKey.update({
      where: { id: createdApiKey.id },
      data: { note },
    });
    testApiKey = {
      id: createdApiKey.id,
      auth: fixture.auth,
      publicKey: fixture.publicKey,
      secretKey: fixture.secretKey,
      orgId: fixture.orgId,
      projectId: fixture.projectId,
      note,
    };
  });

  describe("validates without redis", () => {
    it("should create new api key", async () => {
      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth.validKey).toBe(true);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();
    });

    it("should create new api key and succeed with new key", async () => {
      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      expect(auth.validKey).toBe(true);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const auth2 = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      expect(auth2.validKey).toBe(true);
    });

    it("should create new api key with stripe data and succeed with new key", async () => {
      await prisma.organization.update({
        where: {
          id: testApiKey.orgId,
        },
        data: {
          cloudConfig: {
            stripe: {
              customerId: "cus_test123",
              activeSubscriptionId: "sub_test123",
              activeProductId: "prod_test123",
            },
          },
        },
      });

      await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      expect(auth.validKey).toBe(true);

      if (auth.validKey) {
        expect(auth.scope.orgId).toBe(testApiKey.orgId);
        expect(auth.scope.plan).toBe("cloud:hobby");
        expect(auth.scope.rateLimitOverrides).toEqual([]);
      }

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      await prisma.organization.update({
        where: {
          id: testApiKey.orgId,
        },
        data: {
          cloudConfig: Prisma.JsonNull,
        },
      });
    });

    it("should create new api key with custom rate limits and succeed with new key", async () => {
      await prisma.organization.update({
        where: {
          id: testApiKey.orgId,
        },
        data: {
          cloudConfig: {
            rateLimitOverrides: [
              {
                resource: "ingestion",
                points: 100,
                durationInSec: 60,
              },
            ],
          },
        },
      });

      await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      expect(auth.validKey).toBe(true);

      if (auth.validKey) {
        expect(auth.scope.orgId).toBe(testApiKey.orgId);
        expect(auth.scope.plan).toBe("cloud:hobby");
        expect(auth.scope.rateLimitOverrides).toEqual([
          {
            resource: "ingestion",
            points: 100,
            durationInSec: 60,
          },
        ]);
      }

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      await prisma.organization.update({
        where: {
          id: testApiKey.orgId,
        },
        data: {
          cloudConfig: Prisma.JsonNull,
        },
      });
    });

    it("should fail on wrong api key with new key", async () => {
      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      expect(auth.validKey).toBe(true);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });

      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const wrongAuth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getInvalidAuthHeader());
      expect(wrongAuth.validKey).toBe(false);
    });

    it("should fail on wrong api key without new key", async () => {
      const legacySecretKey = ["legacy", "secret", "key"].join("-");
      const legacyPublicKey = `legacy-public-key-${v4()}`;
      // eslint-disable-next-line @repo/no-restricted-prisma-writes -- needs a bcrypt-only legacy key, which createApiKey never produces.
      await prisma.apiKey.create({
        data: {
          id: v4(),
          projectId: testApiKey.projectId,
          publicKey: legacyPublicKey,
          hashedSecretKey: await hashSecretKey(legacySecretKey),
          displaySecretKey: getDisplaySecretKey(legacySecretKey),
          scope: "PROJECT",
        },
      });

      const initialApiKey = await prisma.apiKey.findUnique({
        where: { publicKey: legacyPublicKey },
      });
      expect(initialApiKey).not.toBeNull();
      expect(initialApiKey?.fastHashedSecretKey).toBeNull();

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(
        createBasicAuthHeader(legacyPublicKey, `${legacySecretKey}-wrong`),
      );
      expect(auth.validKey).toBe(false);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: legacyPublicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).toBeNull();
    });

    it("rejects in-app agent API keys by default", async () => {
      const keyCreator = await prisma.user.create({
        data: { email: `apikey-creator-${v4()}@example.com` },
      });
      const apiKey = await createApiKey(prisma, {
        tenant: OrganizationId(testApiKey.orgId),
        scope: ProjectId(testApiKey.projectId),
        role: SystemRoleId("LEGACY_PROJECT_API_KEY"),
        createdBy: UserId(keyCreator.id),
        isInAppAgentKey: true,
      });

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(
        createBasicAuthHeader(apiKey.publicKey, apiKey.secretKey),
      );

      expect(auth).toEqual({
        validKey: false,
        error:
          "Access denied - in-app agent keys are not allowed for this endpoint",
      });
    });

    it("allows in-app agent API keys when explicitly enabled", async () => {
      const keyCreator = await prisma.user.create({
        data: { email: `apikey-creator-${v4()}@example.com` },
      });
      const apiKey = await createApiKey(prisma, {
        tenant: OrganizationId(testApiKey.orgId),
        scope: ProjectId(testApiKey.projectId),
        role: SystemRoleId("LEGACY_PROJECT_API_KEY"),
        createdBy: UserId(keyCreator.id),
        isInAppAgentKey: true,
      });

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(
        createBasicAuthHeader(apiKey.publicKey, apiKey.secretKey),
        { allowInAppAgentKey: true },
      );

      expect(auth.validKey).toBe(true);
      if (auth.validKey) {
        expect(auth.scope.isInAppAgentKey).toBe(true);
      }
    });

    it("returns the API key's actual public key in scope and warns when the submitted public key does not match", async () => {
      // first use sets fastHashedSecretKey so the secret-hash lookup path is taken
      await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const warnSpy = vi.spyOn(logger, "warn");
      const submittedPublicKey = `pk-lf-mismatch-${v4()}`;

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(
        createBasicAuthHeader(submittedPublicKey, testApiKey.secretKey),
      );

      expect(auth.validKey).toBe(true);
      if (auth.validKey) {
        expect(auth.scope.publicKey).toBe(testApiKey.publicKey);
        expect(auth.scope.projectId).toBe(testApiKey.projectId);
      }

      const mismatchWarning = warnSpy.mock.calls
        .map((call) => String(call[0]))
        .find((message) => message.includes("Public key mismatch"));
      expect(mismatchWarning).toBeDefined();
      expect(mismatchWarning).toContain(submittedPublicKey);
      expect(mismatchWarning).toContain(testApiKey.publicKey);
      expect(mismatchWarning).toContain(testApiKey.projectId);

      warnSpy.mockRestore();
    });

    it("redacts a secret key submitted in the public key slot from the mismatch warning", async () => {
      await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const warnSpy = vi.spyOn(logger, "warn");

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(
        createBasicAuthHeader(testApiKey.secretKey, testApiKey.secretKey),
      );

      expect(auth.validKey).toBe(true);

      const mismatchWarning = warnSpy.mock.calls
        .map((call) => String(call[0]))
        .find((message) => message.includes("Public key mismatch"));
      expect(mismatchWarning).toBeDefined();
      expect(mismatchWarning).not.toContain(testApiKey.secretKey);
      expect(mismatchWarning).toContain(
        getDisplaySecretKey(testApiKey.secretKey),
      );
      expect(mismatchWarning).toContain(testApiKey.publicKey);

      warnSpy.mockRestore();
    });

    it("does not warn when the submitted public key matches", async () => {
      await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const warnSpy = vi.spyOn(logger, "warn");

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth.validKey).toBe(true);
      if (auth.validKey) {
        expect(auth.scope.publicKey).toBe(testApiKey.publicKey);
      }
      expect(
        warnSpy.mock.calls.filter((call) =>
          String(call[0]).includes("Public key mismatch"),
        ),
      ).toHaveLength(0);

      warnSpy.mockRestore();
    });

    it("accepts a key that has not expired yet", async () => {
      await prisma.apiKey.update({
        where: { id: testApiKey.id },
        data: { expiresAt: new Date(Date.now() + 60 * 60_000) },
      });

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth.validKey).toBe(true);
    });

    it("rejects an expired key over basic auth like an unknown key", async () => {
      await prisma.apiKey.update({
        where: { id: testApiKey.id },
        data: { expiresAt: new Date(Date.now() - 60_000) },
      });

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth).toEqual({
        validKey: false,
        error:
          "Invalid credentials. Confirm that you've configured the correct host.",
      });
    });

    it("rejects an expired key over public-key bearer auth like an unknown key", async () => {
      await prisma.apiKey.update({
        where: { id: testApiKey.id },
        data: { expiresAt: new Date(Date.now() - 60_000) },
      });

      const auth = await new ApiAuthService(
        prisma,
        null,
      ).verifyAuthHeaderAndReturnScope(`Bearer ${testApiKey.publicKey}`);

      expect(auth).toEqual({
        validKey: false,
        error:
          "Invalid public key. Confirm that you've configured the correct host.",
      });
    });
  });

  describe("validates with redis", () => {
    let redis: RedisTestClient;

    beforeAll(async () => {
      redis = createRedisClient();
      await ensureRedisReady(redis);
    }, 20_000);

    beforeEach(async () => {
      if (redis.status === "close" || redis.status === "end") {
        redis = createRedisClient();
      }
      await ensureRedisReady(redis);
      // if we do not remove the key, it will remain in the cache and
      // calling the test twice will not add the key to the cache
      await clearApiKeyCacheSafely(redis);
    }, 20_000);

    afterEach(async () => {
      // if we do not remove the key, it will remain in the cache and
      // calling the test twice will not add the key to the cache
      await clearApiKeyCacheSafely(redis);
    }, 20_000);

    afterAll(() => {
      redis.disconnect();
    }, 20_000);

    it("clears only this suite's API-key cache entries", async () => {
      const otherClient = createRedisTestClient({ keyPrefix: "" });
      const ownKey = `api-key:${v4()}`;
      const otherKey = `api-key:${v4()}`;
      try {
        await setRedisValue(redis, ownKey, "owned");
        await setRedisValue(otherClient, otherKey, "other-suite");

        await clearApiKeyCacheSafely(redis);

        expect(await getRedisValue(redis, ownKey)).toBeNull();
        expect(await getRedisValue(otherClient, otherKey)).toBe("other-suite");
      } finally {
        await otherClient.del(otherKey);
        otherClient.disconnect();
      }
    });

    it("should create new api key and read from cache", async () => {
      const legacySecretKey = ["legacy", "secret", "key", v4()].join("-");
      const legacyPublicKey = `legacy-public-key-${v4()}`;
      const legacyAuth = createBasicAuthHeader(
        legacyPublicKey,
        legacySecretKey,
      );
      // eslint-disable-next-line @repo/no-restricted-prisma-writes -- needs a bcrypt-only legacy key, which createApiKey never produces.
      await prisma.apiKey.create({
        data: {
          id: v4(),
          projectId: testApiKey.projectId,
          publicKey: legacyPublicKey,
          hashedSecretKey: await hashSecretKey(legacySecretKey),
          displaySecretKey: getDisplaySecretKey(legacySecretKey),
          scope: "PROJECT",
        },
      });

      // update the organization with a cloud config
      await prisma.organization.update({
        where: { id: testApiKey.orgId },
        data: {
          cloudConfig: {
            rateLimitOverrides: [
              {
                resource: "public-api",
                points: 1000,
                durationInSec: 60,
              },
              {
                resource: "ingestion",
              },
            ],
          },
        },
      });

      // first auth will generate the fast hashed api key
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        legacyAuth,
      );

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: legacyPublicKey },
      });
      const organization = await prisma.organization.findUniqueOrThrow({
        where: { id: testApiKey.orgId },
      });

      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).toBeNull();

      // second will add the key to redis
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        legacyAuth,
      );

      const cachedKey2 = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );

      expect(cachedKey2).not.toBeNull();

      const parsed = OrgEnrichedApiKey.parse(JSON.parse(cachedKey2!));

      expect(parsed).toEqual({
        ...apiKey,
        orgId: testApiKey.orgId,
        plan: "cloud:hobby",
        rateLimitOverrides: [
          {
            resource: "public-api",
            points: 1000,
            durationInSec: 60,
          },
          {
            resource: "ingestion",
          },
        ],
        createdAt: apiKey?.createdAt.toISOString(),
        organizationCreatedAt: organization.createdAt.toISOString(),
        isIngestionSuspended: expect.anything(),
        accessAuditLogEnabled: false,
      });

      await prisma.organization.update({
        where: { id: testApiKey.orgId },
        data: {
          cloudConfig: Prisma.JsonNull,
        },
      });
    });

    it("should handle non-scoped key format gracefully and fallback to prisma", async () => {
      // update the organization with a cloud config
      await prisma.organization.update({
        where: { id: testApiKey.orgId },
        data: {
          cloudConfig: {
            rateLimitOverrides: [
              {
                resource: "public-api",
                points: 1000,
                durationInSec: 60,
              },
              {
                resource: "ingestion",
              },
            ],
          },
        },
      });

      // first auth will generate the fast hashed api key
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });

      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      // Manually add a non-scoped key to Redis (missing the scope property)
      const nonScopedKey = {
        id: "seed-api-key",
        note: "seeded key",
        publicKey: testApiKey.publicKey,
        displaySecretKey: "sk-lf-...7890",
        createdAt: new Date().toISOString(),
        lastUsedAt: null,
        expiresAt: null,
        fastHashedSecretKey: apiKey?.fastHashedSecretKey,
        hashedSecretKey: apiKey?.hashedSecretKey,
        orgId: testApiKey.orgId,
        plan: "cloud:team",
        projectId: testApiKey.projectId,
        // scope property is intentionally missing
      };

      // Add the non-scoped key to Redis
      await setRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
        JSON.stringify(nonScopedKey),
      );

      // Verify the key is in Redis
      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).not.toBeNull();

      // Parse should fail because the scope is missing
      expect(() => {
        OrgEnrichedApiKey.parse(JSON.parse(cachedKey!));
      }).toThrow("invalid_union");

      // Auth should still succeed by falling back to Postgres
      const verification = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(verification.validKey).toBe(true);

      // The invalid key should be removed from Redis
      const cachedKeyAfterAuth = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );

      // A new valid key should be added to Redis after fallback to Postgres
      expect(cachedKeyAfterAuth).not.toBeNull();

      // The new key should be properly formatted with a scope
      const parsed = OrgEnrichedApiKey.parse(JSON.parse(cachedKeyAfterAuth!));

      expect(parsed).toEqual({
        ...apiKey,
        orgId: testApiKey.orgId,
        plan: "cloud:hobby",
        scope: "PROJECT", // Now the scope is present
        projectId: testApiKey.projectId,
        rateLimitOverrides: [
          {
            resource: "public-api",
            points: 1000,
            durationInSec: 60,
          },
          {
            resource: "ingestion",
          },
        ],
        createdAt: apiKey?.createdAt.toISOString(),
        organizationCreatedAt: expect.any(String),
        isIngestionSuspended: expect.anything(),
        accessAuditLogEnabled: false,
      });

      await prisma.organization.update({
        where: { id: testApiKey.orgId },
        data: {
          cloudConfig: Prisma.JsonNull,
        },
      });
    });

    it("searching for non-existing key stores flag in redis and fails auth", async () => {
      // key does not exist in database
      const missingAuthHeader = createMissingAuthHeader();

      const verification = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(missingAuthHeader.auth);

      expect(verification.validKey).toBe(false);

      const redisKeys = await getApiKeyCacheKeys(
        redis,
        missingAuthHeader.redisKey,
      );
      expect(redisKeys.length).toBe(1);
      const redisValue = await getRedisValue(redis, missingAuthHeader.redisKey);
      expect(redisValue).toBe('"api-key-non-existent"');
    });

    it("searching for non-existing key again fails auth", async () => {
      // key does not exist in database
      const missingAuthHeader = createMissingAuthHeader();

      const verification = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(missingAuthHeader.auth);

      expect(verification.validKey).toBe(false);

      const redisKeys = await getApiKeyCacheKeys(
        redis,
        missingAuthHeader.redisKey,
      );
      expect(redisKeys.length).toBe(1);
      const redisValue = await getRedisValue(redis, missingAuthHeader.redisKey);
      expect(redisValue).toBe('"api-key-non-existent"');

      const verification2 = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(missingAuthHeader.auth);
      expect(verification2.validKey).toBe(false);

      const redisKeys2 = await getApiKeyCacheKeys(
        redis,
        missingAuthHeader.redisKey,
      );
      expect(redisKeys2.length).toBe(1);
      const redisValue2 = await getRedisValue(
        redis,
        missingAuthHeader.redisKey,
      );
      expect(redisValue2).toBe('"api-key-non-existent"');
    });

    it("prisma should not be used when reading cached keys", async () => {
      // Mock prisma
      const mockPrisma = {
        apiKey: {
          findUnique: vi.fn(),
        },
      };

      // first auth will generate the fast hashed api key
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      // second will add the key to redis
      const auth2 = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      // third will read from redis only
      await new ApiAuthService(
        mockPrisma as unknown as PrismaClient,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth2.validKey).toBe(true);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      // Ensure prisma was not called
      expect(mockPrisma.apiKey.findUnique).not.toHaveBeenCalled();

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).not.toBeNull();

      const parsed = OrgEnrichedApiKey.parse(JSON.parse(cachedKey!));

      expect(parsed).toEqual({
        id: expect.any(String),
        note: "seeded key",
        publicKey: testApiKey.publicKey,
        hashedSecretKey: expect.any(String),
        fastHashedSecretKey: expect.any(String),
        displaySecretKey: expect.any(String),
        createdAt: expect.any(String),
        lastUsedAt: null,
        expiresAt: null,
        isIngestionSuspended: expect.anything(),
        accessAuditLogEnabled: false,
        isInAppAgentKey: false,
        createdByUserId: null,
        createdByApiKeyId: null,
        userId: null,
        projectId: expect.any(String),
        orgId: testApiKey.orgId,
        plan: "cloud:hobby",
        scope: "PROJECT",
        rateLimitOverrides: [
          {
            resource: "public-api-legacy",
            points: null,
            durationInSec: null,
          },
        ],
        organizationCreatedAt: expect.any(String),
      });
    });

    it("carries the org's access-audit opt-in through the cached key", async () => {
      await prisma.organization.update({
        where: { id: testApiKey.orgId },
        data: { accessAuditLogEnabled: true },
      });
      const mockPrisma = { apiKey: { findUnique: vi.fn() } };

      // first auth generates the fast hashed api key, second caches it
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );
      const fromDb = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());
      const fromCache = await new ApiAuthService(
        mockPrisma as unknown as PrismaClient,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(mockPrisma.apiKey.findUnique).not.toHaveBeenCalled();
      expect(fromDb).toMatchObject({
        validKey: true,
        scope: { accessAuditLogEnabled: true },
      });
      expect(fromCache).toMatchObject({
        validKey: true,
        scope: { accessAuditLogEnabled: true },
      });
    });

    it("ttl should not be extended when reading from redis", async () => {
      // Mock prisma
      const mockPrisma = {
        apiKey: {
          findUnique: vi.fn(),
        },
      };

      // first auth will generate the fast hashed api key
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      // second will add the key to redis
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      // third will read from redis only
      await new ApiAuthService(
        mockPrisma as unknown as PrismaClient,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const redisKey = `api-key:${apiKey?.fastHashedSecretKey}`;
      const ttl = await getRedisTtl(redis, redisKey);

      expect(ttl).toBeGreaterThan(env.LANGFUSE_CACHE_API_KEY_TTL_SECONDS - 2);

      await redis.expire(redisKey, 10);
      const shortenedTtl = await getRedisTtl(redis, redisKey);
      expect(shortenedTtl).toBeGreaterThan(0);
      expect(shortenedTtl).toBeLessThan(env.LANGFUSE_CACHE_API_KEY_TTL_SECONDS);

      // A cache hit must not push the expiry out. The entry has to age out a
      // fixed TTL after it was written, so that revoking a key which is still
      // in use takes effect within one TTL even if the eviction is missed.
      const cachedAuth = await new ApiAuthService(
        mockPrisma as unknown as PrismaClient,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(cachedAuth.validKey).toBe(true);
      expect(mockPrisma.apiKey.findUnique).not.toHaveBeenCalled();

      const ttl2 = await getRedisTtl(redis, redisKey);

      expect(ttl2).toBeGreaterThan(0);
      expect(ttl2).toBeLessThanOrEqual(10);
    });

    it("rejects a cached key once it has expired", async () => {
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );
      const apiKey = await prisma.apiKey.findUniqueOrThrow({
        where: { publicKey: testApiKey.publicKey },
      });
      const redisKey = `api-key:${apiKey.fastHashedSecretKey}`;
      const cached = JSON.parse((await getRedisValue(redis, redisKey))!);
      await setRedisValue(
        redis,
        redisKey,
        JSON.stringify({
          ...cached,
          expiresAt: new Date(Date.now() - 60_000).toISOString(),
        }),
      );

      const auth = await new ApiAuthService(
        { apiKey: { findUnique: vi.fn() } } as unknown as PrismaClient,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth).toEqual({
        validKey: false,
        error:
          "Invalid credentials. Confirm that you've configured the correct host.",
      });
    });

    it("should delete API keys from cache and db", async () => {
      // first auth will generate the fast hashed api key
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      // second will add the key to redis
      const auth2 = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(auth2.validKey).toBe(true);

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });

      expect(apiKey).not.toBeNull();
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).not.toBeNull();

      const parsed = OrgEnrichedApiKey.parse(JSON.parse(cachedKey!));

      expect(parsed).toEqual({
        ...apiKey,
        orgId: testApiKey.orgId,
        plan: "cloud:hobby",
        createdAt: apiKey?.createdAt.toISOString(),
        organizationCreatedAt: expect.any(String),
        scope: "PROJECT",
        rateLimitOverrides: [
          {
            resource: "public-api-legacy",
            points: null,
            durationInSec: null,
          },
        ],
        isIngestionSuspended: expect.anything(),
        accessAuditLogEnabled: false,
      });

      await new ApiAuthService(prisma, redis).deleteApiKey(
        apiKey?.id!,
        apiKey?.projectId!,
        "PROJECT",
      );

      const deletedApiKey = await prisma.apiKey.findUnique({
        where: { id: apiKey?.id! },
      });
      expect(deletedApiKey).toBeNull();

      const deletedCachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(deletedCachedKey).toBeNull();
    });

    it("should revoke a key even if a request re-caches it mid-deletion", async () => {
      // generates the fast hashed secret key the cache is keyed by
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey?.fastHashedSecretKey).not.toBeNull();

      const redisKey = `api-key:${apiKey?.fastHashedSecretKey}`;
      await redis.del(redisKey);

      // Drives a request into the gap between the two steps of the deletion.
      // It misses the cache, still finds the row, and writes the key back with
      // a full TTL - so the eviction has to be the step that happens last.
      const racingPrisma = {
        apiKey: {
          findFirstOrThrow: () =>
            prisma.apiKey.findFirstOrThrow({ where: { id: apiKey!.id } }),
          delete: async () => {
            await new ApiAuthService(
              prisma,
              redis,
            ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

            expect(await getRedisValue(redis, redisKey)).not.toBeNull();

            return prisma.apiKey.delete({ where: { id: apiKey!.id } });
          },
        },
      } as unknown as PrismaClient;

      await new ApiAuthService(racingPrisma, redis).deleteApiKey(
        apiKey!.id,
        apiKey!.projectId!,
        "PROJECT",
      );

      expect(
        await prisma.apiKey.findUnique({ where: { id: apiKey!.id } }),
      ).toBeNull();
      expect(await getRedisValue(redis, redisKey)).toBeNull();

      const afterRevocation = await new ApiAuthService(
        prisma,
        redis,
      ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());

      expect(afterRevocation.validKey).toBe(false);
    });
  });

  describe("invalidates api keys in redis", () => {
    let redis: RedisTestClient;

    beforeAll(async () => {
      redis = createRedisClient();
      await ensureRedisReady(redis);
    }, 20_000);

    beforeEach(async () => {
      if (redis.status === "close" || redis.status === "end") {
        redis = createRedisClient();
      }
      await ensureRedisReady(redis);
      // if we do not remove the key, it will remain in the cache and
      // calling the test twice will not add the key to the cache
      await clearApiKeyCacheSafely(redis);
    }, 20_000);

    afterEach(async () => {
      // if we do not remove the key, it will remain in the cache and
      // calling the test twice will not add the key to the cache
      await clearApiKeyCacheSafely(redis);
    }, 20_000);

    afterAll(() => {
      redis.disconnect();
    }, 20_000);

    it("should invalidate organization API keys in redis", async () => {
      // put keys into cache
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).not.toBeNull();

      await new ApiAuthService(prisma, redis).invalidateCachedOrgApiKeys(
        testApiKey.orgId,
      );

      const invalidatedCachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(invalidatedCachedKey).toBeNull();
    });

    it("if no keys in redis, invalidating org keys should do nothing", async () => {
      const fastHashedSecretKey = `test-fast-hash-${v4()}`;
      await prisma.apiKey.update({
        where: { publicKey: testApiKey.publicKey },
        data: {
          fastHashedSecretKey,
        },
      });

      await new ApiAuthService(prisma, redis).invalidateCachedOrgApiKeys(
        testApiKey.orgId,
      );

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${fastHashedSecretKey}`,
      );
      expect(cachedKey).toBeNull();
    });

    it("if no keys in redis, invalidating org keys without fast hash should do nothing", async () => {
      await new ApiAuthService(prisma, redis).invalidateCachedOrgApiKeys(
        testApiKey.orgId,
      );

      const keysForCurrentApiKey = await getApiKeyCacheKeys(
        redis,
        `api-key:${createShaHash(testApiKey.secretKey, env.SALT)}`,
      );
      expect(keysForCurrentApiKey.length).toBe(0);
    });

    it("should invalidate project API keys in redis", async () => {
      // put keys into cache
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );
      await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
        getValidAuthHeader(),
      );

      const apiKey = await prisma.apiKey.findUnique({
        where: { publicKey: testApiKey.publicKey },
      });
      expect(apiKey).not.toBeNull();

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(cachedKey).not.toBeNull();

      await new ApiAuthService(prisma, redis).invalidateCachedProjectApiKeys(
        testApiKey.projectId,
      );

      const invalidatedCachedKey = await getRedisValue(
        redis,
        `api-key:${apiKey?.fastHashedSecretKey}`,
      );
      expect(invalidatedCachedKey).toBeNull();
    });

    it("if no keys in redis, invalidating project keys should do nothing", async () => {
      const fastHashedSecretKey = `test-fast-hash-${v4()}`;
      await prisma.apiKey.update({
        where: { publicKey: testApiKey.publicKey },
        data: {
          fastHashedSecretKey,
        },
      });

      await new ApiAuthService(prisma, redis).invalidateCachedProjectApiKeys(
        testApiKey.projectId,
      );

      const cachedKey = await getRedisValue(
        redis,
        `api-key:${fastHashedSecretKey}`,
      );
      expect(cachedKey).toBeNull();
    });

    it("if no keys in redis, invalidating project keys without fast hash should do nothing", async () => {
      await prisma.apiKey.update({
        where: { publicKey: testApiKey.publicKey },
        data: {
          fastHashedSecretKey: Math.random().toString(36).substring(2, 15),
        },
      });

      await new ApiAuthService(prisma, redis).invalidateCachedProjectApiKeys(
        testApiKey.projectId,
      );

      const keysForCurrentApiKey = await getApiKeyCacheKeys(
        redis,
        `api-key:${createShaHash(testApiKey.secretKey, env.SALT)}`,
      );
      expect(keysForCurrentApiKey.length).toBe(0);
    });
  });

  describe("API key format", () => {
    it("generated public and secret keys never contain a colon (30 iterations)", async () => {
      // Keys are built as `pk-lf-<uuid>` / `sk-lf-<uuid>`.
      // UUIDs only use hex digits (0-9, a-f) and hyphens, so colons are
      // structurally impossible – but we assert it explicitly so any future
      // change to the generation logic gets caught before it can break
      // Basic-Auth header parsing (which splits on ':').
      for (let i = 0; i < 30; i++) {
        const { pk, sk } = await generateKeySet();
        expect(pk).not.toContain(":");
        expect(sk).not.toContain(":");
      }
    });
  });
});