@sd-jwt/core 0.1.0 → 0.1.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +201 -0
  3. package/README.md +106 -0
  4. package/dist/index.d.mts +659 -0
  5. package/dist/index.d.ts +659 -0
  6. package/dist/index.js +1906 -0
  7. package/dist/index.mjs +1846 -0
  8. package/package.json +59 -47
  9. package/src/decode/decode.ts +376 -0
  10. package/src/decode/index.ts +1 -0
  11. package/src/decoy.ts +15 -0
  12. package/src/flattenJSON.ts +99 -0
  13. package/src/generalJSON.ts +148 -0
  14. package/src/index.ts +945 -0
  15. package/src/jwt.ts +190 -0
  16. package/src/kbjwt.ts +81 -0
  17. package/src/present/index.ts +1 -0
  18. package/src/present/present.ts +210 -0
  19. package/src/sdjwt.ts +354 -0
  20. package/src/test/decode/decode.spec.ts +202 -0
  21. package/src/test/decoy.spec.ts +30 -0
  22. package/src/test/flattenJSON.spec.ts +122 -0
  23. package/src/test/generalJSON.spec.ts +198 -0
  24. package/src/test/index.spec.ts +974 -0
  25. package/src/test/jwt.spec.ts +301 -0
  26. package/src/test/kbjwt.spec.ts +565 -0
  27. package/src/test/pass.spec.ts +7 -0
  28. package/src/test/present/present.spec.ts +305 -0
  29. package/src/test/sdjwt.spec.ts +384 -0
  30. package/src/test/types/type.spec.ts +88 -0
  31. package/src/test/utils/base64url.spec.ts +33 -0
  32. package/src/test/utils/disclosure.spec.ts +170 -0
  33. package/src/test/utils/error.spec.ts +15 -0
  34. package/src/types/index.ts +2 -0
  35. package/src/types/type.ts +249 -0
  36. package/src/types/verification-error.ts +55 -0
  37. package/src/utils/base64url.ts +6 -0
  38. package/src/utils/disclosure.ts +101 -0
  39. package/src/utils/error.ts +25 -0
  40. package/src/utils/index.ts +3 -0
  41. package/src/utils/strict-json.ts +17 -0
  42. package/test/app-e2e.spec.ts +292 -0
  43. package/test/array_data_types.json +31 -0
  44. package/test/array_full_sd.json +21 -0
  45. package/test/array_in_sd.json +13 -0
  46. package/test/array_nested_in_plain.json +29 -0
  47. package/test/array_none_disclosed.json +17 -0
  48. package/test/array_of_nulls.json +15 -0
  49. package/test/array_of_objects.json +65 -0
  50. package/test/array_of_scalars.json +19 -0
  51. package/test/array_recursive_sd.json +35 -0
  52. package/test/array_recursive_sd_some_disclosed.json +63 -0
  53. package/test/complex.json +43 -0
  54. package/test/header_mod.json +48 -0
  55. package/test/json_serialization.json +48 -0
  56. package/test/key_binding.json +48 -0
  57. package/test/no_sd.json +36 -0
  58. package/test/object_data_types.json +62 -0
  59. package/test/recursions.json +117 -0
  60. package/test/rfc9901-validation.spec.ts +150 -0
  61. package/tsconfig.json +7 -0
  62. package/vitest.config.mts +4 -0
  63. package/build/base64url.d.ts +0 -28
  64. package/build/base64url.js +0 -40
  65. package/build/base64url.js.map +0 -1
  66. package/build/hasherAlgorithm.d.ts +0 -70
  67. package/build/hasherAlgorithm.js +0 -75
  68. package/build/hasherAlgorithm.js.map +0 -1
  69. package/build/index.d.ts +0 -11
  70. package/build/index.js +0 -20
  71. package/build/index.js.map +0 -1
  72. package/build/jwt/compact.d.ts +0 -6
  73. package/build/jwt/compact.js +0 -27
  74. package/build/jwt/compact.js.map +0 -1
  75. package/build/jwt/error.d.ts +0 -2
  76. package/build/jwt/error.js +0 -7
  77. package/build/jwt/error.js.map +0 -1
  78. package/build/jwt/index.d.ts +0 -3
  79. package/build/jwt/index.js +0 -20
  80. package/build/jwt/index.js.map +0 -1
  81. package/build/jwt/jwt.d.ts +0 -204
  82. package/build/jwt/jwt.js +0 -324
  83. package/build/jwt/jwt.js.map +0 -1
  84. package/build/keyBinding/index.d.ts +0 -1
  85. package/build/keyBinding/index.js +0 -18
  86. package/build/keyBinding/index.js.map +0 -1
  87. package/build/keyBinding/keyBinding.d.ts +0 -56
  88. package/build/keyBinding/keyBinding.js +0 -99
  89. package/build/keyBinding/keyBinding.js.map +0 -1
  90. package/build/sdJwt/compact.d.ts +0 -8
  91. package/build/sdJwt/compact.js +0 -39
  92. package/build/sdJwt/compact.js.map +0 -1
  93. package/build/sdJwt/decoys.d.ts +0 -2
  94. package/build/sdJwt/decoys.js +0 -35
  95. package/build/sdJwt/decoys.js.map +0 -1
  96. package/build/sdJwt/disclosureFrame.d.ts +0 -7
  97. package/build/sdJwt/disclosureFrame.js +0 -89
  98. package/build/sdJwt/disclosureFrame.js.map +0 -1
  99. package/build/sdJwt/disclosureMapping.d.ts +0 -44
  100. package/build/sdJwt/disclosureMapping.js +0 -289
  101. package/build/sdJwt/disclosureMapping.js.map +0 -1
  102. package/build/sdJwt/disclosures.d.ts +0 -14
  103. package/build/sdJwt/disclosures.js +0 -59
  104. package/build/sdJwt/disclosures.js.map +0 -1
  105. package/build/sdJwt/error.d.ts +0 -2
  106. package/build/sdJwt/error.js +0 -7
  107. package/build/sdJwt/error.js.map +0 -1
  108. package/build/sdJwt/index.d.ts +0 -7
  109. package/build/sdJwt/index.js +0 -24
  110. package/build/sdJwt/index.js.map +0 -1
  111. package/build/sdJwt/presentationFrame.d.ts +0 -4
  112. package/build/sdJwt/presentationFrame.js +0 -73
  113. package/build/sdJwt/presentationFrame.js.map +0 -1
  114. package/build/sdJwt/sdJwt.d.ts +0 -204
  115. package/build/sdJwt/sdJwt.js +0 -383
  116. package/build/sdJwt/sdJwt.js.map +0 -1
  117. package/build/sdJwt/swapClaim.d.ts +0 -3
  118. package/build/sdJwt/swapClaim.js +0 -90
  119. package/build/sdJwt/swapClaim.js.map +0 -1
  120. package/build/sdJwt/types.d.ts +0 -5
  121. package/build/sdJwt/types.js +0 -3
  122. package/build/sdJwt/types.js.map +0 -1
  123. package/build/sdJwtVc/error.d.ts +0 -2
  124. package/build/sdJwtVc/error.js +0 -7
  125. package/build/sdJwtVc/error.js.map +0 -1
  126. package/build/sdJwtVc/index.d.ts +0 -2
  127. package/build/sdJwtVc/index.js +0 -19
  128. package/build/sdJwtVc/index.js.map +0 -1
  129. package/build/sdJwtVc/sdJwtVc.d.ts +0 -47
  130. package/build/sdJwtVc/sdJwtVc.js +0 -125
  131. package/build/sdJwtVc/sdJwtVc.js.map +0 -1
  132. package/build/signatureAndEncryptionAlgorithm.d.ts +0 -105
  133. package/build/signatureAndEncryptionAlgorithm.js +0 -110
  134. package/build/signatureAndEncryptionAlgorithm.js.map +0 -1
  135. package/build/types/disclosure.d.ts +0 -5
  136. package/build/types/disclosure.js +0 -3
  137. package/build/types/disclosure.js.map +0 -1
  138. package/build/types/frame.d.ts +0 -5
  139. package/build/types/frame.js +0 -3
  140. package/build/types/frame.js.map +0 -1
  141. package/build/types/hasher.d.ts +0 -14
  142. package/build/types/hasher.js +0 -3
  143. package/build/types/hasher.js.map +0 -1
  144. package/build/types/index.d.ts +0 -6
  145. package/build/types/index.js +0 -23
  146. package/build/types/index.js.map +0 -1
  147. package/build/types/present.d.ts +0 -2
  148. package/build/types/present.js +0 -3
  149. package/build/types/present.js.map +0 -1
  150. package/build/types/saltGenerator.d.ts +0 -17
  151. package/build/types/saltGenerator.js +0 -3
  152. package/build/types/saltGenerator.js.map +0 -1
  153. package/build/types/signer.d.ts +0 -2
  154. package/build/types/signer.js +0 -3
  155. package/build/types/signer.js.map +0 -1
  156. package/build/types/utils.d.ts +0 -5
  157. package/build/types/utils.js +0 -3
  158. package/build/types/utils.js.map +0 -1
  159. package/build/types/verifier.d.ts +0 -14
  160. package/build/types/verifier.js +0 -3
  161. package/build/types/verifier.js.map +0 -1
  162. package/build/utils/index.d.ts +0 -2
  163. package/build/utils/index.js +0 -19
  164. package/build/utils/index.js.map +0 -1
  165. package/build/utils/traverse.d.ts +0 -8
  166. package/build/utils/traverse.js +0 -29
  167. package/build/utils/traverse.js.map +0 -1
  168. package/build/utils/utils.d.ts +0 -7
  169. package/build/utils/utils.js +0 -109
  170. package/build/utils/utils.js.map +0 -1
