Open History to see the last commit
1import * as NodeCrypto from "node:crypto";
2
3import { describe, expect, 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 expect(
60 verifyDpopProof({
61 proof,
62 method: "POST",
63 url: "https://example.com/oauth/token",
64 nowEpochSeconds: 101,
65 expectedThumbprint: thumbprint,
66 }),
67 ).toMatchObject({
68 ok: true,
69 thumbprint,
70 jti: "proof-1",
71 });
72 });
73
74 it("rejects method, URL, thumbprint, and time-window mismatches", () => {
75 const thumbprint = computeDpopJwkThumbprint(publicJwk);
76 expect(
77 verifyDpopProof({
78 proof,
79 method: "GET",
80 url: "https://example.com/oauth/token",
81 nowEpochSeconds: 101,
82 expectedThumbprint: thumbprint,
83 }),
84 ).toMatchObject({ ok: false });
85 expect(
86 verifyDpopProof({
87 proof,
88 method: "POST",
89 url: "https://example.com/other",
90 nowEpochSeconds: 101,
91 expectedThumbprint: thumbprint,
92 }),
93 ).toMatchObject({ ok: false });
94 expect(
95 verifyDpopProof({
96 proof,
97 method: "POST",
98 url: "https://example.com/oauth/token",
99 nowEpochSeconds: 101,
100 expectedThumbprint: "other-thumbprint",
101 }),
102 ).toMatchObject({ ok: false });
103 expect(
104 verifyDpopProof({
105 proof,
106 method: "POST",
107 url: "https://example.com/oauth/token",
108 nowEpochSeconds: 1_000,
109 expectedThumbprint: thumbprint,
110 }),
111 ).toMatchObject({ ok: false });
112 });
113
114 it("requires the RFC 9449 access token hash when an access token is expected", () => {
115 const thumbprint = computeDpopJwkThumbprint(publicJwk);
116 const accessTokenProof = signDpopProof({
117 method: "POST",
118 url: "https://example.com/v1/environments/env/connect",
119 iat: 100,
120 privateKey,
121 publicJwk,
122 accessToken: "clerk-access-token",
123 });
124
125 expect(
126 verifyDpopProof({
127 proof: accessTokenProof,
128 method: "POST",
129 url: "https://example.com/v1/environments/env/connect",
130 nowEpochSeconds: 101,
131 expectedThumbprint: thumbprint,
132 expectedAccessToken: "clerk-access-token",
133 }),
134 ).toMatchObject({ ok: true });
135 expect(
136 verifyDpopProof({
137 proof,
138 method: "POST",
139 url: "https://example.com/oauth/token",
140 nowEpochSeconds: 101,
141 expectedThumbprint: thumbprint,
142 expectedAccessToken: "clerk-access-token",
143 }),
144 ).toMatchObject({ ok: false, reason: "DPoP access token hash mismatch." });
145 expect(
146 verifyDpopProof({
147 proof: accessTokenProof,
148 method: "POST",
149 url: "https://example.com/v1/environments/env/connect",
150 nowEpochSeconds: 101,
151 expectedThumbprint: thumbprint,
152 expectedAccessToken: "other-access-token",
153 }),
154 ).toMatchObject({ ok: false, reason: "DPoP access token hash mismatch." });
155 });
156
157 it("normalizes htu by excluding query and fragment components per RFC 9449", () => {
158 expect(normalizeDpopHtu("https://example.com/v1/environments/env/connect?foo=bar#frag")).toBe(
159 "https://example.com/v1/environments/env/connect",
160 );
161
162 const thumbprint = computeDpopJwkThumbprint(publicJwk);
163 const queryProof = signDpopProof({
164 method: "POST",
165 url: "https://example.com/v1/environments/env/connect",
166 iat: 100,
167 privateKey,
168 publicJwk,
169 });
170
171 expect(
172 verifyDpopProof({
173 proof: queryProof,
174 method: "POST",
175 url: "https://example.com/v1/environments/env/connect?foo=bar#frag",
176 nowEpochSeconds: 101,
177 expectedThumbprint: thumbprint,
178 }),
179 ).toMatchObject({ ok: true });
180 });
181
182 it("rejects DPoP public JWK headers that expose private key material", () => {
183 const thumbprint = computeDpopJwkThumbprint(publicJwk);
184 const privateJwk = privateKey.export({ format: "jwk" }) as DpopPublicJwk & {
185 readonly d: string;
186 };
187 const proofWithPrivateJwk = signDpopProof({
188 method: "POST",
189 url: "https://example.com/oauth/token",
190 iat: 100,
191 privateKey,
192 publicJwk: privateJwk,
193 });
194
195 expect(
196 verifyDpopProof({
197 proof: proofWithPrivateJwk,
198 method: "POST",
199 url: "https://example.com/oauth/token",
200 nowEpochSeconds: 101,
201 expectedThumbprint: thumbprint,
202 }),
203 ).toMatchObject({ ok: false, reason: "Invalid DPoP JWT header." });
204 });
205});