Open History to see the last commit
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}`;
42}
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("requires the RFC 9449 access token hash when an access token is expected", () => {
149 const thumbprint = computeDpopJwkThumbprint(publicJwk);
150 const accessTokenProof = signDpopProof({
151 method: "POST",
152 url: "https://example.com/v1/environments/env/connect",
153 iat: 100,
154 privateKey,
155 publicJwk,
156 accessToken: "clerk-access-token",
157 });
158
159 assert.equal(
160 verifyDpopProof({
161 proof: accessTokenProof,
162 method: "POST",
163 url: "https://example.com/v1/environments/env/connect",
164 nowEpochSeconds: 101,
165 expectedThumbprint: thumbprint,
166 expectedAccessToken: "clerk-access-token",
167 }).ok,
168 true,
169 );
170
171 const missingHash = verifyDpopProof({
172 proof,
173 method: "POST",
174 url: "https://example.com/oauth/token",
175 nowEpochSeconds: 101,
176 expectedThumbprint: thumbprint,
177 expectedAccessToken: "clerk-access-token",
178 });
179 if (missingHash.ok) {
180 assert.fail("Expected DPoP proof without an access token hash to fail.");
181 }
182 assert.equal(missingHash.reason, "DPoP access token hash mismatch.");
183
184 const mismatchedHash = verifyDpopProof({
185 proof: accessTokenProof,
186 method: "POST",
187 url: "https://example.com/v1/environments/env/connect",
188 nowEpochSeconds: 101,
189 expectedThumbprint: thumbprint,
190 expectedAccessToken: "other-access-token",
191 });
192 if (mismatchedHash.ok) {
193 assert.fail("Expected DPoP proof with a mismatched access token hash to fail.");
194 }
195 assert.equal(mismatchedHash.reason, "DPoP access token hash mismatch.");
196 });
197
198 it("normalizes htu by excluding query and fragment components per RFC 9449", () => {
199 assert.equal(
200 normalizeDpopHtu("https://example.com/v1/environments/env/connect?foo=bar#frag"),
201 "https://example.com/v1/environments/env/connect",
202 );
203
204 const thumbprint = computeDpopJwkThumbprint(publicJwk);
205 const queryProof = signDpopProof({
206 method: "POST",
207 url: "https://example.com/v1/environments/env/connect",
208 iat: 100,
209 privateKey,
210 publicJwk,
211 });
212
213 assert.equal(
214 verifyDpopProof({
215 proof: queryProof,
216 method: "POST",
217 url: "https://example.com/v1/environments/env/connect?foo=bar#frag",
218 nowEpochSeconds: 101,
219 expectedThumbprint: thumbprint,
220 }).ok,
221 true,
222 );
223 });
224
225 it("rejects DPoP public JWK headers that expose private key material", () => {
226 const thumbprint = computeDpopJwkThumbprint(publicJwk);
227 const privateJwk = privateKey.export({ format: "jwk" }) as DpopPublicJwk & {
228 readonly d: string;
229 };
230 const proofWithPrivateJwk = signDpopProof({
231 method: "POST",
232 url: "https://example.com/oauth/token",
233 iat: 100,
234 privateKey,
235 publicJwk: privateJwk,
236 });
237
238 const result = verifyDpopProof({
239 proof: proofWithPrivateJwk,
240 method: "POST",
241 url: "https://example.com/oauth/token",
242 nowEpochSeconds: 101,
243 expectedThumbprint: thumbprint,
244 });
245
246 if (result.ok) {
247 assert.fail("Expected DPoP proof with private JWK material to fail.");
248 }
249 assert.equal(result.reason, "Invalid DPoP JWT header.");
250 });
251});