@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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +201 -0
- package/README.md +106 -0
- package/dist/index.d.mts +659 -0
- package/dist/index.d.ts +659 -0
- package/dist/index.js +1906 -0
- package/dist/index.mjs +1846 -0
- package/package.json +59 -47
- package/src/decode/decode.ts +376 -0
- package/src/decode/index.ts +1 -0
- package/src/decoy.ts +15 -0
- package/src/flattenJSON.ts +99 -0
- package/src/generalJSON.ts +148 -0
- package/src/index.ts +945 -0
- package/src/jwt.ts +190 -0
- package/src/kbjwt.ts +81 -0
- package/src/present/index.ts +1 -0
- package/src/present/present.ts +210 -0
- package/src/sdjwt.ts +354 -0
- package/src/test/decode/decode.spec.ts +202 -0
- package/src/test/decoy.spec.ts +30 -0
- package/src/test/flattenJSON.spec.ts +122 -0
- package/src/test/generalJSON.spec.ts +198 -0
- package/src/test/index.spec.ts +974 -0
- package/src/test/jwt.spec.ts +301 -0
- package/src/test/kbjwt.spec.ts +565 -0
- package/src/test/pass.spec.ts +7 -0
- package/src/test/present/present.spec.ts +305 -0
- package/src/test/sdjwt.spec.ts +384 -0
- package/src/test/types/type.spec.ts +88 -0
- package/src/test/utils/base64url.spec.ts +33 -0
- package/src/test/utils/disclosure.spec.ts +170 -0
- package/src/test/utils/error.spec.ts +15 -0
- package/src/types/index.ts +2 -0
- package/src/types/type.ts +249 -0
- package/src/types/verification-error.ts +55 -0
- package/src/utils/base64url.ts +6 -0
- package/src/utils/disclosure.ts +101 -0
- package/src/utils/error.ts +25 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/strict-json.ts +17 -0
- package/test/app-e2e.spec.ts +292 -0
- package/test/array_data_types.json +31 -0
- package/test/array_full_sd.json +21 -0
- package/test/array_in_sd.json +13 -0
- package/test/array_nested_in_plain.json +29 -0
- package/test/array_none_disclosed.json +17 -0
- package/test/array_of_nulls.json +15 -0
- package/test/array_of_objects.json +65 -0
- package/test/array_of_scalars.json +19 -0
- package/test/array_recursive_sd.json +35 -0
- package/test/array_recursive_sd_some_disclosed.json +63 -0
- package/test/complex.json +43 -0
- package/test/header_mod.json +48 -0
- package/test/json_serialization.json +48 -0
- package/test/key_binding.json +48 -0
- package/test/no_sd.json +36 -0
- package/test/object_data_types.json +62 -0
- package/test/recursions.json +117 -0
- package/test/rfc9901-validation.spec.ts +150 -0
- package/tsconfig.json +7 -0
- package/vitest.config.mts +4 -0
- package/build/base64url.d.ts +0 -28
- package/build/base64url.js +0 -40
- package/build/base64url.js.map +0 -1
- package/build/hasherAlgorithm.d.ts +0 -70
- package/build/hasherAlgorithm.js +0 -75
- package/build/hasherAlgorithm.js.map +0 -1
- package/build/index.d.ts +0 -11
- package/build/index.js +0 -20
- package/build/index.js.map +0 -1
- package/build/jwt/compact.d.ts +0 -6
- package/build/jwt/compact.js +0 -27
- package/build/jwt/compact.js.map +0 -1
- package/build/jwt/error.d.ts +0 -2
- package/build/jwt/error.js +0 -7
- package/build/jwt/error.js.map +0 -1
- package/build/jwt/index.d.ts +0 -3
- package/build/jwt/index.js +0 -20
- package/build/jwt/index.js.map +0 -1
- package/build/jwt/jwt.d.ts +0 -204
- package/build/jwt/jwt.js +0 -324
- package/build/jwt/jwt.js.map +0 -1
- package/build/keyBinding/index.d.ts +0 -1
- package/build/keyBinding/index.js +0 -18
- package/build/keyBinding/index.js.map +0 -1
- package/build/keyBinding/keyBinding.d.ts +0 -56
- package/build/keyBinding/keyBinding.js +0 -99
- package/build/keyBinding/keyBinding.js.map +0 -1
- package/build/sdJwt/compact.d.ts +0 -8
- package/build/sdJwt/compact.js +0 -39
- package/build/sdJwt/compact.js.map +0 -1
- package/build/sdJwt/decoys.d.ts +0 -2
- package/build/sdJwt/decoys.js +0 -35
- package/build/sdJwt/decoys.js.map +0 -1
- package/build/sdJwt/disclosureFrame.d.ts +0 -7
- package/build/sdJwt/disclosureFrame.js +0 -89
- package/build/sdJwt/disclosureFrame.js.map +0 -1
- package/build/sdJwt/disclosureMapping.d.ts +0 -44
- package/build/sdJwt/disclosureMapping.js +0 -289
- package/build/sdJwt/disclosureMapping.js.map +0 -1
- package/build/sdJwt/disclosures.d.ts +0 -14
- package/build/sdJwt/disclosures.js +0 -59
- package/build/sdJwt/disclosures.js.map +0 -1
- package/build/sdJwt/error.d.ts +0 -2
- package/build/sdJwt/error.js +0 -7
- package/build/sdJwt/error.js.map +0 -1
- package/build/sdJwt/index.d.ts +0 -7
- package/build/sdJwt/index.js +0 -24
- package/build/sdJwt/index.js.map +0 -1
- package/build/sdJwt/presentationFrame.d.ts +0 -4
- package/build/sdJwt/presentationFrame.js +0 -73
- package/build/sdJwt/presentationFrame.js.map +0 -1
- package/build/sdJwt/sdJwt.d.ts +0 -204
- package/build/sdJwt/sdJwt.js +0 -383
- package/build/sdJwt/sdJwt.js.map +0 -1
- package/build/sdJwt/swapClaim.d.ts +0 -3
- package/build/sdJwt/swapClaim.js +0 -90
- package/build/sdJwt/swapClaim.js.map +0 -1
- package/build/sdJwt/types.d.ts +0 -5
- package/build/sdJwt/types.js +0 -3
- package/build/sdJwt/types.js.map +0 -1
- package/build/sdJwtVc/error.d.ts +0 -2
- package/build/sdJwtVc/error.js +0 -7
- package/build/sdJwtVc/error.js.map +0 -1
- package/build/sdJwtVc/index.d.ts +0 -2
- package/build/sdJwtVc/index.js +0 -19
- package/build/sdJwtVc/index.js.map +0 -1
- package/build/sdJwtVc/sdJwtVc.d.ts +0 -47
- package/build/sdJwtVc/sdJwtVc.js +0 -125
- package/build/sdJwtVc/sdJwtVc.js.map +0 -1
- package/build/signatureAndEncryptionAlgorithm.d.ts +0 -105
- package/build/signatureAndEncryptionAlgorithm.js +0 -110
- package/build/signatureAndEncryptionAlgorithm.js.map +0 -1
- package/build/types/disclosure.d.ts +0 -5
- package/build/types/disclosure.js +0 -3
- package/build/types/disclosure.js.map +0 -1
- package/build/types/frame.d.ts +0 -5
- package/build/types/frame.js +0 -3
- package/build/types/frame.js.map +0 -1
- package/build/types/hasher.d.ts +0 -14
- package/build/types/hasher.js +0 -3
- package/build/types/hasher.js.map +0 -1
- package/build/types/index.d.ts +0 -6
- package/build/types/index.js +0 -23
- package/build/types/index.js.map +0 -1
- package/build/types/present.d.ts +0 -2
- package/build/types/present.js +0 -3
- package/build/types/present.js.map +0 -1
- package/build/types/saltGenerator.d.ts +0 -17
- package/build/types/saltGenerator.js +0 -3
- package/build/types/saltGenerator.js.map +0 -1
- package/build/types/signer.d.ts +0 -2
- package/build/types/signer.js +0 -3
- package/build/types/signer.js.map +0 -1
- package/build/types/utils.d.ts +0 -5
- package/build/types/utils.js +0 -3
- package/build/types/utils.js.map +0 -1
- package/build/types/verifier.d.ts +0 -14
- package/build/types/verifier.js +0 -3
- package/build/types/verifier.js.map +0 -1
- package/build/utils/index.d.ts +0 -2
- package/build/utils/index.js +0 -19
- package/build/utils/index.js.map +0 -1
- package/build/utils/traverse.d.ts +0 -8
- package/build/utils/traverse.js +0 -29
- package/build/utils/traverse.js.map +0 -1
- package/build/utils/utils.d.ts +0 -7
- package/build/utils/utils.js +0 -109
- 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
|
+
});
|