Make claimVerification return errors

This commit is contained in:
Yarmo Mackenbach 2020-11-03 00:25:02 +01:00
parent 9c1fdc5c4b
commit 2aaa1ce1e2
2 changed files with 36 additions and 18 deletions

View file

@ -1,10 +1,10 @@
const utils = require('./utils') const utils = require('./utils')
const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => { const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
let isVerified = false, re let re
if (!proofData) { if (res.isVerified || !proofData) {
return isVerified return res
} }
if (checkPath.length == 0) { if (checkPath.length == 0) {
@ -12,43 +12,56 @@ const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => {
default: default:
case 'contains': case 'contains':
re = new RegExp(checkClaim.replace('[', '\\[').replace(']', '\\]'), "gi") 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 break
case 'equals': case 'equals':
return proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase() res.isVerified = proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase()
break break
case 'oneOf': case 'oneOf':
re = new RegExp(checkClaim, "gi") re = new RegExp(checkClaim, "gi")
return re.test(proofData.join("|")) res.isVerified = re.test(proofData.join("|"))
break break
} }
return res
}
if (!(checkPath[0] in proofData)) {
res.errors.push('err_data_structure_incorrect')
return res
} }
if (Array.isArray(proofData)) { if (Array.isArray(proofData)) {
proofData.forEach((item, i) => { 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]])) { } else if (Array.isArray(proofData[checkPath[0]])) {
proofData[checkPath[0]].forEach((item, i) => { 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 { } 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) => { const run = (proofData, spData) => {
let res = {
isVerified: false,
errors: []
}
switch (spData.proof.format) { switch (spData.proof.format) {
case 'json': 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 break
case 'text': case 'text':
re = new RegExp(utils.generateClaim(spData.claim.fingerprint, spData.claim.format), "gi") 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 break
} }
return res
} }
exports.run = run exports.run = run

View file

@ -31,9 +31,8 @@ const verify = async (uri, fingerprint, opts) => {
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches return spMatches
} }
let claimVerificationDone = false, claimVerificationResult, sp, iSp = 0, res, proofData, spData
let claimHasBeenVerified = false, sp, iSp = 0, res, proofData, spData = null while (!claimVerificationDone && iSp < spMatches.length) {
while (!claimHasBeenVerified && iSp < spMatches.length) {
spData = spMatches[iSp] spData = spMatches[iSp]
spData.claim.fingerprint = fingerprint spData.claim.fingerprint = fingerprint
@ -47,13 +46,19 @@ const verify = async (uri, fingerprint, opts) => {
proofData = await serviceproviders.proxyRequestHandler(spData) 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++ iSp++
} }
return { return {
isVerified: claimHasBeenVerified, isVerified: claimVerificationResult.isVerified,
matchedServiceprovider: spData ? spData.serviceprovider.name : null, matchedServiceprovider: spData ? spData.serviceprovider.name : null,
verificationData: spData verificationData: spData
} }