@@ -0,0 +1,565 @@
1
+ import Crypto, { type KeyLike } from 'node:crypto';
2
+ import { exportJWK, importJWK, type JWK } from 'jose';
3
+ import { describe, expect, test } from 'vitest';
4
+ import { KBJwt } from '../kbjwt';
5
+ import {
6
+ type JwtPayload,
7
+ KB_JWT_TYP,
8
+ type KbVerifier,
9
+ type kbPayload,
10
+ type Signer,
11
+ } from '../types';
12
+ import type { SDJWTException } from '../utils';
13
+
14
+ describe('KB JWT', () => {
15
+ test('create', async () => {
16
+ const kbJwt = new KBJwt({
17
+ header: {
18
+ typ: KB_JWT_TYP,
19
+ alg: 'EdDSA',
20
+ },
21
+ payload: {
22
+ iat: 1,
23
+ aud: 'aud',
24
+ nonce: 'nonce',
25
+ sd_hash: 'hash',
26
+ },
27
+ });
28
+
29
+ expect(kbJwt.header).toEqual({
30
+ typ: KB_JWT_TYP,
31
+ alg: 'EdDSA',
32
+ });
33
+ expect(kbJwt.payload).toEqual({
34
+ iat: 1,
35
+ aud: 'aud',
36
+ nonce: 'nonce',
37
+ sd_hash: 'hash',
38
+ });
39
+ });
40
+
41
+ test('decode', async () => {
42
+ const { privateKey } = Crypto.generateKeyPairSync('ed25519');
43
+ const testSigner: Signer = async (data: string) => {
44
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
45
+ return Buffer.from(sig).toString('base64url');
46
+ };
47
+ const kbJwt = new KBJwt({
48
+ header: {
49
+ typ: KB_JWT_TYP,
50
+ alg: 'EdDSA',
51
+ },
52
+ payload: {
53
+ iat: 1,
54
+ aud: 'aud',
55
+ nonce: 'nonce',
56
+ sd_hash: 'hash',
57
+ },
58
+ });
59
+ const encodedKbJwt = await kbJwt.sign(testSigner);
60
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
61
+ expect(decoded.header).toEqual({
62
+ typ: KB_JWT_TYP,
63
+ alg: 'EdDSA',
64
+ });
65
+ expect(decoded.payload).toEqual({
66
+ iat: 1,
67
+ aud: 'aud',
68
+ nonce: 'nonce',
69
+ sd_hash: 'hash',
70
+ });
71
+ });
72
+
73
+ test('verify', async () => {
74
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
75
+ const testSigner: Signer = async (data: string) => {
76
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
77
+ return Buffer.from(sig).toString('base64url');
78
+ };
79
+
80
+ const payload = {
81
+ cnf: {
82
+ jwk: await exportJWK(publicKey),
83
+ },
84
+ };
85
+
86
+ const testVerifier: KbVerifier = async (
87
+ data: string,
88
+ sig: string,
89
+ payload: JwtPayload,
90
+ ) => {
91
+ expect(payload).toStrictEqual(payload);
92
+ expect(payload.cnf?.jwk).toBeDefined();
93
+
94
+ const publicKey = payload.cnf?.jwk;
95
+
96
+ return Crypto.verify(
97
+ null,
98
+ Buffer.from(data),
99
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
100
+ Buffer.from(sig, 'base64url'),
101
+ );
102
+ };
103
+ const kbJwt = new KBJwt({
104
+ header: {
105
+ typ: KB_JWT_TYP,
106
+ alg: 'EdDSA',
107
+ },
108
+ payload: {
109
+ iat: 1,
110
+ aud: 'aud',
111
+ nonce: 'nonce',
112
+ sd_hash: 'hash',
113
+ },
114
+ });
115
+ const encodedKbJwt = await kbJwt.sign(testSigner);
116
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
117
+ const verified = await decoded.verifyKB({
118
+ verifier: testVerifier,
119
+ payload,
120
+ nonce: 'nonce',
121
+ });
122
+ expect(verified).toStrictEqual({
123
+ header: {
124
+ typ: KB_JWT_TYP,
125
+ alg: 'EdDSA',
126
+ },
127
+ payload: {
128
+ iat: 1,
129
+ aud: 'aud',
130
+ nonce: 'nonce',
131
+ sd_hash: 'hash',
132
+ },
133
+ });
134
+ });
135
+
136
+ test('verify failed', async () => {
137
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
138
+ const testSigner: Signer = async (data: string) => {
139
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
140
+ return Buffer.from(sig).toString('base64url');
141
+ };
142
+
143
+ const payload = {
144
+ cnf: {
145
+ jwk: await exportJWK(publicKey),
146
+ },
147
+ };
148
+ const testVerifier: KbVerifier = async (
149
+ data: string,
150
+ sig: string,
151
+ payload: JwtPayload,
152
+ ) => {
153
+ expect(payload).toStrictEqual(payload);
154
+ expect(payload.cnf?.jwk).toBeDefined();
155
+
156
+ const publicKey = payload.cnf?.jwk;
157
+
158
+ return Crypto.verify(
159
+ null,
160
+ Buffer.from(data),
161
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
162
+ Buffer.from(sig, 'base64url'),
163
+ );
164
+ };
165
+
166
+ const kbJwt = new KBJwt({
167
+ header: {
168
+ typ: KB_JWT_TYP,
169
+ alg: 'EdDSA',
170
+ },
171
+ payload: {
172
+ iat: 1,
173
+ aud: 'aud',
174
+ nonce: 'nonce',
175
+ sd_hash: '',
176
+ },
177
+ });
178
+ const encodedKbJwt = await kbJwt.sign(testSigner);
179
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
180
+ try {
181
+ await decoded.verifyKB({
182
+ verifier: testVerifier,
183
+ payload,
184
+ nonce: 'nonce',
185
+ });
186
+ } catch (e: unknown) {
187
+ const error = e as SDJWTException;
188
+ expect(error.message).toBe('Invalid Key Binding Jwt');
189
+ }
190
+ });
191
+
192
+ test('verify failed with verifier return false', async () => {
193
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
194
+ const testSigner: Signer = async (data: string) => {
195
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
196
+ return Buffer.from(sig).toString('base64url');
197
+ };
198
+
199
+ const payload = {
200
+ cnf: {
201
+ jwk: await exportJWK(publicKey),
202
+ },
203
+ };
204
+ const testVerifier: KbVerifier = async (
205
+ _data: string,
206
+ _sig: string,
207
+ payload: JwtPayload,
208
+ ) => {
209
+ expect(payload).toStrictEqual(payload);
210
+ expect(payload.cnf?.jwk).toBeDefined();
211
+
212
+ return false;
213
+ };
214
+
215
+ const kbJwt = new KBJwt({
216
+ header: {
217
+ typ: KB_JWT_TYP,
218
+ alg: 'EdDSA',
219
+ },
220
+ payload: {
221
+ iat: 1,
222
+ aud: 'aud',
223
+ nonce: 'nonce',
224
+ sd_hash: 'hash',
225
+ },
226
+ });
227
+ const encodedKbJwt = await kbJwt.sign(testSigner);
228
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
229
+ try {
230
+ await decoded.verifyKB({
231
+ verifier: testVerifier,
232
+ payload,
233
+ nonce: 'nonce',
234
+ });
235
+ } catch (e: unknown) {
236
+ const error = e as SDJWTException;
237
+ expect(error.message).toBe('Verify Error: Invalid JWT Signature');
238
+ }
239
+ });
240
+
241
+ test('verify failed with invalid jwt', async () => {
242
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
243
+ const testSigner: Signer = async (data: string) => {
244
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
245
+ return Buffer.from(sig).toString('base64url');
246
+ };
247
+
248
+ const payload = {
249
+ cnf: {
250
+ jwk: await exportJWK(publicKey),
251
+ },
252
+ };
253
+ const testVerifier: KbVerifier = async (
254
+ _data: string,
255
+ _sig: string,
256
+ payload: JwtPayload,
257
+ ) => {
258
+ expect(payload).toStrictEqual(payload);
259
+ expect(payload.cnf?.jwk).toBeDefined();
260
+
261
+ return false;
262
+ };
263
+
264
+ const kbJwt = new KBJwt({
265
+ header: {
266
+ typ: KB_JWT_TYP,
267
+ alg: 'EdDSA',
268
+ },
269
+ payload: {
270
+ iat: 1,
271
+ aud: 'aud',
272
+ nonce: 'nonce',
273
+ sd_hash: 'hash',
274
+ },
275
+ });
276
+ const encodedKbJwt = await kbJwt.sign(testSigner);
277
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
278
+ decoded.signature = undefined;
279
+ try {
280
+ await decoded.verifyKB({
281
+ verifier: testVerifier,
282
+ payload,
283
+ nonce: 'nonce',
284
+ });
285
+ } catch (e: unknown) {
286
+ const error = e as SDJWTException;
287
+ expect(error.message).toBe('Verify Error: Invalid JWT');
288
+ }
289
+ });
290
+
291
+ test('verify failed with expired exp claim', async () => {
292
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
293
+ const testSigner: Signer = async (data: string) => {
294
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
295
+ return Buffer.from(sig).toString('base64url');
296
+ };
297
+
298
+ const payload = {
299
+ cnf: {
300
+ jwk: await exportJWK(publicKey),
301
+ },
302
+ };
303
+ const testVerifier: KbVerifier = async (
304
+ data: string,
305
+ sig: string,
306
+ payload: JwtPayload,
307
+ ) => {
308
+ const publicKey = payload.cnf?.jwk;
309
+ return Crypto.verify(
310
+ null,
311
+ Buffer.from(data),
312
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
313
+ Buffer.from(sig, 'base64url'),
314
+ );
315
+ };
316
+
317
+ const kbJwt = new KBJwt({
318
+ header: {
319
+ typ: KB_JWT_TYP,
320
+ alg: 'EdDSA',
321
+ },
322
+ payload: {
323
+ iat: 1,
324
+ aud: 'aud',
325
+ nonce: 'nonce',
326
+ sd_hash: 'hash',
327
+ // expired one hour before the current date used below
328
+ exp: 1000,
329
+ } as kbPayload,
330
+ });
331
+ const encodedKbJwt = await kbJwt.sign(testSigner);
332
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
333
+
334
+ await expect(
335
+ decoded.verifyKB({
336
+ verifier: testVerifier,
337
+ payload,
338
+ nonce: 'nonce',
339
+ options: { currentDate: 5000 },
340
+ }),
341
+ ).rejects.toThrow('Verify Error: JWT is expired');
342
+ });
343
+
344
+ test('verify failed with iat in the future', async () => {
345
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
346
+ const testSigner: Signer = async (data: string) => {
347
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
348
+ return Buffer.from(sig).toString('base64url');
349
+ };
350
+
351
+ const payload = {
352
+ cnf: {
353
+ jwk: await exportJWK(publicKey),
354
+ },
355
+ };
356
+ const testVerifier: KbVerifier = async (
357
+ data: string,
358
+ sig: string,
359
+ payload: JwtPayload,
360
+ ) => {
361
+ const publicKey = payload.cnf?.jwk;
362
+ return Crypto.verify(
363
+ null,
364
+ Buffer.from(data),
365
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
366
+ Buffer.from(sig, 'base64url'),
367
+ );
368
+ };
369
+
370
+ const kbJwt = new KBJwt({
371
+ header: {
372
+ typ: KB_JWT_TYP,
373
+ alg: 'EdDSA',
374
+ },
375
+ payload: {
376
+ iat: 5000,
377
+ aud: 'aud',
378
+ nonce: 'nonce',
379
+ sd_hash: 'hash',
380
+ },
381
+ });
382
+ const encodedKbJwt = await kbJwt.sign(testSigner);
383
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
384
+
385
+ await expect(
386
+ decoded.verifyKB({
387
+ verifier: testVerifier,
388
+ payload,
389
+ nonce: 'nonce',
390
+ // iat (5000) is after the current date (1000)
391
+ options: { currentDate: 1000 },
392
+ }),
393
+ ).rejects.toThrow('Verify Error: JWT is not yet valid');
394
+ });
395
+
396
+ test('verify failed with nbf in the future', async () => {
397
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
398
+ const testSigner: Signer = async (data: string) => {
399
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
400
+ return Buffer.from(sig).toString('base64url');
401
+ };
402
+
403
+ const payload = {
404
+ cnf: {
405
+ jwk: await exportJWK(publicKey),
406
+ },
407
+ };
408
+ const testVerifier: KbVerifier = async (
409
+ data: string,
410
+ sig: string,
411
+ payload: JwtPayload,
412
+ ) => {
413
+ const publicKey = payload.cnf?.jwk;
414
+ return Crypto.verify(
415
+ null,
416
+ Buffer.from(data),
417
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
418
+ Buffer.from(sig, 'base64url'),
419
+ );
420
+ };
421
+
422
+ const kbJwt = new KBJwt({
423
+ header: {
424
+ typ: KB_JWT_TYP,
425
+ alg: 'EdDSA',
426
+ },
427
+ payload: {
428
+ iat: 1,
429
+ aud: 'aud',
430
+ nonce: 'nonce',
431
+ sd_hash: 'hash',
432
+ nbf: 5000,
433
+ } as kbPayload,
434
+ });
435
+ const encodedKbJwt = await kbJwt.sign(testSigner);
436
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
437
+
438
+ await expect(
439
+ decoded.verifyKB({
440
+ verifier: testVerifier,
441
+ payload,
442
+ nonce: 'nonce',
443
+ options: { currentDate: 1000 },
444
+ }),
445
+ ).rejects.toThrow('Verify Error: JWT is not yet valid');
446
+ });
447
+
448
+ test('verify succeeds for expired exp within the allowed skew', async () => {
449
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
450
+ const testSigner: Signer = async (data: string) => {
451
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
452
+ return Buffer.from(sig).toString('base64url');
453
+ };
454
+
455
+ const payload = {
456
+ cnf: {
457
+ jwk: await exportJWK(publicKey),
458
+ },
459
+ };
460
+ const testVerifier: KbVerifier = async (
461
+ data: string,
462
+ sig: string,
463
+ payload: JwtPayload,
464
+ ) => {
465
+ const publicKey = payload.cnf?.jwk;
466
+ return Crypto.verify(
467
+ null,
468
+ Buffer.from(data),
469
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
470
+ Buffer.from(sig, 'base64url'),
471
+ );
472
+ };
473
+
474
+ const kbJwt = new KBJwt({
475
+ header: {
476
+ typ: KB_JWT_TYP,
477
+ alg: 'EdDSA',
478
+ },
479
+ payload: {
480
+ iat: 1,
481
+ aud: 'aud',
482
+ nonce: 'nonce',
483
+ sd_hash: 'hash',
484
+ exp: 1000,
485
+ } as kbPayload,
486
+ });
487
+ const encodedKbJwt = await kbJwt.sign(testSigner);
488
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
489
+
490
+ // exp (1000) is before the current date (1100) but within the 200s skew
491
+ const verified = await decoded.verifyKB({
492
+ verifier: testVerifier,
493
+ payload,
494
+ nonce: 'nonce',
495
+ options: { currentDate: 1100, skewSeconds: 200 },
496
+ });
497
+
498
+ expect(verified.payload.exp).toBe(1000);
499
+ });
500
+
501
+ test('compatibility test for version 06', async () => {
502
+ const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
503
+ const testSigner: Signer = async (data: string) => {
504
+ const sig = Crypto.sign(null, Buffer.from(data), privateKey);
505
+ return Buffer.from(sig).toString('base64url');
506
+ };
507
+ const payload = {
508
+ cnf: {
509
+ jwk: await exportJWK(publicKey),
510
+ },
511
+ };
512
+ const testVerifier: KbVerifier = async (
513
+ data: string,
514
+ sig: string,
515
+ payload: JwtPayload,
516
+ ) => {
517
+ expect(payload).toStrictEqual(payload);
518
+ expect(payload.cnf?.jwk).toBeDefined();
519
+
520
+ const publicKey = payload.cnf?.jwk;
521
+
522
+ return Crypto.verify(
523
+ null,
524
+ Buffer.from(data),
525
+ (await importJWK(publicKey as JWK, 'EdDSA')) as KeyLike,
526
+ Buffer.from(sig, 'base64url'),
527
+ );
528
+ };
529
+ const kbJwt = new KBJwt({
530
+ header: {
531
+ typ: KB_JWT_TYP,
532
+ alg: 'EdDSA',
533
+ },
534
+ payload: {
535
+ iat: 1,
536
+ aud: 'aud',
537
+ nonce: 'nonce',
538
+ sd_hash: 'hash',
539
+ },
540
+ });
541
+
542
+ (kbJwt.payload as Record<string, unknown>)._sd_hash = 'hash';
543
+ (kbJwt.payload as Record<string, unknown>).sd_hash = undefined;
544
+
545
+ const encodedKbJwt = await kbJwt.sign(testSigner);
546
+ const decoded = KBJwt.fromKBEncode(encodedKbJwt);
547
+ const verified = await decoded.verifyKB({
548
+ verifier: testVerifier,
549
+ payload,
550
+ nonce: 'nonce',
551
+ });
552
+ expect(verified).toStrictEqual({
553
+ header: {
554
+ typ: KB_JWT_TYP,
555
+ alg: 'EdDSA',
556
+ },
557
+ payload: {
558
+ iat: 1,
559
+ aud: 'aud',
560
+ nonce: 'nonce',
561
+ _sd_hash: 'hash',
562
+ },
563
+ });
564
+ });
565
+ });
@@ -0,0 +1,7 @@
1
+ import { describe, expect, test } from 'vitest';
2
+
3
+ describe('Should always pass', () => {
4
+ test('adds 1 + 2 to equal 3', () => {
5
+ expect(1 + 2).toBe(3);
6
+ });
7
+ });