@sd-jwt/core 0.20.1 → 0.21.0-alpha-20260916100413

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 (68) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +82 -80
  3. package/dist/index.cjs +1526 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +696 -0
  6. package/dist/index.d.mts +537 -527
  7. package/dist/index.mjs +1422 -1998
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +29 -49
  10. package/CHANGELOG.md +0 -39
  11. package/dist/index.d.ts +0 -686
  12. package/dist/index.js +0 -2100
  13. package/src/decode/decode.ts +0 -407
  14. package/src/decode/index.ts +0 -1
  15. package/src/decoy.ts +0 -15
  16. package/src/flattenJSON.ts +0 -99
  17. package/src/generalJSON.ts +0 -157
  18. package/src/index.ts +0 -1000
  19. package/src/jwt.ts +0 -286
  20. package/src/kbjwt.ts +0 -101
  21. package/src/present/index.ts +0 -1
  22. package/src/present/present.ts +0 -216
  23. package/src/sdjwt.ts +0 -384
  24. package/src/test/decode/decode.spec.ts +0 -197
  25. package/src/test/decoy.spec.ts +0 -30
  26. package/src/test/flattenJSON.spec.ts +0 -122
  27. package/src/test/generalJSON.spec.ts +0 -198
  28. package/src/test/index.spec.ts +0 -986
  29. package/src/test/jwt.spec.ts +0 -301
  30. package/src/test/kbjwt.spec.ts +0 -555
  31. package/src/test/pass.spec.ts +0 -7
  32. package/src/test/present/present.spec.ts +0 -305
  33. package/src/test/sdjwt.spec.ts +0 -384
  34. package/src/test/types/type.spec.ts +0 -88
  35. package/src/test/utils/base64url.spec.ts +0 -33
  36. package/src/test/utils/disclosure.spec.ts +0 -170
  37. package/src/test/utils/error.spec.ts +0 -15
  38. package/src/types/index.ts +0 -2
  39. package/src/types/type.ts +0 -270
  40. package/src/types/verification-error.ts +0 -55
  41. package/src/utils/base64url.ts +0 -6
  42. package/src/utils/disclosure.ts +0 -121
  43. package/src/utils/error.ts +0 -25
  44. package/src/utils/index.ts +0 -3
  45. package/src/utils/strict-json.ts +0 -17
  46. package/test/app-e2e.spec.ts +0 -292
  47. package/test/array_data_types.json +0 -31
  48. package/test/array_full_sd.json +0 -21
  49. package/test/array_in_sd.json +0 -13
  50. package/test/array_nested_in_plain.json +0 -29
  51. package/test/array_none_disclosed.json +0 -17
  52. package/test/array_of_nulls.json +0 -15
  53. package/test/array_of_objects.json +0 -65
  54. package/test/array_of_scalars.json +0 -19
  55. package/test/array_recursive_sd.json +0 -35
  56. package/test/array_recursive_sd_some_disclosed.json +0 -63
  57. package/test/complex.json +0 -43
  58. package/test/header_mod.json +0 -48
  59. package/test/json_serialization.json +0 -48
  60. package/test/key_binding.json +0 -48
  61. package/test/no_sd.json +0 -36
  62. package/test/object_data_types.json +0 -62
  63. package/test/recursions.json +0 -117
  64. package/test/rfc9901-audit-fixes.spec.ts +0 -252
  65. package/test/rfc9901-validation.spec.ts +0 -150
  66. package/test/tsconfig.json +0 -4
  67. package/tsconfig.json +0 -7
  68. package/vitest.config.mts +0 -4
@@ -1,555 +0,0 @@
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('rejects draft-era _sd_hash without sd_hash', 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
- await expect(
548
- decoded.verifyKB({
549
- verifier: testVerifier,
550
- payload,
551
- nonce: 'nonce',
552
- }),
553
- ).rejects.toThrow('Invalid Key Binding Jwt');
554
- });
555
- });
@@ -1,7 +0,0 @@
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
- });