File size: 2,559 Bytes
311fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b4ed52
 
 
 
 
 
 
 
 
311fa35
2b4ed52
311fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { describe, expect, it } from "vitest";
import { apiKeysMatch, extractApiKey, PUBLIC_API_PATHS, requiresApiAuth } from "./auth.js";

describe("extractApiKey", () => {
  it("reads a Bearer token from Authorization", () => {
    expect(extractApiKey({ authorization: "Bearer secret-1" })).toBe("secret-1");
    expect(extractApiKey({ authorization: "bearer   secret-2  " })).toBe("secret-2");
  });

  it("reads X-API-Key", () => {
    expect(extractApiKey({ "x-api-key": "header-key" })).toBe("header-key");
  });

  it("reads the api_key query parameter", () => {
    expect(extractApiKey({}, { api_key: "query-key" })).toBe("query-key");
    expect(extractApiKey({}, { api_key: ["first", "second"] })).toBe("first");
  });

  it("prefers Authorization over X-API-Key and query", () => {
    expect(
      extractApiKey(
        { authorization: "Bearer auth-key", "x-api-key": "header-key" },
        {
          api_key: "query-key",
        },
      ),
    ).toBe("auth-key");
  });

  it("returns null when no key is present", () => {
    expect(extractApiKey({})).toBeNull();
    expect(extractApiKey({ authorization: "Basic abc" })).toBeNull();
    expect(extractApiKey({ "x-api-key": "" })).toBeNull();
  });
});

describe("apiKeysMatch", () => {
  it("matches equal keys", () => {
    expect(apiKeysMatch("abc", "abc")).toBe(true);
  });

  it("rejects different keys", () => {
    expect(apiKeysMatch("abc", "abd")).toBe(false);
    expect(apiKeysMatch("abc", "abcd")).toBe(false);
  });

  it("rejects a missing key", () => {
    expect(apiKeysMatch(null, "abc")).toBe(false);
  });
});

describe("public paths", () => {
  it("leaves only the health check public", () => {
    expect(requiresApiAuth("/api/v1/health")).toBe(false);
    expect(requiresApiAuth("/api/v1/openapi.json")).toBe(true);
    expect(requiresApiAuth("/api/v1/docs")).toBe(true);
    expect(requiresApiAuth("/api/v1/jobs")).toBe(true);
  });
});

describe("requiresApiAuth", () => {
  it("keeps the health check public", () => {
    for (const path of PUBLIC_API_PATHS) {
      expect(requiresApiAuth(path)).toBe(false);
      expect(requiresApiAuth(`${path}/`)).toBe(false);
    }
  });

  it("requires auth for job routes", () => {
    expect(requiresApiAuth("/api/v1/jobs")).toBe(true);
    expect(requiresApiAuth("/api/v1/jobs/abc/events")).toBe(true);
    expect(requiresApiAuth("/api/v1/stats")).toBe(true);
  });

  it("ignores non-API routes", () => {
    expect(requiresApiAuth("/")).toBe(false);
    expect(requiresApiAuth("/jobs/abc")).toBe(false);
  });
});