doipjs/src/verifications.js

127 lines
3 KiB
JavaScript
Raw Normal View History

2021-04-16 05:13:06 -06:00
/*
Copyright 2021 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 E = require('./enums')
2021-04-22 07:14:21 -06:00
/**
* @module verifications
* @ignore
*/
2021-04-19 03:44:30 -06:00
const runJSON = (proofData, checkPath, checkClaim, checkRelation) => {
2021-04-16 05:13:06 -06:00
let re
if (!proofData) {
return false
}
if (Array.isArray(proofData)) {
let result = false
proofData.forEach((item, i) => {
2021-04-19 03:44:30 -06:00
if (result) {
return
}
2021-04-16 05:13:06 -06:00
result = runJSON(item, checkPath, checkClaim, checkRelation)
})
return result
}
if (checkPath.length == 0) {
switch (checkRelation) {
default:
case E.ClaimRelation.CONTAINS:
re = new RegExp(checkClaim, 'gi')
return re.test(proofData.replace(/\r?\n|\r|\\/g, ''))
break
case E.ClaimRelation.EQUALS:
2021-04-19 03:44:30 -06:00
return (
proofData.replace(/\r?\n|\r|\\/g, '').toLowerCase() ==
2021-04-16 05:13:06 -06:00
checkClaim.toLowerCase()
2021-04-19 03:44:30 -06:00
)
2021-04-16 05:13:06 -06:00
break
case E.ClaimRelation.ONEOF:
re = new RegExp(checkClaim, 'gi')
return re.test(proofData.join('|'))
break
}
}
try {
checkPath[0] in proofData
} catch (e) {
throw new Error('err_json_structure_incorrect')
}
return runJSON(
proofData[checkPath[0]],
checkPath.slice(1),
checkClaim,
checkRelation
)
}
2021-04-22 07:14:21 -06:00
/**
* Run the verification by finding the formatted fingerprint in the proof
* @param {object} proofData - The proof data
* @param {object} claimData - The claim data
* @param {string} fingerprint - The fingerprint
* @returns {object}
*/
2021-04-16 05:13:06 -06:00
const run = (proofData, claimData, fingerprint) => {
let res = {
result: false,
completed: false,
errors: [],
}
2021-04-19 03:44:30 -06:00
2021-04-16 05:13:06 -06:00
switch (claimData.proof.request.format) {
case E.ProofFormat.JSON:
try {
res.result = runJSON(
proofData,
claimData.claim.path,
utils.generateClaim(fingerprint, claimData.claim.format),
claimData.claim.relation
)
res.completed = true
} catch (error) {
res.errors.push(error.message ? error.message : error)
}
break
case E.ProofFormat.TEXT:
try {
const re = new RegExp(
utils
.generateClaim(fingerprint, claimData.claim.format)
.replace('[', '\\[')
.replace(']', '\\]'),
'gi'
)
res.result = re.test(proofData.replace(/\r?\n|\r/, ''))
res.completed = true
} catch (error) {
res.errors.push('err_unknown_text_verification')
}
break
}
return res
}
2021-04-19 03:44:30 -06:00
exports.run = run