-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.test.ts
180 lines (151 loc) · 7.62 KB
/
mod.test.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { assertEquals, assertRejects } from "@std/assert";
import { test } from "@cross/test";
import { generateKey, generateKeyPair, signJWT, unsafeParseJOSEHeader, unsafeParseJWT, validateJWT } from "./mod.ts";
import { JWTAmbiguousClaimError, JWTFormatError, JWTValidationError } from "./src/error.ts";
import type { SupportedKeyAlgorithms, SupportedKeyPairAlgorithms } from "./src/cryptokeys.ts";
import type { JOSEHeader } from "./mod.ts";
test("signJWT() and validateJWT() with HMAC algorithms", async () => {
for (const algorithm of ["HS256", "HS384", "HS512"]) {
const secret =
`my_strong_secretmy_strong_secretmy_strong_secretmy_strong_secretmy_strong_secretmy_strong_${algorithm}`;
const key = await generateKey(secret, algorithm as SupportedKeyAlgorithms);
const payload = { foo: `bar_${algorithm}` };
const jwtString = await signJWT(payload, key, { algorithm });
const decodedPayload = await validateJWT(jwtString, key, { algorithm });
assertEquals(decodedPayload.foo, payload.foo);
}
});
test("signJWT() and validateJWT() with RSA algorithms", async () => {
for (const algorithm of ["RS256", "RS384", "RS512"]) {
const { privateKey, publicKey } = await generateKeyPair(algorithm as SupportedKeyPairAlgorithms);
const payload = { foo: `bar_${algorithm}` };
const jwtString = await signJWT(payload, privateKey, { algorithm });
const decodedPayload = await validateJWT(jwtString, publicKey, { algorithm });
assertEquals(decodedPayload.foo, payload.foo);
}
});
test("signJWT() and validateJWT() with ECDSA algorithms", async () => {
for (const algorithm of ["ES256", "ES384"]) {
const { privateKey, publicKey } = await generateKeyPair(algorithm as SupportedKeyPairAlgorithms);
const payload = { foo: `bar_${algorithm}` };
const jwtString = await signJWT(payload, privateKey, { algorithm });
const decodedPayload = await validateJWT(jwtString, publicKey, { algorithm });
assertEquals(decodedPayload.foo, payload.foo);
}
});
test("signJWT() and validateJWT() with RSA-PPS algorithms", async () => {
for (const algorithm of ["PS256", "PS384", "PS512"]) {
const { privateKey, publicKey } = await generateKeyPair(algorithm as SupportedKeyPairAlgorithms);
const payload = { foo: `bar_${algorithm}` };
const jwtString = await signJWT(payload, privateKey, { algorithm });
const decodedPayload = await validateJWT(jwtString, publicKey, { algorithm });
assertEquals(decodedPayload.foo, payload.foo);
}
});
test("validateJWT() throws JWTFormatError on invalid jwt structure", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar" };
let jwtString = await signJWT(payload, secret);
// Add extra period
jwtString += ".extraPart";
await assertRejects(() => validateJWT(jwtString, secret), JWTFormatError);
});
test("validateJWT() throws JWTValidationError on incorrect key", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const jwtString = await signJWT({ foo: "bar" }, secret);
await assertRejects(
() => validateJWT(jwtString, "incorrect_keyincorrect_keyincorrect_keyincorrect_key"),
JWTValidationError,
);
});
test("validateJWT() throws JWTFormatError on invalid Base64URL", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar" };
let jwtString = await signJWT(payload, secret);
const parts = jwtString.split(".");
// Tamper with the header
const tamperedHeader = parts[0] + "A";
jwtString = [tamperedHeader, parts[1], parts[2]].join(".");
await assertRejects(() => validateJWT(jwtString, secret), JWTFormatError);
});
test("validateJWT() throws JWTValidationError on tampered payload ", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar" };
let jwtString = await signJWT(payload, secret);
const parts = jwtString.split(".");
// Tamper with the payload
const tamperedPayload = parts[1].slice(0, -2);
jwtString = [parts[0], tamperedPayload, parts[2]].join(".");
await assertRejects(() => validateJWT(jwtString, secret), JWTValidationError);
});
// Tests for JWTAmbiguousClaimError
test("signJWT() throws JWTAmbiguousClaimError with 'expiresIn' and 'exp'", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar", exp: 1234567890 }; // Explicit exp
await assertRejects(
() => signJWT(payload, secret, { expiresIn: "1h" }), // Also using expiresIn
JWTAmbiguousClaimError,
);
});
test("signJWT() throws JWTAmbiguousClaimError with 'notBefore' and 'nbf'", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar", nbf: 1234567890 }; // Explicit nbf
await assertRejects(
() => signJWT(payload, secret, { notBefore: "5m" }), // Also using notBefore
JWTAmbiguousClaimError,
);
});
test("signJWT() works with 'expiresIn' only", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar" };
const jwt = await signJWT(payload, secret, { expiresIn: "1h" });
const decoded = await validateJWT(jwt, secret);
assertEquals(typeof decoded.exp, "number");
});
test("signJWT() works with 'notBefore' only", async () => {
const secret = "mySuperSecretAtLeast32CharsLong!";
const payload = { foo: "bar" };
const jwt = await signJWT(payload, secret, { notBefore: "5m" });
const decoded = await validateJWT(jwt, secret);
assertEquals(typeof decoded.nbf, "number");
});
test("signJWT() supports additional header claims", async () => {
const algorithm: SupportedKeyPairAlgorithms = "RS256";
const { privateKey, publicKey } = await generateKeyPair(algorithm);
const payload = { foo: "bar", baz: 42 };
const jwtString = await signJWT(payload, privateKey, {
algorithm: algorithm,
additionalHeaderClaims: { typ: "JOSE", kid: "abc123", someOther: [1, 2, 3] },
});
const unsafeHeader = unsafeParseJOSEHeader(jwtString);
const unsafePayload = unsafeParseJWT(jwtString);
const decodedPayload = await validateJWT(jwtString, publicKey, { algorithm });
assertEquals(unsafePayload, payload);
assertEquals(decodedPayload, payload);
const expectedHeader: JOSEHeader = { alg: algorithm, typ: "JOSE", kid: "abc123", someOther: [1, 2, 3] };
assertEquals(unsafeHeader, expectedHeader);
});
test("validateJWT() ignores the `alg` header claim", async () => {
const algorithm: SupportedKeyPairAlgorithms = "RS256";
const { privateKey, publicKey } = await generateKeyPair(algorithm);
const payload = { foo: "bar", baz: 42 };
const jwtString = await signJWT(payload, privateKey, {
algorithm: algorithm,
additionalHeaderClaims: { alg: "none" }, // Note the algorithm mismatch
});
// Should use the algorithm present in the public key, not the algorithm claimed by the JWT.
const decodedPayload = await validateJWT(jwtString, publicKey);
assertEquals(decodedPayload, payload);
});
test("validateJWT() throws JWTValidationError on stripped token", async () => {
const algorithm: SupportedKeyPairAlgorithms = "RS256";
const keyPair = await generateKeyPair(algorithm);
const payload = { foo: "bar", baz: 42 };
// Simulates the scenario where the attacker tampered with the body and tries to pass an
// unsigned token as the real item.
const jwtString = await signJWT(payload, false);
await assertRejects(
() => validateJWT(jwtString, keyPair.publicKey),
JWTValidationError,
);
});