From 2aaa1ce1e23bfcd9e56c7aea78e0b1af4ee138c0 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Tue, 3 Nov 2020 00:25:02 +0100 Subject: [PATCH] Make claimVerification return errors --- src/claimVerification.js | 39 ++++++++++++++++++++++++++------------- src/index.js | 15 ++++++++++----- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/claimVerification.js b/src/claimVerification.js index bac5516..ddd291a 100644 --- a/src/claimVerification.js +++ b/src/claimVerification.js @@ -1,10 +1,10 @@ const utils = require('./utils') -const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => { - let isVerified = false, re +const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => { + let re - if (!proofData) { - return isVerified + if (res.isVerified || !proofData) { + return res } if (checkPath.length == 0) { @@ -12,43 +12,56 @@ const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => { default: case 'contains': re = new RegExp(checkClaim.replace('[', '\\[').replace(']', '\\]'), "gi") - return re.test(proofData.replace(/\r?\n|\r/, '')) + res.isVerified = re.test(proofData.replace(/\r?\n|\r/, '')) break case 'equals': - return proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase() + res.isVerified = proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase() break case 'oneOf': re = new RegExp(checkClaim, "gi") - return re.test(proofData.join("|")) + res.isVerified = re.test(proofData.join("|")) break } + return res + } + + if (!(checkPath[0] in proofData)) { + res.errors.push('err_data_structure_incorrect') + return res } if (Array.isArray(proofData)) { proofData.forEach((item, i) => { - isVerified = isVerified || runOnJson(item, checkPath, checkClaim, checkRelation) + res = runOnJson(res, item, checkPath, checkClaim, checkRelation) }) } else if (Array.isArray(proofData[checkPath[0]])) { proofData[checkPath[0]].forEach((item, i) => { - isVerified = isVerified || runOnJson(item, checkPath.slice(1), checkClaim, checkRelation) + res = runOnJson(res, item, checkPath.slice(1), checkClaim, checkRelation) }) } else { - isVerified = isVerified || runOnJson(proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation) + res = runOnJson(res, proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation) } - return isVerified; + return res } const run = (proofData, spData) => { + let res = { + isVerified: false, + errors: [] + } + switch (spData.proof.format) { case 'json': - return runOnJson(proofData, spData.claim.path, utils.generateClaim(spData.claim.fingerprint, spData.claim.format), spData.claim.relation) + res = runOnJson(res, proofData, spData.claim.path, utils.generateClaim(spData.claim.fingerprint, spData.claim.format), spData.claim.relation) break case 'text': re = new RegExp(utils.generateClaim(spData.claim.fingerprint, spData.claim.format), "gi") - return re.test(proofData.replace(/\r?\n|\r/, '')) + res = re.test(proofData.replace(/\r?\n|\r/, '')) break } + + return res } exports.run = run diff --git a/src/index.js b/src/index.js index ff7fdec..0bf0cac 100644 --- a/src/index.js +++ b/src/index.js @@ -31,9 +31,8 @@ const verify = async (uri, fingerprint, opts) => { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { return spMatches } - - let claimHasBeenVerified = false, sp, iSp = 0, res, proofData, spData = null - while (!claimHasBeenVerified && iSp < spMatches.length) { + let claimVerificationDone = false, claimVerificationResult, sp, iSp = 0, res, proofData, spData + while (!claimVerificationDone && iSp < spMatches.length) { spData = spMatches[iSp] spData.claim.fingerprint = fingerprint @@ -47,13 +46,19 @@ const verify = async (uri, fingerprint, opts) => { proofData = await serviceproviders.proxyRequestHandler(spData) } - claimHasBeenVerified = claimVerification.run(proofData, spData) + if (!proofData) { continue } + + claimVerificationResult = claimVerification.run(proofData, spData) + + if (claimVerificationResult.errors.length == 0) { + claimVerificationDone = true + } iSp++ } return { - isVerified: claimHasBeenVerified, + isVerified: claimVerificationResult.isVerified, matchedServiceprovider: spData ? spData.serviceprovider.name : null, verificationData: spData }