Restructure code

Merge claimVerification and proofs into claims
This commit is contained in:
Yarmo Mackenbach 2020-11-17 01:18:12 +01:00
parent 2e8820a9ae
commit 094aca1283
4 changed files with 92 additions and 112 deletions

View file

@ -1,100 +0,0 @@
/*
Copyright 2020 Yarmo Mackenbach
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const utils = require('./utils')
const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
let re
if (res.isVerified || !proofData) {
return res
}
if (Array.isArray(proofData)) {
proofData.forEach((item, i) => {
res = runOnJson(res, item, checkPath, checkClaim, checkRelation)
})
return res
}
if (checkPath.length == 0) {
switch (checkRelation) {
default:
case 'contains':
re = new RegExp(
checkClaim.replace('[', '\\[').replace(']', '\\]'),
'gi'
)
res.isVerified = re.test(proofData.replace(/\r?\n|\r/, ''))
break
case 'equals':
res.isVerified =
proofData.replace(/\r?\n|\r/, '').toLowerCase() ==
checkClaim.toLowerCase()
break
case 'oneOf':
re = new RegExp(checkClaim, 'gi')
res.isVerified = re.test(proofData.join('|'))
break
}
return res
}
try {
checkPath[0] in proofData
} catch (e) {
res.errors.push('err_data_structure_incorrect')
return res
}
res = runOnJson(
res,
proofData[checkPath[0]],
checkPath.slice(1),
checkClaim,
checkRelation
)
return res
}
const run = (proofData, spData) => {
let res = {
isVerified: false,
errors: [],
}
switch (spData.proof.format) {
case 'json':
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'
)
res = re.test(proofData.replace(/\r?\n|\r/, ''))
break
}
return res
}
exports.run = run

View file

@ -16,7 +16,89 @@ limitations under the License.
const mergeOptions = require('merge-options') const mergeOptions = require('merge-options')
const validUrl = require('valid-url') const validUrl = require('valid-url')
const serviceproviders = require('./serviceproviders') const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification') const utils = require('./utils')
const runVerificationJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
let re
if (res.isVerified || !proofData) {
return res
}
if (Array.isArray(proofData)) {
proofData.forEach((item, i) => {
res = runVerificationJson(res, item, checkPath, checkClaim, checkRelation)
})
return res
}
if (checkPath.length == 0) {
switch (checkRelation) {
default:
case 'contains':
re = new RegExp(
checkClaim.replace('[', '\\[').replace(']', '\\]'),
'gi'
)
res.isVerified = re.test(proofData.replace(/\r?\n|\r/, ''))
break
case 'equals':
res.isVerified =
proofData.replace(/\r?\n|\r/, '').toLowerCase() ==
checkClaim.toLowerCase()
break
case 'oneOf':
re = new RegExp(checkClaim, 'gi')
res.isVerified = re.test(proofData.join('|'))
break
}
return res
}
try {
checkPath[0] in proofData
} catch (e) {
res.errors.push('err_data_structure_incorrect')
return res
}
res = runVerificationJson(
res,
proofData[checkPath[0]],
checkPath.slice(1),
checkClaim,
checkRelation
)
return res
}
const runVerification = (proofData, spData) => {
let res = {
isVerified: false,
errors: [],
}
switch (spData.proof.format) {
case 'json':
res = runVerificationJson(
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'
)
res = re.test(proofData.replace(/\r?\n|\r/, ''))
break
}
return res
}
const verify = async (uri, fingerprint, opts) => { const verify = async (uri, fingerprint, opts) => {
if (!fingerprint) { if (!fingerprint) {
@ -67,7 +149,7 @@ const verify = async (uri, fingerprint, opts) => {
} }
if (proofData) { if (proofData) {
claimVerificationResult = claimVerification.run(proofData, spData) claimVerificationResult = runVerification(proofData, spData)
if (claimVerificationResult.errors.length == 0) { if (claimVerificationResult.errors.length == 0) {
claimVerificationDone = true claimVerificationDone = true

View file

@ -13,14 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const proofs = require('./proofs') const claims = require('./claims')
const keys = require('./keys') const keys = require('./keys')
const serviceproviders = require('./serviceproviders') const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification')
const utils = require('./utils') const utils = require('./utils')
exports.proofs = proofs exports.claims = claims
exports.keys = keys exports.keys = keys
exports.serviceproviders = serviceproviders exports.serviceproviders = serviceproviders
exports.claimVerification = claimVerification
exports.utils = utils exports.utils = utils

View file

@ -57,17 +57,17 @@ const pattern = {
}, },
} }
describe('proofs.verify', () => { describe('claims.verify', () => {
it('should be a function (3 arguments)', () => { it('should be a function (3 arguments)', () => {
expect(doipjs.proofs.verify).to.be.a('function') expect(doipjs.claims.verify).to.be.a('function')
expect(doipjs.proofs.verify).to.have.length(3) expect(doipjs.claims.verify).to.have.length(3)
}) })
it('should throw an error for non-valid URIs', () => { it('should throw an error for non-valid URIs', () => {
return expect(doipjs.proofs.verify('noURI')).to.eventually.be.rejectedWith( return expect(doipjs.claims.verify('noURI')).to.eventually.be.rejectedWith(
'Not a valid URI' 'Not a valid URI'
) )
return expect( return expect(
doipjs.proofs.verify('domain.org') doipjs.claims.verify('domain.org')
).to.eventually.be.rejectedWith('Not a valid URI') ).to.eventually.be.rejectedWith('Not a valid URI')
}) })
@ -79,7 +79,7 @@ describe('proofs.verify', () => {
} }
it(`should return a valid object for the "${spName}" service provider`, async () => { it(`should return a valid object for the "${spName}" service provider`, async () => {
const matches = await doipjs.proofs.verify(sp.tests[0].uri, null, { const matches = await doipjs.claims.verify(sp.tests[0].uri, null, {
returnMatchesOnly: true, returnMatchesOnly: true,
}) })
expect(matches).to.be.a('array') expect(matches).to.be.a('array')