@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,301 +0,0 @@
1
- import Crypto from 'node:crypto';
2
- import { describe, expect, test } from 'vitest';
3
- import { Jwt } from '../jwt';
4
- import type { Signer, Verifier } from '../types';
5
- import { base64urlEncode, SDJWTException } from '../utils';
6
-
7
- describe('JWT', () => {
8
- test('create', async () => {
9
- const jwt = new Jwt({
10
- header: { alg: 'EdDSA' },
11
- payload: { foo: 'bar' },
12
- });
13
-
14
- expect(jwt.header).toEqual({ alg: 'EdDSA' });
15
- expect(jwt.payload).toEqual({ foo: 'bar' });
16
- });
17
-
18
- test('returns decoded JWT when correct JWT string is provided', () => {
19
- const jwt = `${base64urlEncode(
20
- JSON.stringify({ alg: 'HS256', typ: 'JWT' }),
21
- )}.${base64urlEncode(
22
- JSON.stringify({ sub: '1234567890', name: 'John Doe' }),
23
- )}.signature`;
24
- const result = Jwt.decodeJWT(jwt);
25
- expect(result).toEqual({
26
- header: { alg: 'HS256', typ: 'JWT' },
27
- payload: { sub: '1234567890', name: 'John Doe' },
28
- signature: 'signature',
29
- });
30
- });
31
-
32
- test('throws an error when JWT string is not correctly formed', () => {
33
- const jwt = 'abc.def';
34
- expect(() => Jwt.decodeJWT(jwt)).toThrow('Invalid JWT as input');
35
- });
36
-
37
- test('throws an error when JWT parts are missing', () => {
38
- const jwt = `${base64urlEncode(
39
- JSON.stringify({ alg: 'HS256', typ: 'JWT' }),
40
- )}`;
41
- expect(() => Jwt.decodeJWT(jwt)).toThrow('Invalid JWT as input');
42
- });
43
-
44
- test('set', async () => {
45
- const jwt = new Jwt();
46
- jwt.setHeader({ alg: 'EdDSA' });
47
- jwt.setPayload({ foo: 'bar' });
48
-
49
- expect(jwt.header).toEqual({ alg: 'EdDSA' });
50
- expect(jwt.payload).toEqual({ foo: 'bar' });
51
- });
52
-
53
- test('sign', async () => {
54
- const { privateKey } = Crypto.generateKeyPairSync('ed25519');
55
- const testSigner: Signer = async (data: string) => {
56
- const sig = Crypto.sign(null, Buffer.from(data), privateKey);
57
- return Buffer.from(sig).toString('base64url');
58
- };
59
- const jwt = new Jwt({
60
- header: { alg: 'EdDSA' },
61
- payload: { foo: 'bar' },
62
- });
63
-
64
- const encodedJwt = await jwt.sign(testSigner);
65
- expect(typeof encodedJwt).toBe('string');
66
- });
67
-
68
- test('verify', async () => {
69
- const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
70
- const testSigner: Signer = async (data: string) => {
71
- const sig = Crypto.sign(null, Buffer.from(data), privateKey);
72
- return Buffer.from(sig).toString('base64url');
73
- };
74
- const testVerifier: Verifier = async (data: string, sig: string) => {
75
- return Crypto.verify(
76
- null,
77
- Buffer.from(data),
78
- publicKey,
79
- Buffer.from(sig, 'base64url'),
80
- );
81
- };
82
-
83
- const jwt = new Jwt({
84
- header: { alg: 'EdDSA' },
85
- payload: { foo: 'bar' },
86
- });
87
-
88
- const encodedJwt = await jwt.sign(testSigner);
89
- const newJwt = Jwt.fromEncode(encodedJwt);
90
- const verified = await newJwt.verify(testVerifier);
91
- expect(verified).toStrictEqual({
92
- header: { alg: 'EdDSA' },
93
- payload: { foo: 'bar' },
94
- });
95
- try {
96
- await newJwt.verify(() => false);
97
- } catch (e: unknown) {
98
- expect(e).toBeInstanceOf(SDJWTException);
99
- }
100
- });
101
-
102
- test('encode', async () => {
103
- const { privateKey } = Crypto.generateKeyPairSync('ed25519');
104
- const testSigner: Signer = async (data: string) => {
105
- const sig = Crypto.sign(null, Buffer.from(data), privateKey);
106
- return Buffer.from(sig).toString('base64url');
107
- };
108
-
109
- const jwt = new Jwt({
110
- header: { alg: 'EdDSA' },
111
- payload: { foo: 'bar' },
112
- });
113
-
114
- const encodedJwt = await jwt.sign(testSigner);
115
- const newJwt = Jwt.fromEncode(encodedJwt);
116
- const newEncodedJwt = newJwt.encodeJwt();
117
- expect(newEncodedJwt).toBe(encodedJwt);
118
- });
119
-
120
- test('decode failed', () => {
121
- expect(() => Jwt.fromEncode('asfasfas')).toThrow();
122
- });
123
-
124
- test('encode failed', async () => {
125
- const jwt = new Jwt({
126
- header: { alg: 'EdDSA' },
127
- });
128
-
129
- try {
130
- jwt.encodeJwt();
131
- } catch (e: unknown) {
132
- expect(e).toBeInstanceOf(SDJWTException);
133
- }
134
- });
135
-
136
- test('getUnsignedToken failed', async () => {
137
- const { privateKey } = 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 jwt = new Jwt({
144
- header: { alg: 'EdDSA' },
145
- });
146
-
147
- try {
148
- await jwt.sign(testSigner);
149
- } catch (e: unknown) {
150
- expect(e).toBeInstanceOf(SDJWTException);
151
- }
152
- });
153
-
154
- test('wrong encoded field', async () => {
155
- const { privateKey } = Crypto.generateKeyPairSync('ed25519');
156
- const testSigner: Signer = async (data: string) => {
157
- const sig = Crypto.sign(null, Buffer.from(data), privateKey);
158
- return Buffer.from(sig).toString('base64url');
159
- };
160
-
161
- const jwt = new Jwt({
162
- header: { alg: 'EdDSA' },
163
- payload: { foo: 'bar' },
164
- encoded: 'asfasfafaf.dfasfafafasf', // it has to be 3 parts
165
- });
166
-
167
- try {
168
- await jwt.sign(testSigner);
169
- } catch (e: unknown) {
170
- expect(e).toBeInstanceOf(SDJWTException);
171
- }
172
- });
173
-
174
- test('verify failed no signature', async () => {
175
- const { publicKey } = Crypto.generateKeyPairSync('ed25519');
176
- const testVerifier: Verifier = async (data: string, sig: string) => {
177
- return Crypto.verify(
178
- null,
179
- Buffer.from(data),
180
- publicKey,
181
- Buffer.from(sig, 'base64url'),
182
- );
183
- };
184
-
185
- const jwt = new Jwt({
186
- header: { alg: 'EdDSA' },
187
- payload: { foo: 'bar' },
188
- });
189
-
190
- try {
191
- await jwt.verify(testVerifier);
192
- } catch (e: unknown) {
193
- expect(e).toBeInstanceOf(SDJWTException);
194
- }
195
- });
196
-
197
- test('verify with issuance date in the future', async () => {
198
- const { publicKey } = Crypto.generateKeyPairSync('ed25519');
199
- const testVerifier: Verifier = async (data: string, sig: string) => {
200
- return Crypto.verify(
201
- null,
202
- Buffer.from(data),
203
- publicKey,
204
- Buffer.from(sig, 'base64url'),
205
- );
206
- };
207
-
208
- const jwt = new Jwt({
209
- header: { alg: 'EdDSA' },
210
- payload: { iat: Math.floor(Date.now() / 1000) + 100 },
211
- });
212
-
213
- try {
214
- await jwt.verify(testVerifier);
215
- } catch (e: unknown) {
216
- expect(e).toBeInstanceOf(SDJWTException);
217
- expect((e as SDJWTException).message).toBe(
218
- 'Verify Error: JWT is not yet valid',
219
- );
220
- }
221
- });
222
-
223
- test('verify with not before in the future', async () => {
224
- const { publicKey } = Crypto.generateKeyPairSync('ed25519');
225
- const testVerifier: Verifier = async (data: string, sig: string) => {
226
- return Crypto.verify(
227
- null,
228
- Buffer.from(data),
229
- publicKey,
230
- Buffer.from(sig, 'base64url'),
231
- );
232
- };
233
-
234
- const jwt = new Jwt({
235
- header: { alg: 'EdDSA' },
236
- payload: { nbf: Math.floor(Date.now() / 1000) + 100 },
237
- });
238
-
239
- try {
240
- await jwt.verify(testVerifier);
241
- } catch (e: unknown) {
242
- expect(e).toBeInstanceOf(SDJWTException);
243
- expect((e as SDJWTException).message).toBe(
244
- 'Verify Error: JWT is not yet valid',
245
- );
246
- }
247
- });
248
-
249
- test('verify with expired', async () => {
250
- const { publicKey } = Crypto.generateKeyPairSync('ed25519');
251
- const testVerifier: Verifier = async (data: string, sig: string) => {
252
- return Crypto.verify(
253
- null,
254
- Buffer.from(data),
255
- publicKey,
256
- Buffer.from(sig, 'base64url'),
257
- );
258
- };
259
-
260
- const jwt = new Jwt({
261
- header: { alg: 'EdDSA' },
262
- payload: { exp: Math.floor(Date.now() / 1000) },
263
- });
264
-
265
- try {
266
- await jwt.verify(testVerifier, {
267
- currentDate: Math.floor(Date.now() / 1000) + 100,
268
- });
269
- } catch (e: unknown) {
270
- expect(e).toBeInstanceOf(SDJWTException);
271
- expect((e as SDJWTException).message).toBe(
272
- 'Verify Error: JWT is expired',
273
- );
274
- }
275
- });
276
-
277
- test('verify with skew', async () => {
278
- const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
279
- const testVerifier: Verifier = async (data: string, sig: string) => {
280
- return Crypto.verify(
281
- null,
282
- Buffer.from(data),
283
- publicKey,
284
- Buffer.from(sig, 'base64url'),
285
- );
286
- };
287
-
288
- const jwt = new Jwt({
289
- header: { alg: 'EdDSA' },
290
- payload: { exp: Math.floor(Date.now() / 1000) - 1 },
291
- });
292
-
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
- await jwt.sign(testSigner);
299
- await jwt.verify(testVerifier, { skewSeconds: 2 });
300
- });
301
- });