@sd-jwt/core 0.15.2-next.8 → 0.16.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 +18 -0
- package/dist/index.d.mts +92 -92
- package/dist/index.d.ts +92 -92
- package/dist/index.js +232 -232
- package/dist/index.mjs +218 -218
- package/package.json +8 -8
- package/src/flattenJSON.ts +1 -1
- package/src/generalJSON.ts +1 -1
- package/src/index.ts +18 -18
- package/src/jwt.ts +2 -2
- package/src/kbjwt.ts +3 -3
- package/src/sdjwt.ts +13 -14
- package/src/test/decoy.spec.ts +3 -3
- package/src/test/generalJSON.spec.ts +2 -2
- package/src/test/index.spec.ts +8 -8
- package/src/test/jwt.spec.ts +9 -9
- package/src/test/kbjwt.spec.ts +8 -9
- package/src/test/pass.spec.ts +1 -0
- package/src/test/sdjwt.spec.ts +6 -6
- package/test/app-e2e.spec.ts +4 -4
package/dist/index.js
CHANGED
|
@@ -69,11 +69,183 @@ __export(index_exports, {
|
|
|
69
69
|
pack: () => pack
|
|
70
70
|
});
|
|
71
71
|
module.exports = __toCommonJS(index_exports);
|
|
72
|
+
var import_decode5 = require("@sd-jwt/decode");
|
|
73
|
+
var import_types5 = require("@sd-jwt/types");
|
|
72
74
|
var import_utils7 = require("@sd-jwt/utils");
|
|
73
75
|
|
|
74
|
-
// src/
|
|
75
|
-
var import_utils = require("@sd-jwt/utils");
|
|
76
|
+
// src/flattenJSON.ts
|
|
76
77
|
var import_decode = require("@sd-jwt/decode");
|
|
78
|
+
var import_types = require("@sd-jwt/types");
|
|
79
|
+
var import_utils = require("@sd-jwt/utils");
|
|
80
|
+
var FlattenJSON = class _FlattenJSON {
|
|
81
|
+
constructor(data) {
|
|
82
|
+
this.disclosures = data.disclosures;
|
|
83
|
+
this.kb_jwt = data.kb_jwt;
|
|
84
|
+
this.payload = data.jwtData.payload;
|
|
85
|
+
this.signature = data.jwtData.signature;
|
|
86
|
+
this.protected = data.jwtData.protected;
|
|
87
|
+
}
|
|
88
|
+
static fromEncode(encodedSdJwt) {
|
|
89
|
+
const { jwt, disclosures, kbJwt } = (0, import_decode.splitSdJwt)(encodedSdJwt);
|
|
90
|
+
const { 0: protectedHeader, 1: payload, 2: signature } = jwt.split(".");
|
|
91
|
+
if (!protectedHeader || !payload || !signature) {
|
|
92
|
+
throw new import_utils.SDJWTException("Invalid JWT");
|
|
93
|
+
}
|
|
94
|
+
return new _FlattenJSON({
|
|
95
|
+
jwtData: {
|
|
96
|
+
protected: protectedHeader,
|
|
97
|
+
payload,
|
|
98
|
+
signature
|
|
99
|
+
},
|
|
100
|
+
disclosures,
|
|
101
|
+
kb_jwt: kbJwt
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
static fromSerialized(json) {
|
|
105
|
+
return new _FlattenJSON({
|
|
106
|
+
jwtData: {
|
|
107
|
+
protected: json.protected,
|
|
108
|
+
payload: json.payload,
|
|
109
|
+
signature: json.signature
|
|
110
|
+
},
|
|
111
|
+
disclosures: json.header.disclosures,
|
|
112
|
+
kb_jwt: json.header.kb_jwt
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
toJson() {
|
|
116
|
+
return {
|
|
117
|
+
payload: this.payload,
|
|
118
|
+
signature: this.signature,
|
|
119
|
+
protected: this.protected,
|
|
120
|
+
header: {
|
|
121
|
+
disclosures: this.disclosures,
|
|
122
|
+
kb_jwt: this.kb_jwt
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
toEncoded() {
|
|
127
|
+
var _a;
|
|
128
|
+
const data = [];
|
|
129
|
+
const jwt = `${this.protected}.${this.payload}.${this.signature}`;
|
|
130
|
+
data.push(jwt);
|
|
131
|
+
if (this.disclosures && this.disclosures.length > 0) {
|
|
132
|
+
const disclosures = this.disclosures.join(import_types.SD_SEPARATOR);
|
|
133
|
+
data.push(disclosures);
|
|
134
|
+
}
|
|
135
|
+
const kb_jwt = (_a = this.kb_jwt) != null ? _a : "";
|
|
136
|
+
data.push(kb_jwt);
|
|
137
|
+
return data.join(import_types.SD_SEPARATOR);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/generalJSON.ts
|
|
142
|
+
var import_decode2 = require("@sd-jwt/decode");
|
|
143
|
+
var import_types2 = require("@sd-jwt/types");
|
|
144
|
+
var import_utils2 = require("@sd-jwt/utils");
|
|
145
|
+
var GeneralJSON = class _GeneralJSON {
|
|
146
|
+
constructor(data) {
|
|
147
|
+
this.payload = data.payload;
|
|
148
|
+
this.disclosures = data.disclosures;
|
|
149
|
+
this.kb_jwt = data.kb_jwt;
|
|
150
|
+
this.signatures = data.signatures;
|
|
151
|
+
}
|
|
152
|
+
static fromEncode(encodedSdJwt) {
|
|
153
|
+
const { jwt, disclosures, kbJwt } = (0, import_decode2.splitSdJwt)(encodedSdJwt);
|
|
154
|
+
const { 0: protectedHeader, 1: payload, 2: signature } = jwt.split(".");
|
|
155
|
+
if (!protectedHeader || !payload || !signature) {
|
|
156
|
+
throw new import_utils2.SDJWTException("Invalid JWT");
|
|
157
|
+
}
|
|
158
|
+
return new _GeneralJSON({
|
|
159
|
+
payload,
|
|
160
|
+
disclosures,
|
|
161
|
+
kb_jwt: kbJwt,
|
|
162
|
+
signatures: [
|
|
163
|
+
{
|
|
164
|
+
protected: protectedHeader,
|
|
165
|
+
signature
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
static fromSerialized(json) {
|
|
171
|
+
var _a, _b, _c;
|
|
172
|
+
if (!json.signatures[0]) {
|
|
173
|
+
throw new import_utils2.SDJWTException("Invalid JSON");
|
|
174
|
+
}
|
|
175
|
+
const disclosures = (_b = (_a = json.signatures[0].header) == null ? void 0 : _a.disclosures) != null ? _b : [];
|
|
176
|
+
const kb_jwt = (_c = json.signatures[0].header) == null ? void 0 : _c.kb_jwt;
|
|
177
|
+
return new _GeneralJSON({
|
|
178
|
+
payload: json.payload,
|
|
179
|
+
disclosures,
|
|
180
|
+
kb_jwt,
|
|
181
|
+
signatures: json.signatures.map((s) => {
|
|
182
|
+
var _a2;
|
|
183
|
+
return {
|
|
184
|
+
protected: s.protected,
|
|
185
|
+
signature: s.signature,
|
|
186
|
+
kid: (_a2 = s.header) == null ? void 0 : _a2.kid
|
|
187
|
+
};
|
|
188
|
+
})
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
toJson() {
|
|
192
|
+
return {
|
|
193
|
+
payload: this.payload,
|
|
194
|
+
signatures: this.signatures.map((s, i) => {
|
|
195
|
+
if (i !== 0) {
|
|
196
|
+
return {
|
|
197
|
+
header: {
|
|
198
|
+
kid: s.kid
|
|
199
|
+
},
|
|
200
|
+
protected: s.protected,
|
|
201
|
+
signature: s.signature
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
header: {
|
|
206
|
+
disclosures: this.disclosures,
|
|
207
|
+
kid: s.kid,
|
|
208
|
+
kb_jwt: this.kb_jwt
|
|
209
|
+
},
|
|
210
|
+
protected: s.protected,
|
|
211
|
+
signature: s.signature
|
|
212
|
+
};
|
|
213
|
+
})
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
toEncoded(index) {
|
|
217
|
+
var _a;
|
|
218
|
+
if (index < 0 || index >= this.signatures.length) {
|
|
219
|
+
throw new import_utils2.SDJWTException("Index out of bounds");
|
|
220
|
+
}
|
|
221
|
+
const data = [];
|
|
222
|
+
const { protected: protectedHeader, signature } = this.signatures[index];
|
|
223
|
+
const jwt = `${protectedHeader}.${this.payload}.${signature}`;
|
|
224
|
+
data.push(jwt);
|
|
225
|
+
if (this.disclosures && this.disclosures.length > 0) {
|
|
226
|
+
const disclosures = this.disclosures.join(import_types2.SD_SEPARATOR);
|
|
227
|
+
data.push(disclosures);
|
|
228
|
+
}
|
|
229
|
+
const kb = (_a = this.kb_jwt) != null ? _a : "";
|
|
230
|
+
data.push(kb);
|
|
231
|
+
return data.join(import_types2.SD_SEPARATOR);
|
|
232
|
+
}
|
|
233
|
+
addSignature(protectedHeader, signer, kid) {
|
|
234
|
+
return __async(this, null, function* () {
|
|
235
|
+
const header = (0, import_utils2.base64urlEncode)(JSON.stringify(protectedHeader));
|
|
236
|
+
const signature = yield signer(`${header}.${this.payload}`);
|
|
237
|
+
this.signatures.push({
|
|
238
|
+
protected: header,
|
|
239
|
+
signature,
|
|
240
|
+
kid
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/jwt.ts
|
|
247
|
+
var import_decode3 = require("@sd-jwt/decode");
|
|
248
|
+
var import_utils3 = require("@sd-jwt/utils");
|
|
77
249
|
var Jwt = class _Jwt {
|
|
78
250
|
constructor(data) {
|
|
79
251
|
this.header = data == null ? void 0 : data.header;
|
|
@@ -82,7 +254,7 @@ var Jwt = class _Jwt {
|
|
|
82
254
|
this.encoded = data == null ? void 0 : data.encoded;
|
|
83
255
|
}
|
|
84
256
|
static decodeJWT(jwt) {
|
|
85
|
-
return (0,
|
|
257
|
+
return (0, import_decode3.decodeJwt)(jwt);
|
|
86
258
|
}
|
|
87
259
|
static fromEncode(encodedJwt) {
|
|
88
260
|
const { header, payload, signature } = _Jwt.decodeJWT(
|
|
@@ -108,18 +280,18 @@ var Jwt = class _Jwt {
|
|
|
108
280
|
}
|
|
109
281
|
getUnsignedToken() {
|
|
110
282
|
if (!this.header || !this.payload) {
|
|
111
|
-
throw new
|
|
283
|
+
throw new import_utils3.SDJWTException("Serialize Error: Invalid JWT");
|
|
112
284
|
}
|
|
113
285
|
if (this.encoded) {
|
|
114
286
|
const parts = this.encoded.split(".");
|
|
115
287
|
if (parts.length !== 3) {
|
|
116
|
-
throw new
|
|
288
|
+
throw new import_utils3.SDJWTException(`Invalid JWT format: ${this.encoded}`);
|
|
117
289
|
}
|
|
118
290
|
const unsignedToken = parts.slice(0, 2).join(".");
|
|
119
291
|
return unsignedToken;
|
|
120
292
|
}
|
|
121
|
-
const header = (0,
|
|
122
|
-
const payload = (0,
|
|
293
|
+
const header = (0, import_utils3.base64urlEncode)(JSON.stringify(this.header));
|
|
294
|
+
const payload = (0, import_utils3.base64urlEncode)(JSON.stringify(this.payload));
|
|
123
295
|
return `${header}.${payload}`;
|
|
124
296
|
}
|
|
125
297
|
sign(signer) {
|
|
@@ -134,10 +306,10 @@ var Jwt = class _Jwt {
|
|
|
134
306
|
return this.encoded;
|
|
135
307
|
}
|
|
136
308
|
if (!this.header || !this.payload || !this.signature) {
|
|
137
|
-
throw new
|
|
309
|
+
throw new import_utils3.SDJWTException("Serialize Error: Invalid JWT");
|
|
138
310
|
}
|
|
139
|
-
const header = (0,
|
|
140
|
-
const payload = (0,
|
|
311
|
+
const header = (0, import_utils3.base64urlEncode)(JSON.stringify(this.header));
|
|
312
|
+
const payload = (0, import_utils3.base64urlEncode)(JSON.stringify(this.payload));
|
|
141
313
|
const signature = this.signature;
|
|
142
314
|
const compact = `${header}.${payload}.${signature}`;
|
|
143
315
|
this.encoded = compact;
|
|
@@ -156,21 +328,21 @@ var Jwt = class _Jwt {
|
|
|
156
328
|
const skew = (options == null ? void 0 : options.skewSeconds) ? options.skewSeconds : 0;
|
|
157
329
|
const currentDate = (_a = options == null ? void 0 : options.currentDate) != null ? _a : Math.floor(Date.now() / 1e3);
|
|
158
330
|
if (((_b = this.payload) == null ? void 0 : _b.iat) && this.payload.iat - skew > currentDate) {
|
|
159
|
-
throw new
|
|
331
|
+
throw new import_utils3.SDJWTException("Verify Error: JWT is not yet valid");
|
|
160
332
|
}
|
|
161
333
|
if (((_c = this.payload) == null ? void 0 : _c.nbf) && this.payload.nbf - skew > currentDate) {
|
|
162
|
-
throw new
|
|
334
|
+
throw new import_utils3.SDJWTException("Verify Error: JWT is not yet valid");
|
|
163
335
|
}
|
|
164
336
|
if (((_d = this.payload) == null ? void 0 : _d.exp) && this.payload.exp + skew < currentDate) {
|
|
165
|
-
throw new
|
|
337
|
+
throw new import_utils3.SDJWTException("Verify Error: JWT is expired");
|
|
166
338
|
}
|
|
167
339
|
if (!this.signature) {
|
|
168
|
-
throw new
|
|
340
|
+
throw new import_utils3.SDJWTException("Verify Error: no signature in JWT");
|
|
169
341
|
}
|
|
170
342
|
const data = this.getUnsignedToken();
|
|
171
343
|
const verified = yield verifier(data, this.signature);
|
|
172
344
|
if (!verified) {
|
|
173
|
-
throw new
|
|
345
|
+
throw new import_utils3.SDJWTException("Verify Error: Invalid JWT Signature");
|
|
174
346
|
}
|
|
175
347
|
return { payload: this.payload, header: this.header };
|
|
176
348
|
});
|
|
@@ -178,8 +350,8 @@ var Jwt = class _Jwt {
|
|
|
178
350
|
};
|
|
179
351
|
|
|
180
352
|
// src/kbjwt.ts
|
|
181
|
-
var
|
|
182
|
-
var
|
|
353
|
+
var import_types3 = require("@sd-jwt/types");
|
|
354
|
+
var import_utils4 = require("@sd-jwt/utils");
|
|
183
355
|
var KBJwt = class _KBJwt extends Jwt {
|
|
184
356
|
// Checking the validity of the key binding jwt
|
|
185
357
|
// the type unknown is not good, but we don't know at this point how to get the public key of the signer, this is defined in the kbVerifier
|
|
@@ -187,11 +359,11 @@ var KBJwt = class _KBJwt extends Jwt {
|
|
|
187
359
|
return __async(this, null, function* () {
|
|
188
360
|
var _a;
|
|
189
361
|
if (!this.header || !this.payload || !this.signature) {
|
|
190
|
-
throw new
|
|
362
|
+
throw new import_utils4.SDJWTException("Verify Error: Invalid JWT");
|
|
191
363
|
}
|
|
192
|
-
if (!this.header.alg || this.header.alg === "none" || !this.header.typ || this.header.typ !==
|
|
364
|
+
if (!this.header.alg || this.header.alg === "none" || !this.header.typ || this.header.typ !== import_types3.KB_JWT_TYP || !this.payload.iat || !this.payload.aud || !this.payload.nonce || // this is for backward compatibility with version 06
|
|
193
365
|
!(this.payload.sd_hash || ((_a = this.payload) == null ? void 0 : _a._sd_hash))) {
|
|
194
|
-
throw new
|
|
366
|
+
throw new import_utils4.SDJWTException("Invalid Key Binding Jwt");
|
|
195
367
|
}
|
|
196
368
|
const data = this.getUnsignedToken();
|
|
197
369
|
const verified = yield values.verifier(
|
|
@@ -200,10 +372,10 @@ var KBJwt = class _KBJwt extends Jwt {
|
|
|
200
372
|
values.payload
|
|
201
373
|
);
|
|
202
374
|
if (!verified) {
|
|
203
|
-
throw new
|
|
375
|
+
throw new import_utils4.SDJWTException("Verify Error: Invalid JWT Signature");
|
|
204
376
|
}
|
|
205
377
|
if (this.payload.nonce !== values.nonce) {
|
|
206
|
-
throw new
|
|
378
|
+
throw new import_utils4.SDJWTException("Verify Error: Invalid Nonce");
|
|
207
379
|
}
|
|
208
380
|
return { payload: this.payload, header: this.header };
|
|
209
381
|
});
|
|
@@ -223,20 +395,22 @@ var KBJwt = class _KBJwt extends Jwt {
|
|
|
223
395
|
}
|
|
224
396
|
};
|
|
225
397
|
|
|
398
|
+
// src/sdjwt.ts
|
|
399
|
+
var import_decode4 = require("@sd-jwt/decode");
|
|
400
|
+
var import_present = require("@sd-jwt/present");
|
|
401
|
+
var import_types4 = require("@sd-jwt/types");
|
|
402
|
+
var import_utils6 = require("@sd-jwt/utils");
|
|
403
|
+
|
|
226
404
|
// src/decoy.ts
|
|
227
|
-
var
|
|
228
|
-
var createDecoy = (hash, saltGenerator) => __async(
|
|
405
|
+
var import_utils5 = require("@sd-jwt/utils");
|
|
406
|
+
var createDecoy = (hash, saltGenerator) => __async(null, null, function* () {
|
|
229
407
|
const { hasher, alg } = hash;
|
|
230
408
|
const salt = yield saltGenerator(16);
|
|
231
409
|
const decoy = yield hasher(salt, alg);
|
|
232
|
-
return (0,
|
|
410
|
+
return (0, import_utils5.uint8ArrayToBase64Url)(decoy);
|
|
233
411
|
});
|
|
234
412
|
|
|
235
413
|
// src/sdjwt.ts
|
|
236
|
-
var import_utils4 = require("@sd-jwt/utils");
|
|
237
|
-
var import_types2 = require("@sd-jwt/types");
|
|
238
|
-
var import_decode2 = require("@sd-jwt/decode");
|
|
239
|
-
var import_present = require("@sd-jwt/present");
|
|
240
414
|
var SDJwt = class _SDJwt {
|
|
241
415
|
constructor(data) {
|
|
242
416
|
this.jwt = data == null ? void 0 : data.jwt;
|
|
@@ -245,7 +419,7 @@ var SDJwt = class _SDJwt {
|
|
|
245
419
|
}
|
|
246
420
|
static decodeSDJwt(sdjwt, hasher) {
|
|
247
421
|
return __async(this, null, function* () {
|
|
248
|
-
const [encodedJwt, ...encodedDisclosures] = sdjwt.split(
|
|
422
|
+
const [encodedJwt, ...encodedDisclosures] = sdjwt.split(import_types4.SD_SEPARATOR);
|
|
249
423
|
const jwt = Jwt.fromEncode(encodedJwt);
|
|
250
424
|
if (!jwt.payload) {
|
|
251
425
|
throw new Error("Payload is undefined on the JWT. Invalid state reached");
|
|
@@ -258,10 +432,10 @@ var SDJwt = class _SDJwt {
|
|
|
258
432
|
}
|
|
259
433
|
const encodedKeyBindingJwt = encodedDisclosures.pop();
|
|
260
434
|
const kbJwt = encodedKeyBindingJwt ? KBJwt.fromKBEncode(encodedKeyBindingJwt) : void 0;
|
|
261
|
-
const { _sd_alg } = (0,
|
|
435
|
+
const { _sd_alg } = (0, import_decode4.getSDAlgAndPayload)(jwt.payload);
|
|
262
436
|
const disclosures = yield Promise.all(
|
|
263
437
|
encodedDisclosures.map(
|
|
264
|
-
(ed) =>
|
|
438
|
+
(ed) => import_utils6.Disclosure.fromEncode(ed, { alg: _sd_alg, hasher })
|
|
265
439
|
)
|
|
266
440
|
);
|
|
267
441
|
return {
|
|
@@ -273,7 +447,7 @@ var SDJwt = class _SDJwt {
|
|
|
273
447
|
}
|
|
274
448
|
static extractJwt(encodedSdJwt) {
|
|
275
449
|
return __async(this, null, function* () {
|
|
276
|
-
const [encodedJwt, ..._encodedDisclosures] = encodedSdJwt.split(
|
|
450
|
+
const [encodedJwt, ..._encodedDisclosures] = encodedSdJwt.split(import_types4.SD_SEPARATOR);
|
|
277
451
|
return Jwt.fromEncode(encodedJwt);
|
|
278
452
|
});
|
|
279
453
|
}
|
|
@@ -302,12 +476,12 @@ var SDJwt = class _SDJwt {
|
|
|
302
476
|
return __async(this, null, function* () {
|
|
303
477
|
var _a;
|
|
304
478
|
if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
|
|
305
|
-
throw new
|
|
479
|
+
throw new import_utils6.SDJWTException("Invalid sd-jwt: jwt or disclosures is missing");
|
|
306
480
|
}
|
|
307
|
-
const { _sd_alg: alg } = (0,
|
|
481
|
+
const { _sd_alg: alg } = (0, import_decode4.getSDAlgAndPayload)(this.jwt.payload);
|
|
308
482
|
const hash = { alg, hasher };
|
|
309
|
-
const hashmap = yield (0,
|
|
310
|
-
const { disclosureKeymap } = yield (0,
|
|
483
|
+
const hashmap = yield (0, import_decode4.createHashMapping)(this.disclosures, hash);
|
|
484
|
+
const { disclosureKeymap } = yield (0, import_decode4.unpack)(
|
|
311
485
|
this.jwt.payload,
|
|
312
486
|
this.disclosures,
|
|
313
487
|
hasher
|
|
@@ -320,16 +494,16 @@ var SDJwt = class _SDJwt {
|
|
|
320
494
|
encodeSDJwt() {
|
|
321
495
|
const data = [];
|
|
322
496
|
if (!this.jwt) {
|
|
323
|
-
throw new
|
|
497
|
+
throw new import_utils6.SDJWTException("Invalid sd-jwt: jwt is missing");
|
|
324
498
|
}
|
|
325
499
|
const encodedJwt = this.jwt.encodeJwt();
|
|
326
500
|
data.push(encodedJwt);
|
|
327
501
|
if (this.disclosures && this.disclosures.length > 0) {
|
|
328
|
-
const encodeddisclosures = this.disclosures.map((dc) => dc.encode()).join(
|
|
502
|
+
const encodeddisclosures = this.disclosures.map((dc) => dc.encode()).join(import_types4.SD_SEPARATOR);
|
|
329
503
|
data.push(encodeddisclosures);
|
|
330
504
|
}
|
|
331
505
|
data.push(this.kbJwt ? this.kbJwt.encodeJwt() : "");
|
|
332
|
-
return data.join(
|
|
506
|
+
return data.join(import_types4.SD_SEPARATOR);
|
|
333
507
|
}
|
|
334
508
|
keys(hasher) {
|
|
335
509
|
return __async(this, null, function* () {
|
|
@@ -340,9 +514,9 @@ var SDJwt = class _SDJwt {
|
|
|
340
514
|
return __async(this, null, function* () {
|
|
341
515
|
var _a, _b;
|
|
342
516
|
if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
|
|
343
|
-
throw new
|
|
517
|
+
throw new import_utils6.SDJWTException("Invalid sd-jwt: jwt or disclosures is missing");
|
|
344
518
|
}
|
|
345
|
-
const { disclosureKeymap } = yield (0,
|
|
519
|
+
const { disclosureKeymap } = yield (0, import_decode4.unpack)(
|
|
346
520
|
(_b = this.jwt) == null ? void 0 : _b.payload,
|
|
347
521
|
this.disclosures,
|
|
348
522
|
hasher
|
|
@@ -354,9 +528,9 @@ var SDJwt = class _SDJwt {
|
|
|
354
528
|
return __async(this, null, function* () {
|
|
355
529
|
var _a;
|
|
356
530
|
if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
|
|
357
|
-
throw new
|
|
531
|
+
throw new import_utils6.SDJWTException("Invalid sd-jwt: jwt or disclosures is missing");
|
|
358
532
|
}
|
|
359
|
-
const { unpackedObj } = yield (0,
|
|
533
|
+
const { unpackedObj } = yield (0, import_decode4.unpack)(
|
|
360
534
|
this.jwt.payload,
|
|
361
535
|
this.disclosures,
|
|
362
536
|
hasher
|
|
@@ -377,7 +551,7 @@ var listKeys = (obj, prefix = "") => {
|
|
|
377
551
|
}
|
|
378
552
|
return keys;
|
|
379
553
|
};
|
|
380
|
-
var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(
|
|
554
|
+
var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(null, null, function* () {
|
|
381
555
|
var _a, _b;
|
|
382
556
|
if (!disclosureFrame) {
|
|
383
557
|
return {
|
|
@@ -385,15 +559,15 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
385
559
|
disclosures: []
|
|
386
560
|
};
|
|
387
561
|
}
|
|
388
|
-
const sd = (_a = disclosureFrame[
|
|
389
|
-
const decoyCount = (_b = disclosureFrame[
|
|
562
|
+
const sd = (_a = disclosureFrame[import_types4.SD_DIGEST]) != null ? _a : [];
|
|
563
|
+
const decoyCount = (_b = disclosureFrame[import_types4.SD_DECOY]) != null ? _b : 0;
|
|
390
564
|
if (Array.isArray(claims)) {
|
|
391
565
|
const packedClaims2 = [];
|
|
392
566
|
const disclosures2 = [];
|
|
393
567
|
const recursivePackedClaims2 = {};
|
|
394
568
|
for (const key in disclosureFrame) {
|
|
395
|
-
if (key !==
|
|
396
|
-
const idx = Number.parseInt(key);
|
|
569
|
+
if (key !== import_types4.SD_DIGEST) {
|
|
570
|
+
const idx = Number.parseInt(key, 10);
|
|
397
571
|
const packed = yield pack(
|
|
398
572
|
claims[idx],
|
|
399
573
|
disclosureFrame[idx],
|
|
@@ -408,9 +582,9 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
408
582
|
const claim = recursivePackedClaims2[i] ? recursivePackedClaims2[i] : claims[i];
|
|
409
583
|
if (sd.includes(i)) {
|
|
410
584
|
const salt = yield saltGenerator(16);
|
|
411
|
-
const disclosure = new
|
|
585
|
+
const disclosure = new import_utils6.Disclosure([salt, claim]);
|
|
412
586
|
const digest = yield disclosure.digest(hash);
|
|
413
|
-
packedClaims2.push({ [
|
|
587
|
+
packedClaims2.push({ [import_types4.SD_LIST_KEY]: digest });
|
|
414
588
|
disclosures2.push(disclosure);
|
|
415
589
|
} else {
|
|
416
590
|
packedClaims2.push(claim);
|
|
@@ -418,7 +592,7 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
418
592
|
}
|
|
419
593
|
for (let j = 0; j < decoyCount; j++) {
|
|
420
594
|
const decoyDigest = yield createDecoy(hash, saltGenerator);
|
|
421
|
-
packedClaims2.push({ [
|
|
595
|
+
packedClaims2.push({ [import_types4.SD_LIST_KEY]: decoyDigest });
|
|
422
596
|
}
|
|
423
597
|
return { packedClaims: packedClaims2, disclosures: disclosures2 };
|
|
424
598
|
}
|
|
@@ -426,9 +600,9 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
426
600
|
const disclosures = [];
|
|
427
601
|
const recursivePackedClaims = {};
|
|
428
602
|
for (const key in disclosureFrame) {
|
|
429
|
-
if (key !==
|
|
603
|
+
if (key !== import_types4.SD_DIGEST) {
|
|
430
604
|
const packed = yield pack(
|
|
431
|
-
// @ts-
|
|
605
|
+
// @ts-expect-error
|
|
432
606
|
claims[key],
|
|
433
607
|
disclosureFrame[key],
|
|
434
608
|
hash,
|
|
@@ -443,7 +617,7 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
443
617
|
const claim = recursivePackedClaims[key] ? recursivePackedClaims[key] : claims[key];
|
|
444
618
|
if (sd.includes(key)) {
|
|
445
619
|
const salt = yield saltGenerator(16);
|
|
446
|
-
const disclosure = new
|
|
620
|
+
const disclosure = new import_utils6.Disclosure([salt, key, claim]);
|
|
447
621
|
const digest = yield disclosure.digest(hash);
|
|
448
622
|
_sd.push(digest);
|
|
449
623
|
disclosures.push(disclosure);
|
|
@@ -456,185 +630,11 @@ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, nul
|
|
|
456
630
|
_sd.push(decoyDigest);
|
|
457
631
|
}
|
|
458
632
|
if (_sd.length > 0) {
|
|
459
|
-
packedClaims[
|
|
633
|
+
packedClaims[import_types4.SD_DIGEST] = _sd.sort();
|
|
460
634
|
}
|
|
461
635
|
return { packedClaims, disclosures };
|
|
462
636
|
});
|
|
463
637
|
|
|
464
|
-
// src/index.ts
|
|
465
|
-
var import_types5 = require("@sd-jwt/types");
|
|
466
|
-
var import_decode5 = require("@sd-jwt/decode");
|
|
467
|
-
|
|
468
|
-
// src/flattenJSON.ts
|
|
469
|
-
var import_utils5 = require("@sd-jwt/utils");
|
|
470
|
-
var import_decode3 = require("@sd-jwt/decode");
|
|
471
|
-
var import_types3 = require("@sd-jwt/types");
|
|
472
|
-
var FlattenJSON = class _FlattenJSON {
|
|
473
|
-
constructor(data) {
|
|
474
|
-
this.disclosures = data.disclosures;
|
|
475
|
-
this.kb_jwt = data.kb_jwt;
|
|
476
|
-
this.payload = data.jwtData.payload;
|
|
477
|
-
this.signature = data.jwtData.signature;
|
|
478
|
-
this.protected = data.jwtData.protected;
|
|
479
|
-
}
|
|
480
|
-
static fromEncode(encodedSdJwt) {
|
|
481
|
-
const { jwt, disclosures, kbJwt } = (0, import_decode3.splitSdJwt)(encodedSdJwt);
|
|
482
|
-
const { 0: protectedHeader, 1: payload, 2: signature } = jwt.split(".");
|
|
483
|
-
if (!protectedHeader || !payload || !signature) {
|
|
484
|
-
throw new import_utils5.SDJWTException("Invalid JWT");
|
|
485
|
-
}
|
|
486
|
-
return new _FlattenJSON({
|
|
487
|
-
jwtData: {
|
|
488
|
-
protected: protectedHeader,
|
|
489
|
-
payload,
|
|
490
|
-
signature
|
|
491
|
-
},
|
|
492
|
-
disclosures,
|
|
493
|
-
kb_jwt: kbJwt
|
|
494
|
-
});
|
|
495
|
-
}
|
|
496
|
-
static fromSerialized(json) {
|
|
497
|
-
return new _FlattenJSON({
|
|
498
|
-
jwtData: {
|
|
499
|
-
protected: json.protected,
|
|
500
|
-
payload: json.payload,
|
|
501
|
-
signature: json.signature
|
|
502
|
-
},
|
|
503
|
-
disclosures: json.header.disclosures,
|
|
504
|
-
kb_jwt: json.header.kb_jwt
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
toJson() {
|
|
508
|
-
return {
|
|
509
|
-
payload: this.payload,
|
|
510
|
-
signature: this.signature,
|
|
511
|
-
protected: this.protected,
|
|
512
|
-
header: {
|
|
513
|
-
disclosures: this.disclosures,
|
|
514
|
-
kb_jwt: this.kb_jwt
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
toEncoded() {
|
|
519
|
-
var _a;
|
|
520
|
-
const data = [];
|
|
521
|
-
const jwt = `${this.protected}.${this.payload}.${this.signature}`;
|
|
522
|
-
data.push(jwt);
|
|
523
|
-
if (this.disclosures && this.disclosures.length > 0) {
|
|
524
|
-
const disclosures = this.disclosures.join(import_types3.SD_SEPARATOR);
|
|
525
|
-
data.push(disclosures);
|
|
526
|
-
}
|
|
527
|
-
const kb_jwt = (_a = this.kb_jwt) != null ? _a : "";
|
|
528
|
-
data.push(kb_jwt);
|
|
529
|
-
return data.join(import_types3.SD_SEPARATOR);
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
|
|
533
|
-
// src/generalJSON.ts
|
|
534
|
-
var import_utils6 = require("@sd-jwt/utils");
|
|
535
|
-
var import_decode4 = require("@sd-jwt/decode");
|
|
536
|
-
var import_types4 = require("@sd-jwt/types");
|
|
537
|
-
var GeneralJSON = class _GeneralJSON {
|
|
538
|
-
constructor(data) {
|
|
539
|
-
this.payload = data.payload;
|
|
540
|
-
this.disclosures = data.disclosures;
|
|
541
|
-
this.kb_jwt = data.kb_jwt;
|
|
542
|
-
this.signatures = data.signatures;
|
|
543
|
-
}
|
|
544
|
-
static fromEncode(encodedSdJwt) {
|
|
545
|
-
const { jwt, disclosures, kbJwt } = (0, import_decode4.splitSdJwt)(encodedSdJwt);
|
|
546
|
-
const { 0: protectedHeader, 1: payload, 2: signature } = jwt.split(".");
|
|
547
|
-
if (!protectedHeader || !payload || !signature) {
|
|
548
|
-
throw new import_utils6.SDJWTException("Invalid JWT");
|
|
549
|
-
}
|
|
550
|
-
return new _GeneralJSON({
|
|
551
|
-
payload,
|
|
552
|
-
disclosures,
|
|
553
|
-
kb_jwt: kbJwt,
|
|
554
|
-
signatures: [
|
|
555
|
-
{
|
|
556
|
-
protected: protectedHeader,
|
|
557
|
-
signature
|
|
558
|
-
}
|
|
559
|
-
]
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
static fromSerialized(json) {
|
|
563
|
-
var _a, _b, _c;
|
|
564
|
-
if (!json.signatures[0]) {
|
|
565
|
-
throw new import_utils6.SDJWTException("Invalid JSON");
|
|
566
|
-
}
|
|
567
|
-
const disclosures = (_b = (_a = json.signatures[0].header) == null ? void 0 : _a.disclosures) != null ? _b : [];
|
|
568
|
-
const kb_jwt = (_c = json.signatures[0].header) == null ? void 0 : _c.kb_jwt;
|
|
569
|
-
return new _GeneralJSON({
|
|
570
|
-
payload: json.payload,
|
|
571
|
-
disclosures,
|
|
572
|
-
kb_jwt,
|
|
573
|
-
signatures: json.signatures.map((s) => {
|
|
574
|
-
var _a2;
|
|
575
|
-
return {
|
|
576
|
-
protected: s.protected,
|
|
577
|
-
signature: s.signature,
|
|
578
|
-
kid: (_a2 = s.header) == null ? void 0 : _a2.kid
|
|
579
|
-
};
|
|
580
|
-
})
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
toJson() {
|
|
584
|
-
return {
|
|
585
|
-
payload: this.payload,
|
|
586
|
-
signatures: this.signatures.map((s, i) => {
|
|
587
|
-
if (i !== 0) {
|
|
588
|
-
return {
|
|
589
|
-
header: {
|
|
590
|
-
kid: s.kid
|
|
591
|
-
},
|
|
592
|
-
protected: s.protected,
|
|
593
|
-
signature: s.signature
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
return {
|
|
597
|
-
header: {
|
|
598
|
-
disclosures: this.disclosures,
|
|
599
|
-
kid: s.kid,
|
|
600
|
-
kb_jwt: this.kb_jwt
|
|
601
|
-
},
|
|
602
|
-
protected: s.protected,
|
|
603
|
-
signature: s.signature
|
|
604
|
-
};
|
|
605
|
-
})
|
|
606
|
-
};
|
|
607
|
-
}
|
|
608
|
-
toEncoded(index) {
|
|
609
|
-
var _a;
|
|
610
|
-
if (index < 0 || index >= this.signatures.length) {
|
|
611
|
-
throw new import_utils6.SDJWTException("Index out of bounds");
|
|
612
|
-
}
|
|
613
|
-
const data = [];
|
|
614
|
-
const { protected: protectedHeader, signature } = this.signatures[index];
|
|
615
|
-
const jwt = `${protectedHeader}.${this.payload}.${signature}`;
|
|
616
|
-
data.push(jwt);
|
|
617
|
-
if (this.disclosures && this.disclosures.length > 0) {
|
|
618
|
-
const disclosures = this.disclosures.join(import_types4.SD_SEPARATOR);
|
|
619
|
-
data.push(disclosures);
|
|
620
|
-
}
|
|
621
|
-
const kb = (_a = this.kb_jwt) != null ? _a : "";
|
|
622
|
-
data.push(kb);
|
|
623
|
-
return data.join(import_types4.SD_SEPARATOR);
|
|
624
|
-
}
|
|
625
|
-
addSignature(protectedHeader, signer, kid) {
|
|
626
|
-
return __async(this, null, function* () {
|
|
627
|
-
const header = (0, import_utils6.base64urlEncode)(JSON.stringify(protectedHeader));
|
|
628
|
-
const signature = yield signer(`${header}.${this.payload}`);
|
|
629
|
-
this.signatures.push({
|
|
630
|
-
protected: header,
|
|
631
|
-
signature,
|
|
632
|
-
kid
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
|
|
638
638
|
// src/index.ts
|
|
639
639
|
var _SDJwtInstance = class _SDJwtInstance {
|
|
640
640
|
constructor(userConfig) {
|
|
@@ -731,7 +731,7 @@ var _SDJwtInstance = class _SDJwtInstance {
|
|
|
731
731
|
* @param disclosureFrame
|
|
732
732
|
* @returns
|
|
733
733
|
*/
|
|
734
|
-
validateReservedFields(
|
|
734
|
+
validateReservedFields(_disclosureFrame) {
|
|
735
735
|
return;
|
|
736
736
|
}
|
|
737
737
|
present(encodedSDJwt, presentationFrame, options) {
|
|
@@ -986,7 +986,7 @@ var SDJwtGeneralJSONInstance = class {
|
|
|
986
986
|
* @param disclosureFrame
|
|
987
987
|
* @returns
|
|
988
988
|
*/
|
|
989
|
-
validateReservedFields(
|
|
989
|
+
validateReservedFields(_disclosureFrame) {
|
|
990
990
|
return;
|
|
991
991
|
}
|
|
992
992
|
present(generalJSON, presentationFrame, options) {
|