forked from NateBJones-Projects/OB1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
61 lines (54 loc) · 1.76 KB
/
Copy pathauth.test.ts
File metadata and controls
61 lines (54 loc) · 1.76 KB
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
import { assertEquals } from "jsr:@std/assert@1";
import { accessKeyMatches, parseBearerToken, selectAccessKey } from "./auth.ts";
function headers(values: Record<string, string>) {
const normalized = new Map(
Object.entries(values).map(([key, value]) => [key.toLowerCase(), value]),
);
return {
get(name: string) {
return normalized.get(name.toLowerCase()) ?? null;
},
};
}
Deno.test("parseBearerToken accepts Authorization Bearer keys", () => {
assertEquals(parseBearerToken("Bearer secret-key"), "secret-key");
assertEquals(parseBearerToken("bearer secret-key"), "secret-key");
});
Deno.test("selectAccessKey prefers x-brain-key over Authorization Bearer", () => {
assertEquals(
selectAccessKey(
headers({
"x-brain-key": "header-key",
authorization: "Bearer bearer-key",
}),
"https://example.test",
),
"header-key",
);
});
Deno.test("selectAccessKey accepts Authorization Bearer when x-brain-key is absent", () => {
assertEquals(
selectAccessKey(
headers({ authorization: "Bearer bearer-key" }),
"https://example.test",
),
"bearer-key",
);
});
Deno.test("selectAccessKey ignores query string keys unless explicitly allowed", () => {
assertEquals(
selectAccessKey(headers({}), "https://example.test?key=query-key"),
undefined,
);
assertEquals(
selectAccessKey(headers({}), "https://example.test?key=query-key", {
allowQueryKey: true,
}),
"query-key",
);
});
Deno.test("accessKeyMatches requires provided and expected keys to match", () => {
assertEquals(accessKeyMatches("secret-key", "secret-key"), true);
assertEquals(accessKeyMatches("secret-key", "other-key"), false);
assertEquals(accessKeyMatches(undefined, "secret-key"), false);
});