Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

dpop.test.ts

288 lines8.4 KB

1import * as NodeCrypto from "node:crypto";
2​
3import { assert, describe, it } from "@effect/vitest";
4​
5import {
6 computeDpopAccessTokenHash,
7 computeDpopJwkThumbprint,
8 normalizeDpopHtu,
9 type DpopPublicJwk,
10 verifyDpopProof,
11} from "./dpop.ts";
12​
13function signDpopProof(input: {
14 readonly method: string;
15 readonly url: string;
16 readonly iat: number;
17 readonly privateKey: NodeCrypto.KeyObject;
18 readonly publicJwk: DpopPublicJwk | (DpopPublicJwk & { readonly d: string });
19 readonly accessToken?: string;
20}) {
21 const header = Buffer.from(
22 JSON.stringify({
23 typ: "dpop+jwt",
24 alg: "ES256",
25 jwk: input.publicJwk,
26 }),
27 ).toString("base64url");
28 const payload = Buffer.from(
29 JSON.stringify({
30 htm: input.method,
31 htu: input.url,
32 jti: "proof-1",
33 iat: input.iat,
34 ...(input.accessToken ? { ath: computeDpopAccessTokenHash(input.accessToken) } : {}),
35 }),
36 ).toString("base64url");
37 const signature = NodeCrypto.sign("sha256", Buffer.from(`${header}.${payload}`), {
38 key: input.privateKey,
39 dsaEncoding: "ieee-p1363",
40 }).toString("base64url");
41 return `${header}.${payload}.${signature}`;
43​
44describe("verifyDpopProof", () => {
45 const { privateKey, publicKey } = NodeCrypto.generateKeyPairSync("ec", {
46 namedCurve: "P-256",
47 });
48 const publicJwk = publicKey.export({ format: "jwk" }) as DpopPublicJwk;
49 const proof = signDpopProof({
50 method: "POST",
51 url: "https://example.com/oauth/token",
52 iat: 100,
53 privateKey,
54 publicJwk,
55 });
56​
57 it("verifies an ES256 DPoP proof and returns the RFC 7638 thumbprint", () => {
58 const thumbprint = computeDpopJwkThumbprint(publicJwk);
59 const result = verifyDpopProof({
60 proof,
61 method: "POST",
62 url: "https://example.com/oauth/token",
63 nowEpochSeconds: 101,
64 expectedThumbprint: thumbprint,
65 });
66​
67 if (!result.ok) {
68 assert.fail(result.reason);
69 }
70 assert.equal(result.thumbprint, thumbprint);
71 assert.equal(result.jti, "proof-1");
72 });
73​
74 it("rejects malformed DPoP header and payload JSON", () => {
75 const [header, payload, signature] = proof.split(".");
76 if (!header || !payload || !signature) {
77 assert.fail("Expected the test DPoP proof to use compact JWT format.");
78 }
79 const malformedJson = Buffer.from("{").toString("base64url");
80​
81 const malformedHeader = verifyDpopProof({
82 proof: `${malformedJson}.${payload}.${signature}`,
83 method: "POST",
84 url: "https://example.com/oauth/token",
85 nowEpochSeconds: 101,
86 });
87 if (malformedHeader.ok) {
88 assert.fail("Expected malformed DPoP header JSON to fail.");
89 }
90 assert.equal(malformedHeader.reason, "Invalid DPoP JWT header.");
91​
92 const malformedPayload = verifyDpopProof({
93 proof: `${header}.${malformedJson}.${signature}`,
94 method: "POST",
95 url: "https://example.com/oauth/token",
96 nowEpochSeconds: 101,
97 });
98 if (malformedPayload.ok) {
99 assert.fail("Expected malformed DPoP payload JSON to fail.");
100 }
101 assert.equal(malformedPayload.reason, "Invalid DPoP JWT payload.");
102 });
103​
104 it("rejects method, URL, thumbprint, and time-window mismatches", () => {
105 const thumbprint = computeDpopJwkThumbprint(publicJwk);
106 assert.equal(
107 verifyDpopProof({
108 proof,
109 method: "GET",
110 url: "https://example.com/oauth/token",
111 nowEpochSeconds: 101,
112 expectedThumbprint: thumbprint,
113 }).ok,
114 false,
115 );
116 assert.equal(
117 verifyDpopProof({
118 proof,
119 method: "POST",
120 url: "https://example.com/other",
121 nowEpochSeconds: 101,
122 expectedThumbprint: thumbprint,
123 }).ok,
124 false,
125 );
126 assert.equal(
127 verifyDpopProof({
128 proof,
129 method: "POST",
130 url: "https://example.com/oauth/token",
131 nowEpochSeconds: 101,
132 expectedThumbprint: "other-thumbprint",
133 }).ok,
134 false,
135 );
136 assert.equal(
137 verifyDpopProof({
138 proof,
139 method: "POST",
140 url: "https://example.com/oauth/token",
141 nowEpochSeconds: 1_000,
142 expectedThumbprint: thumbprint,
143 }).ok,
144 false,
145 );
146 });
147​
148 it("reports a time-window failure only after the proof signature is valid", () => {
149 const thumbprint = computeDpopJwkThumbprint(publicJwk);
150 const outsideWindow = verifyDpopProof({
151 proof,
152 method: "POST",
153 url: "https://example.com/oauth/token",
154 nowEpochSeconds: 1_000,
155 expectedThumbprint: thumbprint,
156 });
157 if (outsideWindow.ok) {
158 assert.fail("Expected an old DPoP proof to fail.");
159 }
160 assert.equal(outsideWindow.code, "time_window");
161​
162 const { privateKey: otherPrivateKey } = NodeCrypto.generateKeyPairSync("ec", {
163 namedCurve: "P-256",
164 });
165 const invalidSignatureProof = signDpopProof({
166 method: "POST",
167 url: "https://example.com/oauth/token",
168 iat: 100,
169 privateKey: otherPrivateKey,
170 publicJwk,
171 });
172 const invalidSignature = verifyDpopProof({
173 proof: invalidSignatureProof,
174 method: "POST",
175 url: "https://example.com/oauth/token",
176 nowEpochSeconds: 1_000,
177 expectedThumbprint: thumbprint,
178 });
179 if (invalidSignature.ok) {
180 assert.fail("Expected a proof signed by a different key to fail.");
181 }
182 assert.equal(invalidSignature.code, "invalid_signature");
183 });
184​
185 it("requires the RFC 9449 access token hash when an access token is expected", () => {
186 const thumbprint = computeDpopJwkThumbprint(publicJwk);
187 const accessTokenProof = signDpopProof({
188 method: "POST",
189 url: "https://example.com/v1/environments/env/connect",
190 iat: 100,
191 privateKey,
192 publicJwk,
193 accessToken: "clerk-access-token",
194 });
195​
196 assert.equal(
197 verifyDpopProof({
198 proof: accessTokenProof,
199 method: "POST",
200 url: "https://example.com/v1/environments/env/connect",
201 nowEpochSeconds: 101,
202 expectedThumbprint: thumbprint,
203 expectedAccessToken: "clerk-access-token",
204 }).ok,
205 true,
206 );
207​
208 const missingHash = verifyDpopProof({
209 proof,
210 method: "POST",
211 url: "https://example.com/oauth/token",
212 nowEpochSeconds: 101,
213 expectedThumbprint: thumbprint,
214 expectedAccessToken: "clerk-access-token",
215 });
216 if (missingHash.ok) {
217 assert.fail("Expected DPoP proof without an access token hash to fail.");
218 }
219 assert.equal(missingHash.reason, "DPoP access token hash mismatch.");
220​
221 const mismatchedHash = verifyDpopProof({
222 proof: accessTokenProof,
223 method: "POST",
224 url: "https://example.com/v1/environments/env/connect",
225 nowEpochSeconds: 101,
226 expectedThumbprint: thumbprint,
227 expectedAccessToken: "other-access-token",
228 });
229 if (mismatchedHash.ok) {
230 assert.fail("Expected DPoP proof with a mismatched access token hash to fail.");
231 }
232 assert.equal(mismatchedHash.reason, "DPoP access token hash mismatch.");
233 });
234​
235 it("normalizes htu by excluding query and fragment components per RFC 9449", () => {
236 assert.equal(
237 normalizeDpopHtu("https://example.com/v1/environments/env/connect?foo=bar#frag"),
238 "https://example.com/v1/environments/env/connect",
239 );
240​
241 const thumbprint = computeDpopJwkThumbprint(publicJwk);
242 const queryProof = signDpopProof({
243 method: "POST",
244 url: "https://example.com/v1/environments/env/connect",
245 iat: 100,
246 privateKey,
247 publicJwk,
248 });
249​
250 assert.equal(
251 verifyDpopProof({
252 proof: queryProof,
253 method: "POST",
254 url: "https://example.com/v1/environments/env/connect?foo=bar#frag",
255 nowEpochSeconds: 101,
256 expectedThumbprint: thumbprint,
257 }).ok,
258 true,
259 );
260 });
261​
262 it("rejects DPoP public JWK headers that expose private key material", () => {
263 const thumbprint = computeDpopJwkThumbprint(publicJwk);
264 const privateJwk = privateKey.export({ format: "jwk" }) as DpopPublicJwk & {
265 readonly d: string;
266 };
267 const proofWithPrivateJwk = signDpopProof({
268 method: "POST",
269 url: "https://example.com/oauth/token",
270 iat: 100,
271 privateKey,
272 publicJwk: privateJwk,
273 });
274​
275 const result = verifyDpopProof({
276 proof: proofWithPrivateJwk,
277 method: "POST",
278 url: "https://example.com/oauth/token",
279 nowEpochSeconds: 101,
280 expectedThumbprint: thumbprint,
281 });
282​
283 if (result.ok) {
284 assert.fail("Expected DPoP proof with private JWK material to fail.");
285 }
286 assert.equal(result.reason, "Invalid DPoP JWT header.");
287 });
288});