@digitalbazaar/vc
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -104,1 +104,19 @@ /*! | ||
} | ||
/** | ||
* Compares two times with consideration of max clock skew | ||
* | ||
* @param {object} options - Options. | ||
* @param {number} options.t1 - time 1 | ||
* @param {number} options.t2 - time 2 | ||
* @param {number} options.maxClockSkew - number of seconds | ||
* @returns {number} - A number greater or less than zero | ||
*/ | ||
export function compareTime({t1, t2, maxClockSkew}) { | ||
// `maxClockSkew` is in seconds, so transform to milliseconds | ||
if(Math.abs(t1 - t2) < (maxClockSkew * 1000)) { | ||
// times are equal within the max clock skew | ||
return 0; | ||
} | ||
return t1 < t2 ? -1 : 1; | ||
} |
@@ -41,2 +41,3 @@ /** | ||
checkContextVersion, | ||
compareTime, | ||
getContextForVersion | ||
@@ -107,2 +108,6 @@ } from './helpers.js'; | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* | ||
@@ -117,3 +122,4 @@ * @throws {Error} If missing required properties. | ||
documentLoader = defaultDocumentLoader, | ||
now | ||
now, | ||
maxClockSkew = 300 | ||
} = {}) { | ||
@@ -141,3 +147,3 @@ // check to make sure the `suite` has required params | ||
// run common credential checks | ||
_checkCredential({credential, now, mode: 'issue'}); | ||
_checkCredential({credential, now, mode: 'issue', maxClockSkew}); | ||
@@ -226,2 +232,6 @@ return jsigs.sign(credential, {purpose, documentLoader, suite}); | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* | ||
@@ -272,2 +282,6 @@ * @returns {Promise<VerifyPresentationResult>} The verification result. | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* | ||
@@ -304,2 +318,6 @@ * @returns {Promise<VerifyCredentialResult>} The verification result. | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* | ||
@@ -316,6 +334,6 @@ * @throws {Error} If required parameters are missing (in `_checkCredential`). | ||
async function _verifyCredential(options = {}) { | ||
const {credential, checkStatus, now} = options; | ||
const {credential, checkStatus, now, maxClockSkew} = options; | ||
// run common credential checks | ||
_checkCredential({credential, now}); | ||
_checkCredential({credential, now, maxClockSkew}); | ||
@@ -363,2 +381,6 @@ // if credential status is provided, a `checkStatus` function must be given | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* @param {number} [options.version = 2.0] - The VC context version to use. | ||
@@ -374,3 +396,3 @@ * | ||
export function createPresentation({ | ||
verifiableCredential, id, holder, now, version = 2.0 | ||
verifiableCredential, id, holder, now, version = 2.0, maxClockSkew = 300 | ||
} = {}) { | ||
@@ -386,3 +408,3 @@ const initialContext = getContextForVersion({version}); | ||
for(const credential of credentials) { | ||
_checkCredential({credential, now}); | ||
_checkCredential({credential, now, maxClockSkew}); | ||
} | ||
@@ -470,2 +492,6 @@ presentation.verifiableCredential = credentials; | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* | ||
@@ -586,2 +612,6 @@ * @throws {Error} If presentation is missing required params. | ||
* ISO 8601 format or an instance of Date. Defaults to current date time. | ||
* @param {number} [options.maxClockSkew=300] - A maximum number of seconds | ||
* that clocks may be skewed when checking capability expiration date-times | ||
* against `date` and when comparing invocation proof creation time against | ||
* delegation proof creation time. | ||
* @param {string} [options.mode] - The mode of operation for this | ||
@@ -594,3 +624,3 @@ * validation function, either `issue` or `verify`. | ||
export function _checkCredential({ | ||
credential, now = new Date(), mode = 'verify' | ||
credential, now = new Date(), mode = 'verify', maxClockSkew = 300 | ||
} = {}) { | ||
@@ -634,3 +664,4 @@ if(typeof now === 'string') { | ||
// check if `now` is after `expirationDate` | ||
if(now > new Date(credential.expirationDate)) { | ||
const expirationDate = new Date(credential.expirationDate); | ||
if(compareTime({t1: now, t2: expirationDate, maxClockSkew}) > 0) { | ||
throw new Error('Credential has expired.'); | ||
@@ -643,3 +674,3 @@ } | ||
const issuanceDate = new Date(credential.issuanceDate); | ||
if(now < issuanceDate) { | ||
if(compareTime({t1: issuanceDate, t2: now, maxClockSkew}) > 0) { | ||
throw new Error( | ||
@@ -658,3 +689,3 @@ `The current date time (${now.toISOString()}) is before the ` + | ||
validUntil = new Date(credential.validUntil); | ||
if(now > validUntil) { | ||
if(compareTime({t1: now, t2: validUntil, maxClockSkew}) > 0) { | ||
throw new Error( | ||
@@ -671,3 +702,3 @@ `The current date time (${now.toISOString()}) is after ` + | ||
validFrom = new Date(credential.validFrom); | ||
if(now < validFrom) { | ||
if(compareTime({t1: validFrom, t2: now, maxClockSkew}) > 0) { | ||
throw new Error( | ||
@@ -674,0 +705,0 @@ `The current date time (${now.toISOString()}) is before ` + |
{ | ||
"name": "@digitalbazaar/vc", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "Verifiable Credentials JavaScript library.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/digitalbazaar/vc", |
@@ -75,3 +75,3 @@ # Verifiable Credentials JS Library _(@digitalbazaar/vc)_ | ||
```js | ||
import vc from '@digitalbazaar/vc'; | ||
import * as vc from '@digitalbazaar/vc'; | ||
@@ -98,3 +98,3 @@ // Required to set up a suite instance with private key | ||
```js | ||
const vc = require('@digitalbazaar/vc'); | ||
import * as vc from '@digitalbazaar/vc'; | ||
@@ -482,4 +482,4 @@ // Sample unsigned credential | ||
// and not an insecure source. | ||
import * as vc from '@digitalbazaar/vc'; | ||
const {extendContextLoader} = require('jsonld-signatures'); | ||
const vc = require('@digitalbazaar/vc'); | ||
// @digitalbazaar/vc exports its own secure documentLoader. | ||
@@ -522,2 +522,4 @@ const {defaultDocumentLoader} = vc; | ||
```js | ||
import * as vc from '@digitalbazaar/vc'; | ||
const vp = await vc.signPresentation({ | ||
@@ -585,2 +587,4 @@ presentation, suite, challenge, documentLoader | ||
```js | ||
import * as vc from '@digitalbazaar/vc'; | ||
// challenge has been received from the requesting party - see 'challenge' | ||
@@ -598,2 +602,4 @@ // section below | ||
```js | ||
import * as vc from '@digitalbazaar/vc'; | ||
const result = await vc.verify({ | ||
@@ -600,0 +606,0 @@ presentation, suite, documentLoader, unsignedPresentation: true |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63899
1018
697