Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import type { FastifyRequest } from "fastify";
import jwt from "jsonwebtoken";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { crypto } from "@app/lib/crypto";
import { AuthMethod, AuthMode, AuthTokenType } from "@app/services/auth/auth-type";
import { extractAuth } from "./inject-identity";
vi.mock("@app/lib/logger", () => ({
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }
}));
vi.mock("@app/lib/config/env", async (importOriginal) => ({
...(await importOriginal<typeof import("@app/lib/config/env")>()),
getConfig: () => ({ INFISICAL_CLOUD: true, INTERNAL_REGION: "us" })
}));
const AUTH_SECRET = "test-secret-for-extract-auth-unit-tests";
const baseAccessClaims = {
authTokenType: AuthTokenType.ACCESS_TOKEN as const,
authMethod: AuthMethod.EMAIL,
userId: "user-1",
tokenVersionId: "session-1",
accessVersion: 1,
organizationId: "org-1"
};
const sign = (claims: Record<string, unknown>) => crypto.jwt().sign(claims, AUTH_SECRET, { expiresIn: "1h" });
const makeReq = (authorization?: string, apiKey?: string) =>
({
headers: {
authorization,
...(apiKey ? { "x-api-key": apiKey } : {})
}
}) as unknown as FastifyRequest;
describe("extractAuth — OAuth access token classification", () => {
beforeAll(async () => {
process.env.FIPS_ENABLED = "false";
await crypto.initialize({} as never, {} as never, {} as never);
});
afterAll(() => {
delete process.env.FIPS_ENABLED;
});
test("an access token carrying oauthClientId is classified as AuthMode.OAUTH", async () => {
const token = sign({ ...baseAccessClaims, oauthClientId: "oauth_client_abc" });
const result = await extractAuth(makeReq(`Bearer ${token}`), AUTH_SECRET);
expect(result.authMode).toBe(AuthMode.OAUTH);
expect((result.token as { oauthClientId?: string }).oauthClientId).toBe("oauth_client_abc");
});
test("a plain access token (no marker) remains a first-party AuthMode.JWT", async () => {
const token = sign(baseAccessClaims);
const result = await extractAuth(makeReq(`Bearer ${token}`), AUTH_SECRET);
expect(result.authMode).toBe(AuthMode.JWT);
});
test("a service token is still detected by its st. prefix, never as OAuth", async () => {
const result = await extractAuth(makeReq("Bearer st.some-service-token"), AUTH_SECRET);
expect(result.authMode).toBe(AuthMode.SERVICE_TOKEN);
});
test("an x-api-key header short-circuits before token parsing", async () => {
const token = sign({ ...baseAccessClaims, oauthClientId: "oauth_client_abc" });
const result = await extractAuth(makeReq(`Bearer ${token}`, "my-api-key"), AUTH_SECRET);
expect(result.authMode).toBe(AuthMode.API_KEY);
});
test("a missing authorization header yields no auth mode", async () => {
const result = await extractAuth(makeReq(undefined), AUTH_SECRET);
expect(result.authMode).toBeNull();
});
});
describe("extractAuth — token from another instance", () => {
beforeAll(async () => {
process.env.FIPS_ENABLED = "false";
await crypto.initialize({} as never, {} as never, {} as never);
});
afterAll(() => {
delete process.env.FIPS_ENABLED;
});
test("a token signed with another instance's secret names the likely cause", async () => {
const token = crypto.jwt().sign(baseAccessClaims, "another-instance-secret", { expiresIn: "1h" });
const attempt = extractAuth(makeReq(`Bearer ${token}`), AUTH_SECRET);
// Still a JsonWebTokenError, so the error handler answers with the same 403 TokenError as before.
await expect(attempt).rejects.toBeInstanceOf(jwt.JsonWebTokenError);
await expect(attempt).rejects.toThrow("may have been issued by a different Infisical instance: this is US Cloud");
});
test("other token errors keep the library message", async () => {
const attempt = extractAuth(makeReq("Bearer not-a-jwt"), AUTH_SECRET);
await expect(attempt).rejects.toThrow("jwt malformed");
});
});