Skip to content

Commit

Permalink
Merge pull request #94 from identity-com/ID-111_support_constraints_f…
Browse files Browse the repository at this point in the history
…or_parent_path

ID-111 support constraints for parent path
  • Loading branch information
lucmir authored Apr 4, 2019
2 parents 08b199b + e4d2707 commit 1c1d26a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
22 changes: 22 additions & 0 deletions __test__/creds/VerifiableCredential.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,28 @@ describe('Unit tests for Verifiable Credentials', () => {
})).toBeTruthy();
});

it('Should match constraints targeting the parent properties of dates', () => {
const name = new Claim.IdentityName({ givenNames: 'Joao', otherNames: 'Barbosa', familyNames: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const cred = new VC('credential-cvc:Identity-v1', uuidv4(), null, [name, dob], '1');
expect(cred.isMatch({
claims: [
{ path: 'identity.dateOfBirth', is: { $lt: 1554377905342 } }, // 4-4-2019
],
})).toBeTruthy();
});

it('Should match constraints targeting the parent properties and string deltas', () => {
const name = new Claim.IdentityName({ givenNames: 'Joao', otherNames: 'Barbosa', familyNames: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
const cred = new VC('credential-cvc:Identity-v1', uuidv4(), null, [name, dob], '1');
expect(cred.isMatch({
claims: [
{ path: 'identity.dateOfBirth', is: { $gte: '-18y' } },
],
})).toBeTruthy();
});

it('Should not match', () => {
const name = new Claim.IdentityName({ givenNames: 'Joao', otherNames: 'Barbosa', familyNames: 'Santos' });
const dob = new Claim.IdentityDateOfBirth({ day: 20, month: 3, year: 1978 });
Expand Down
36 changes: 34 additions & 2 deletions src/creds/VerifiableCredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ function transformConstraint(constraints) {
return resultConstraints;
}

/**
* Checks if object is a Date Structure (has day, month, year properties)
*
* @param obj - Structure to test
* @return {boolean}
*/
function isDateStructure(obj) {
const objKeys = _.keys(obj);
if (objKeys.length !== 3) {
// it has more or less keys the (day, month, year)
return false;
}
return (_.includes(objKeys, 'day') && _.includes(objKeys, 'month') && _.includes(objKeys, 'year'));
}

/**
* Trasnform {day, month, year } to Unix Date
*
* @param obj
* @return {number}
*/
function transformDate(obj) {
return new Date(obj.year, obj.month, obj.day).getTime();
}
/**
* Transforms a list of UCAs into the signature property of the verifiable claims
*/
Expand Down Expand Up @@ -440,9 +464,17 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas,
this.isMatch = (constraints) => {
const siftConstraints = transformConstraint(constraints);
let result = true;

const claims = _.cloneDeep(this.claim);
_.forEach(siftConstraints, (constraint) => {
result = (sift.indexOf(constraint, [this.claim]) > -1);
const path = _.keys(constraint)[0];
const pathValue = _.get(claims, path);
if (isDateStructure(pathValue)) {
_.set(claims, path, transformDate(pathValue));
// transforns delta values lile "-18y" to a proper timestamp
// eslint-disable-next-line no-confusing-arrow
_.set(constraint, path, _.mapValues(constraint[path], obj => _.isString(obj) ? timestamp.now(obj) : obj));
}
result = (sift.indexOf(constraint, [claims]) > -1);
return result;
});
return result;
Expand Down

0 comments on commit 1c1d26a

Please sign in to comment.