@digitalbazaar/vc 2.0.0 → 4.0.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/README.md +7 -1
- package/lib/CredentialIssuancePurpose.js +6 -7
- package/lib/contexts/index.js +17 -11
- package/lib/contexts/odrl.js +3 -3
- package/lib/contexts/vc-examples-v1.js +21 -2
- package/lib/documentLoader.js +4 -6
- package/lib/index.js +652 -3
- package/package.json +29 -43
- package/CHANGELOG.md +0 -102
- package/lib/vc.js +0 -601
package/lib/index.js
CHANGED
|
@@ -1,8 +1,657 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A
|
|
2
|
+
* A JavaScript implementation of Verifiable Credentials.
|
|
3
3
|
*
|
|
4
|
+
* @author Dave Longley
|
|
4
5
|
* @author David I. Lehn
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
+
* @license BSD 3-Clause License
|
|
8
|
+
* Copyright (c) 2017-2022 Digital Bazaar, Inc.
|
|
9
|
+
* All rights reserved.
|
|
10
|
+
*
|
|
11
|
+
* Redistribution and use in source and binary forms, with or without
|
|
12
|
+
* modification, are permitted provided that the following conditions are met:
|
|
13
|
+
*
|
|
14
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
15
|
+
* this list of conditions and the following disclaimer.
|
|
16
|
+
*
|
|
17
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
18
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
19
|
+
* documentation and/or other materials provided with the distribution.
|
|
20
|
+
*
|
|
21
|
+
* Neither the name of the Digital Bazaar, Inc. nor the names of its
|
|
22
|
+
* contributors may be used to endorse or promote products derived from
|
|
23
|
+
* this software without specific prior written permission.
|
|
24
|
+
*
|
|
25
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
26
|
+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
27
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
28
|
+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
29
|
+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
30
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
31
|
+
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
32
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
33
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
34
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
35
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
36
|
+
*/
|
|
37
|
+
import jsonld from 'jsonld';
|
|
38
|
+
import jsigs from 'jsonld-signatures';
|
|
39
|
+
import {CredentialIssuancePurpose} from './CredentialIssuancePurpose.js';
|
|
40
|
+
import {documentLoader as _documentLoader} from './documentLoader.js';
|
|
41
|
+
export const defaultDocumentLoader =
|
|
42
|
+
jsigs.extendContextLoader(_documentLoader);
|
|
43
|
+
import * as credentialsContext from 'credentials-context';
|
|
44
|
+
|
|
45
|
+
const {AuthenticationProofPurpose} = jsigs.purposes;
|
|
46
|
+
const {constants: {CREDENTIALS_CONTEXT_V1_URL}} = credentialsContext;
|
|
47
|
+
|
|
48
|
+
export {CredentialIssuancePurpose};
|
|
49
|
+
|
|
50
|
+
// Z and T can be lowercase
|
|
51
|
+
// RFC3339 regex
|
|
52
|
+
export const dateRegex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
|
|
53
|
+
'(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):' +
|
|
54
|
+
'([0-5][0-9]):([0-5][0-9]|60)' +
|
|
55
|
+
'(\\.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):' +
|
|
56
|
+
'([0-5][0-9]))$', 'i');
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @typedef {object} LinkedDataSignature
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {object} Presentation
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @typedef {object} ProofPurpose
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {object} VerifiableCredential
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {object} VerifiablePresentation
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {object} VerifyPresentationResult
|
|
80
|
+
* @property {boolean} verified - True if verified, false if not.
|
|
81
|
+
* @property {object} presentationResult
|
|
82
|
+
* @property {Array} credentialResults
|
|
83
|
+
* @property {object} error
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @typedef {object} VerifyCredentialResult
|
|
88
|
+
* @property {boolean} verified - True if verified, false if not.
|
|
89
|
+
* @property {object} statusResult
|
|
90
|
+
* @property {Array} results
|
|
91
|
+
* @property {object} error
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Issues a verifiable credential (by taking a base credential document,
|
|
96
|
+
* and adding a digital signature to it).
|
|
97
|
+
*
|
|
98
|
+
* @param {object} [options={}] - The options to use.
|
|
99
|
+
*
|
|
100
|
+
* @param {object} options.credential - Base credential document.
|
|
101
|
+
* @param {LinkedDataSignature} options.suite - Signature suite (with private
|
|
102
|
+
* key material), passed in to sign().
|
|
103
|
+
*
|
|
104
|
+
* @param {ProofPurpose} [options.purpose] - A ProofPurpose. If not specified,
|
|
105
|
+
* a default purpose will be created.
|
|
106
|
+
*
|
|
107
|
+
* Other optional params passed to `sign()`:
|
|
108
|
+
* @param {object} [options.documentLoader] - A document loader.
|
|
109
|
+
* @param {object} [options.expansionMap] - An expansion map.
|
|
110
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
111
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
112
|
+
*
|
|
113
|
+
* @throws {Error} If missing required properties.
|
|
114
|
+
*
|
|
115
|
+
* @returns {Promise<VerifiableCredential>} Resolves on completion.
|
|
116
|
+
*/
|
|
117
|
+
export async function issue({
|
|
118
|
+
credential, suite, expansionMap,
|
|
119
|
+
purpose = new CredentialIssuancePurpose(),
|
|
120
|
+
documentLoader = defaultDocumentLoader,
|
|
121
|
+
now
|
|
122
|
+
} = {}) {
|
|
123
|
+
// check to make sure the `suite` has required params
|
|
124
|
+
// Note: verificationMethod defaults to publicKey.id, in suite constructor
|
|
125
|
+
if(!suite) {
|
|
126
|
+
throw new TypeError('"suite" parameter is required for issuing.');
|
|
127
|
+
}
|
|
128
|
+
if(!suite.verificationMethod) {
|
|
129
|
+
throw new TypeError('"suite.verificationMethod" property is required.');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if(!credential) {
|
|
133
|
+
throw new TypeError('"credential" parameter is required for issuing.');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Set the issuance date to now(), if missing
|
|
137
|
+
if(!credential.issuanceDate) {
|
|
138
|
+
const now = (new Date()).toJSON();
|
|
139
|
+
credential.issuanceDate = `${now.substr(0, now.length - 5)}Z`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// run common credential checks
|
|
143
|
+
_checkCredential({credential, now});
|
|
144
|
+
|
|
145
|
+
return jsigs.sign(credential, {purpose, documentLoader, suite, expansionMap});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Verifies a verifiable presentation:
|
|
150
|
+
* - Checks that the presentation is well-formed
|
|
151
|
+
* - Checks the proofs (for example, checks digital signatures against the
|
|
152
|
+
* provided public keys).
|
|
153
|
+
*
|
|
154
|
+
* @param {object} [options={}] - The options to use.
|
|
155
|
+
*
|
|
156
|
+
* @param {VerifiablePresentation} options.presentation - Verifiable
|
|
157
|
+
* presentation, signed or unsigned, that may contain within it a
|
|
158
|
+
* verifiable credential.
|
|
159
|
+
*
|
|
160
|
+
* @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - One or
|
|
161
|
+
* more signature suites that are supported by the caller's use case. This is
|
|
162
|
+
* an explicit design decision -- the calling code must specify which
|
|
163
|
+
* signature types (ed25519, RSA, etc) are allowed.
|
|
164
|
+
* Although it is expected that the secure resolution/fetching of the public
|
|
165
|
+
* key material (to verify against) is to be handled by the documentLoader,
|
|
166
|
+
* the suite param can optionally include the key directly.
|
|
167
|
+
*
|
|
168
|
+
* @param {boolean} [options.unsignedPresentation=false] - By default, this
|
|
169
|
+
* function assumes that a presentation is signed (and will return an error if
|
|
170
|
+
* a `proof` section is missing). Set this to `true` if you're using an
|
|
171
|
+
* unsigned presentation.
|
|
172
|
+
*
|
|
173
|
+
* Either pass in a proof purpose,
|
|
174
|
+
* @param {AuthenticationProofPurpose} [options.presentationPurpose] - Optional
|
|
175
|
+
* proof purpose (a default one will be created if not passed in).
|
|
176
|
+
*
|
|
177
|
+
* or a default purpose will be created with params:
|
|
178
|
+
* @param {string} [options.challenge] - Required if purpose is not passed in.
|
|
179
|
+
* @param {string} [options.controller] - A controller.
|
|
180
|
+
* @param {string} [options.domain] - A domain.
|
|
181
|
+
*
|
|
182
|
+
* @param {Function} [options.documentLoader] - A document loader.
|
|
183
|
+
* @param {Function} [options.checkStatus] - Optional function for checking
|
|
184
|
+
* credential status if `credentialStatus` is present on the credential.
|
|
185
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
186
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
187
|
+
*
|
|
188
|
+
* @returns {Promise<VerifyPresentationResult>} The verification result.
|
|
189
|
+
*/
|
|
190
|
+
export async function verify(options = {}) {
|
|
191
|
+
const {presentation} = options;
|
|
192
|
+
try {
|
|
193
|
+
if(!presentation) {
|
|
194
|
+
throw new TypeError(
|
|
195
|
+
'A "presentation" property is required for verifying.');
|
|
196
|
+
}
|
|
197
|
+
return _verifyPresentation(options);
|
|
198
|
+
} catch(error) {
|
|
199
|
+
return {
|
|
200
|
+
verified: false,
|
|
201
|
+
results: [{presentation, verified: false, error}],
|
|
202
|
+
error
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Verifies a verifiable credential:
|
|
209
|
+
* - Checks that the credential is well-formed
|
|
210
|
+
* - Checks the proofs (for example, checks digital signatures against the
|
|
211
|
+
* provided public keys).
|
|
212
|
+
*
|
|
213
|
+
* @param {object} [options={}] - The options.
|
|
214
|
+
*
|
|
215
|
+
* @param {object} options.credential - Verifiable credential.
|
|
216
|
+
*
|
|
217
|
+
* @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - One or
|
|
218
|
+
* more signature suites that are supported by the caller's use case. This is
|
|
219
|
+
* an explicit design decision -- the calling code must specify which
|
|
220
|
+
* signature types (ed25519, RSA, etc) are allowed.
|
|
221
|
+
* Although it is expected that the secure resolution/fetching of the public
|
|
222
|
+
* key material (to verify against) is to be handled by the documentLoader,
|
|
223
|
+
* the suite param can optionally include the key directly.
|
|
224
|
+
*
|
|
225
|
+
* @param {CredentialIssuancePurpose} [options.purpose] - Optional
|
|
226
|
+
* proof purpose (a default one will be created if not passed in).
|
|
227
|
+
* @param {Function} [options.documentLoader] - A document loader.
|
|
228
|
+
* @param {Function} [options.checkStatus] - Optional function for checking
|
|
229
|
+
* credential status if `credentialStatus` is present on the credential.
|
|
230
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
231
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
232
|
+
*
|
|
233
|
+
* @returns {Promise<VerifyCredentialResult>} The verification result.
|
|
234
|
+
*/
|
|
235
|
+
export async function verifyCredential(options = {}) {
|
|
236
|
+
const {credential} = options;
|
|
237
|
+
try {
|
|
238
|
+
if(!credential) {
|
|
239
|
+
throw new TypeError(
|
|
240
|
+
'A "credential" property is required for verifying.');
|
|
241
|
+
}
|
|
242
|
+
return await _verifyCredential(options);
|
|
243
|
+
} catch(error) {
|
|
244
|
+
return {
|
|
245
|
+
verified: false,
|
|
246
|
+
results: [{credential, verified: false, error}],
|
|
247
|
+
error
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Verifies a verifiable credential.
|
|
254
|
+
*
|
|
255
|
+
* @private
|
|
256
|
+
* @param {object} [options={}] - The options.
|
|
257
|
+
*
|
|
258
|
+
* @param {object} options.credential - Verifiable credential.
|
|
259
|
+
* @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - See the
|
|
260
|
+
* definition in the `verify()` docstring, for this param.
|
|
261
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
262
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
263
|
+
*
|
|
264
|
+
* @throws {Error} If required parameters are missing (in `_checkCredential`).
|
|
265
|
+
*
|
|
266
|
+
* @param {CredentialIssuancePurpose} [options.purpose] - A purpose.
|
|
267
|
+
* @param {Function} [options.documentLoader] - A document loader.
|
|
268
|
+
* @param {Function} [options.checkStatus] - Optional function for checking
|
|
269
|
+
* credential status if `credentialStatus` is present on the credential.
|
|
270
|
+
*
|
|
271
|
+
* @returns {Promise<VerifyCredentialResult>} The verification result.
|
|
272
|
+
*/
|
|
273
|
+
async function _verifyCredential(options = {}) {
|
|
274
|
+
const {credential, checkStatus, now} = options;
|
|
275
|
+
|
|
276
|
+
// run common credential checks
|
|
277
|
+
_checkCredential({credential, now});
|
|
278
|
+
|
|
279
|
+
// if credential status is provided, a `checkStatus` function must be given
|
|
280
|
+
if(credential.credentialStatus && typeof options.checkStatus !== 'function') {
|
|
281
|
+
throw new TypeError(
|
|
282
|
+
'A "checkStatus" function must be given to verify credentials with ' +
|
|
283
|
+
'"credentialStatus".');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const documentLoader = options.documentLoader || defaultDocumentLoader;
|
|
287
|
+
|
|
288
|
+
const {controller} = options;
|
|
289
|
+
const purpose = options.purpose || new CredentialIssuancePurpose({
|
|
290
|
+
controller
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const result = await jsigs.verify(
|
|
294
|
+
credential, {purpose, documentLoader, ...options});
|
|
295
|
+
|
|
296
|
+
// if verification has already failed, skip status check
|
|
297
|
+
if(!result.verified) {
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if(credential.credentialStatus) {
|
|
302
|
+
result.statusResult = await checkStatus(options);
|
|
303
|
+
if(!result.statusResult.verified) {
|
|
304
|
+
result.verified = false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Creates an unsigned presentation from a given verifiable credential.
|
|
313
|
+
*
|
|
314
|
+
* @param {object} options - Options to use.
|
|
315
|
+
* @param {object|Array<object>} [options.verifiableCredential] - One or more
|
|
316
|
+
* verifiable credential.
|
|
317
|
+
* @param {string} [options.id] - Optional VP id.
|
|
318
|
+
* @param {string} [options.holder] - Optional presentation holder url.
|
|
319
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
320
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
321
|
+
*
|
|
322
|
+
* @throws {TypeError} If verifiableCredential param is missing.
|
|
323
|
+
* @throws {Error} If the credential (or the presentation params) are missing
|
|
324
|
+
* required properties.
|
|
325
|
+
*
|
|
326
|
+
* @returns {Presentation} The credential wrapped inside of a
|
|
327
|
+
* VerifiablePresentation.
|
|
328
|
+
*/
|
|
329
|
+
export function createPresentation({
|
|
330
|
+
verifiableCredential, id, holder, now
|
|
331
|
+
} = {}) {
|
|
332
|
+
const presentation = {
|
|
333
|
+
'@context': [CREDENTIALS_CONTEXT_V1_URL],
|
|
334
|
+
type: ['VerifiablePresentation']
|
|
335
|
+
};
|
|
336
|
+
if(verifiableCredential) {
|
|
337
|
+
const credentials = [].concat(verifiableCredential);
|
|
338
|
+
// ensure all credentials are valid
|
|
339
|
+
for(const credential of credentials) {
|
|
340
|
+
_checkCredential({credential, now});
|
|
341
|
+
}
|
|
342
|
+
presentation.verifiableCredential = credentials;
|
|
343
|
+
}
|
|
344
|
+
if(id) {
|
|
345
|
+
presentation.id = id;
|
|
346
|
+
}
|
|
347
|
+
if(holder) {
|
|
348
|
+
presentation.holder = holder;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
_checkPresentation(presentation);
|
|
352
|
+
|
|
353
|
+
return presentation;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Signs a given presentation.
|
|
358
|
+
*
|
|
359
|
+
* @param {object} [options={}] - Options to use.
|
|
360
|
+
*
|
|
361
|
+
* Required:
|
|
362
|
+
* @param {Presentation} options.presentation - A presentation.
|
|
363
|
+
* @param {LinkedDataSignature} options.suite - passed in to sign()
|
|
364
|
+
*
|
|
365
|
+
* Either pass in a ProofPurpose, or a default one will be created with params:
|
|
366
|
+
* @param {ProofPurpose} [options.purpose] - A ProofPurpose. If not specified,
|
|
367
|
+
* a default purpose will be created with the domain and challenge options.
|
|
368
|
+
*
|
|
369
|
+
* @param {string} [options.domain] - A domain.
|
|
370
|
+
* @param {string} options.challenge - A required challenge.
|
|
371
|
+
*
|
|
372
|
+
* @param {Function} [options.documentLoader] - A document loader.
|
|
373
|
+
*
|
|
374
|
+
* @returns {Promise<{VerifiablePresentation}>} A VerifiablePresentation with
|
|
375
|
+
* a proof.
|
|
376
|
+
*/
|
|
377
|
+
export async function signPresentation(options = {}) {
|
|
378
|
+
const {presentation, domain, challenge} = options;
|
|
379
|
+
const purpose = options.purpose || new AuthenticationProofPurpose({
|
|
380
|
+
domain,
|
|
381
|
+
challenge
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const documentLoader = options.documentLoader || defaultDocumentLoader;
|
|
385
|
+
|
|
386
|
+
return jsigs.sign(presentation, {purpose, documentLoader, ...options});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Verifies that the VerifiablePresentation is well formed, and checks the
|
|
391
|
+
* proof signature if it's present. Also verifies all the VerifiableCredentials
|
|
392
|
+
* that are present in the presentation, if any.
|
|
393
|
+
*
|
|
394
|
+
* @param {object} [options={}] - The options.
|
|
395
|
+
* @param {VerifiablePresentation} options.presentation - A
|
|
396
|
+
* VerifiablePresentation.
|
|
397
|
+
*
|
|
398
|
+
* @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - See the
|
|
399
|
+
* definition in the `verify()` docstring, for this param.
|
|
400
|
+
*
|
|
401
|
+
* @param {boolean} [options.unsignedPresentation=false] - By default, this
|
|
402
|
+
* function assumes that a presentation is signed (and will return an error if
|
|
403
|
+
* a `proof` section is missing). Set this to `true` if you're using an
|
|
404
|
+
* unsigned presentation.
|
|
405
|
+
*
|
|
406
|
+
* Either pass in a proof purpose,
|
|
407
|
+
* @param {AuthenticationProofPurpose} [options.presentationPurpose] - A
|
|
408
|
+
* ProofPurpose. If not specified, a default purpose will be created with
|
|
409
|
+
* the challenge, controller, and domain options.
|
|
410
|
+
*
|
|
411
|
+
* @param {string} [options.challenge] - A challenge. Required if purpose is
|
|
412
|
+
* not passed in.
|
|
413
|
+
* @param {string} [options.controller] - A controller. Required if purpose is
|
|
414
|
+
* not passed in.
|
|
415
|
+
* @param {string} [options.domain] - A domain. Required if purpose is not
|
|
416
|
+
* passed in.
|
|
417
|
+
*
|
|
418
|
+
* @param {Function} [options.documentLoader] - A document loader.
|
|
419
|
+
* @param {Function} [options.checkStatus] - Optional function for checking
|
|
420
|
+
* credential status if `credentialStatus` is present on the credential.
|
|
421
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
422
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
423
|
+
*
|
|
424
|
+
* @throws {Error} If presentation is missing required params.
|
|
425
|
+
*
|
|
426
|
+
* @returns {Promise<VerifyPresentationResult>} The verification result.
|
|
427
|
+
*/
|
|
428
|
+
async function _verifyPresentation(options = {}) {
|
|
429
|
+
const {presentation, unsignedPresentation} = options;
|
|
430
|
+
|
|
431
|
+
_checkPresentation(presentation);
|
|
432
|
+
|
|
433
|
+
const documentLoader = options.documentLoader || defaultDocumentLoader;
|
|
434
|
+
|
|
435
|
+
// FIXME: verify presentation first, then each individual credential
|
|
436
|
+
// only if that proof is verified
|
|
437
|
+
|
|
438
|
+
// if verifiableCredentials are present, verify them, individually
|
|
439
|
+
let credentialResults;
|
|
440
|
+
let verified = true;
|
|
441
|
+
const credentials = jsonld.getValues(presentation, 'verifiableCredential');
|
|
442
|
+
if(credentials.length > 0) {
|
|
443
|
+
// verify every credential in `verifiableCredential`
|
|
444
|
+
credentialResults = await Promise.all(credentials.map(credential => {
|
|
445
|
+
return verifyCredential({credential, documentLoader, ...options});
|
|
446
|
+
}));
|
|
447
|
+
|
|
448
|
+
for(const [i, credentialResult] of credentialResults.entries()) {
|
|
449
|
+
credentialResult.credentialId = credentials[i].id;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const allCredentialsVerified = credentialResults.every(r => r.verified);
|
|
453
|
+
if(!allCredentialsVerified) {
|
|
454
|
+
verified = false;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if(unsignedPresentation) {
|
|
459
|
+
// No need to verify the proof section of this presentation
|
|
460
|
+
return {verified, results: [presentation], credentialResults};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const {controller, domain, challenge} = options;
|
|
464
|
+
if(!options.presentationPurpose && !challenge) {
|
|
465
|
+
throw new Error(
|
|
466
|
+
'A "challenge" param is required for AuthenticationProofPurpose.');
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const purpose = options.presentationPurpose ||
|
|
470
|
+
new AuthenticationProofPurpose({controller, domain, challenge});
|
|
471
|
+
|
|
472
|
+
const presentationResult = await jsigs.verify(
|
|
473
|
+
presentation, {purpose, documentLoader, ...options});
|
|
474
|
+
|
|
475
|
+
return {
|
|
476
|
+
presentationResult,
|
|
477
|
+
verified: verified && presentationResult.verified,
|
|
478
|
+
credentialResults,
|
|
479
|
+
error: presentationResult.error
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* @param {string|object} obj - Either an object with an id property
|
|
485
|
+
* or a string that is an id.
|
|
486
|
+
* @returns {string|undefined} Either an id or undefined.
|
|
487
|
+
* @private
|
|
488
|
+
*
|
|
489
|
+
*/
|
|
490
|
+
function _getId(obj) {
|
|
491
|
+
if(typeof obj === 'string') {
|
|
492
|
+
return obj;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if(!('id' in obj)) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return obj.id;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// export for testing
|
|
503
|
+
/**
|
|
504
|
+
* @param {object} presentation - An object that could be a presentation.
|
|
505
|
+
*
|
|
506
|
+
* @throws {Error}
|
|
507
|
+
* @private
|
|
508
|
+
*/
|
|
509
|
+
export function _checkPresentation(presentation) {
|
|
510
|
+
// normalize to an array to allow the common case of context being a string
|
|
511
|
+
const context = Array.isArray(presentation['@context']) ?
|
|
512
|
+
presentation['@context'] : [presentation['@context']];
|
|
513
|
+
|
|
514
|
+
// ensure first context is 'https://www.w3.org/2018/credentials/v1'
|
|
515
|
+
if(context[0] !== CREDENTIALS_CONTEXT_V1_URL) {
|
|
516
|
+
throw new Error(
|
|
517
|
+
`"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
|
|
518
|
+
'list of contexts.');
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const types = jsonld.getValues(presentation, 'type');
|
|
522
|
+
|
|
523
|
+
// check type presence
|
|
524
|
+
if(!types.includes('VerifiablePresentation')) {
|
|
525
|
+
throw new Error('"type" must include "VerifiablePresentation".');
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// export for testing
|
|
530
|
+
/**
|
|
531
|
+
* @param {object} options - The options.
|
|
532
|
+
* @param {object} options.credential - An object that could be a
|
|
533
|
+
* VerifiableCredential.
|
|
534
|
+
* @param {string|Date} [options.now] - A string representing date time in
|
|
535
|
+
* ISO 8601 format or an instance of Date. Defaults to current date time.
|
|
536
|
+
*
|
|
537
|
+
* @throws {Error}
|
|
538
|
+
* @private
|
|
7
539
|
*/
|
|
8
|
-
|
|
540
|
+
export function _checkCredential({credential, now = new Date()}) {
|
|
541
|
+
if(typeof now === 'string') {
|
|
542
|
+
now = new Date(now);
|
|
543
|
+
}
|
|
544
|
+
// ensure first context is 'https://www.w3.org/2018/credentials/v1'
|
|
545
|
+
if(credential['@context'][0] !== CREDENTIALS_CONTEXT_V1_URL) {
|
|
546
|
+
throw new Error(
|
|
547
|
+
`"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
|
|
548
|
+
'list of contexts.');
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// check type presence and cardinality
|
|
552
|
+
if(!credential.type) {
|
|
553
|
+
throw new Error('"type" property is required.');
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if(!jsonld.getValues(credential, 'type').includes('VerifiableCredential')) {
|
|
557
|
+
throw new Error('"type" must include `VerifiableCredential`.');
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if(!credential.credentialSubject) {
|
|
561
|
+
throw new Error('"credentialSubject" property is required.');
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// If credentialSubject.id is present and is not a URI, reject it
|
|
565
|
+
if(credential.credentialSubject.id) {
|
|
566
|
+
_validateUriId({
|
|
567
|
+
id: credential.credentialSubject.id, propertyName: 'credentialSubject.id'
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
if(!credential.issuer) {
|
|
572
|
+
throw new Error('"issuer" property is required.');
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// check issuanceDate cardinality
|
|
576
|
+
if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
|
|
577
|
+
throw new Error('"issuanceDate" property can only have one value.');
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// check issued is a date
|
|
581
|
+
if(!credential.issuanceDate) {
|
|
582
|
+
throw new Error('"issuanceDate" property is required.');
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if('issuanceDate' in credential) {
|
|
586
|
+
let {issuanceDate} = credential;
|
|
587
|
+
if(!dateRegex.test(issuanceDate)) {
|
|
588
|
+
throw new Error(`"issuanceDate" must be a valid date: ${issuanceDate}`);
|
|
589
|
+
}
|
|
590
|
+
// check if `now` is before `issuanceDate`
|
|
591
|
+
issuanceDate = new Date(issuanceDate);
|
|
592
|
+
if(now < issuanceDate) {
|
|
593
|
+
throw new Error(
|
|
594
|
+
`The current date time (${now.toISOString()}) is before the ` +
|
|
595
|
+
`"issuanceDate" (${issuanceDate.toISOString()}).`);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// check issuer cardinality
|
|
600
|
+
if(jsonld.getValues(credential, 'issuer').length > 1) {
|
|
601
|
+
throw new Error('"issuer" property can only have one value.');
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// check issuer is a URL
|
|
605
|
+
if('issuer' in credential) {
|
|
606
|
+
const issuer = _getId(credential.issuer);
|
|
607
|
+
if(!issuer) {
|
|
608
|
+
throw new Error(`"issuer" id is required.`);
|
|
609
|
+
}
|
|
610
|
+
_validateUriId({id: issuer, propertyName: 'issuer'});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if('credentialStatus' in credential) {
|
|
614
|
+
if(!credential.credentialStatus.id) {
|
|
615
|
+
throw new Error('"credentialStatus" must include an id.');
|
|
616
|
+
}
|
|
617
|
+
if(!credential.credentialStatus.type) {
|
|
618
|
+
throw new Error('"credentialStatus" must include a type.');
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// check evidences are URLs
|
|
623
|
+
jsonld.getValues(credential, 'evidence').forEach(evidence => {
|
|
624
|
+
const evidenceId = _getId(evidence);
|
|
625
|
+
if(evidenceId) {
|
|
626
|
+
_validateUriId({id: evidenceId, propertyName: 'evidence'});
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
if('expirationDate' in credential) {
|
|
631
|
+
const {expirationDate} = credential;
|
|
632
|
+
// check if `expirationDate` property is a date
|
|
633
|
+
if(!dateRegex.test(expirationDate)) {
|
|
634
|
+
throw new Error(
|
|
635
|
+
`"expirationDate" must be a valid date: ${expirationDate}`);
|
|
636
|
+
}
|
|
637
|
+
// check if `now` is after `expirationDate`
|
|
638
|
+
if(now > new Date(expirationDate)) {
|
|
639
|
+
throw new Error('Credential has expired.');
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function _validateUriId({id, propertyName}) {
|
|
645
|
+
let parsed;
|
|
646
|
+
try {
|
|
647
|
+
parsed = new URL(id);
|
|
648
|
+
} catch(e) {
|
|
649
|
+
const error = new TypeError(`"${propertyName}" must be a URI: "${id}".`);
|
|
650
|
+
error.cause = e;
|
|
651
|
+
throw error;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if(!parsed.protocol) {
|
|
655
|
+
throw new TypeError(`"${propertyName}" must be a URI: "${id}".`);
|
|
656
|
+
}
|
|
657
|
+
}
|