doipjs/src/verifications.js

162 lines
3.9 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
*/
2022-09-10 04:16:50 -06:00
const containsProof = async (data, target) => {
let result = false
2021-04-16 05:13:06 -06:00
2022-09-10 04:16:50 -06:00
// Check for plaintext proof
result = data.replace(/\r?\n|\r/g, '')
.toLowerCase()
.indexOf(target.toLowerCase()) !== -1
// Check for HTTP proof if plaintext not found
if (!result) {
const uris = utils.getUriFromString(data)
for (let index = 0; index < uris.length; index++) {
if (result) continue
const candidate = uris[index]
let candidateURL
try {
candidateURL = new URL(candidate)
} catch (_) {
continue
}
if (candidateURL.protocol !== 'https:') {
continue
}
2022-09-10 08:21:14 -06:00
// Using fetch -> axios doesn't find the ariadne-identity-proof header
const response = await fetch(candidate, { // eslint-disable-line
method: 'HEAD'
2022-09-10 04:16:50 -06:00
})
2022-09-10 08:21:14 -06:00
.catch(e => {
return false
})
2022-09-10 04:16:50 -06:00
2022-09-10 08:21:14 -06:00
if (!response) continue
2022-09-10 04:16:50 -06:00
if (response.status !== 200) continue
2022-09-10 08:21:14 -06:00
if (!response.headers.get('ariadne-identity-proof')) continue
2022-09-10 04:16:50 -06:00
2022-09-10 08:21:14 -06:00
result = response.headers.get('ariadne-identity-proof')
2022-09-10 04:16:50 -06:00
.toLowerCase()
.indexOf(target.toLowerCase()) !== -1
}
}
return result
}
const runJSON = async (proofData, checkPath, checkClaim, checkRelation) => {
2021-04-16 05:13:06 -06:00
if (!proofData) {
return false
}
if (Array.isArray(proofData)) {
let result = false
2022-09-10 04:16:50 -06:00
for (let index = 0; index < proofData.length; index++) {
const item = proofData[index]
2021-04-19 03:44:30 -06:00
if (result) {
2022-09-10 04:16:50 -06:00
continue
2021-04-19 03:44:30 -06:00
}
2022-09-10 04:16:50 -06:00
result = await runJSON(item, checkPath, checkClaim, checkRelation)
}
2021-04-16 05:13:06 -06:00
return result
}
2021-07-09 15:44:52 -06:00
if (checkPath.length === 0) {
2021-04-16 05:13:06 -06:00
switch (checkRelation) {
case E.ClaimRelation.ONEOF:
2022-09-10 04:16:50 -06:00
return await containsProof(proofData.join('|'), checkClaim)
2021-07-09 15:44:52 -06:00
case E.ClaimRelation.CONTAINS:
2022-09-10 04:16:50 -06:00
case E.ClaimRelation.EQUALS:
2021-07-09 15:44:52 -06:00
default:
2022-09-10 04:16:50 -06:00
return await containsProof(proofData, checkClaim)
2021-04-16 05:13:06 -06:00
}
}
2021-07-09 15:44:52 -06:00
if (!(checkPath[0] in proofData)) {
2021-04-16 05:13:06 -06:00
throw new Error('err_json_structure_incorrect')
}
2022-09-10 04:16:50 -06:00
return await runJSON(
2021-04-16 05:13:06 -06:00
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
2022-09-10 04:16:50 -06:00
* @async
2021-04-22 07:14:21 -06:00
* @param {object} proofData - The proof data
* @param {object} claimData - The claim data
* @param {string} fingerprint - The fingerprint
* @returns {object}
*/
2022-09-10 04:16:50 -06:00
const run = async (proofData, claimData, fingerprint) => {
2021-07-09 15:44:52 -06:00
const res = {
2021-04-16 05:13:06 -06:00
result: false,
completed: false,
2021-07-09 15:44:52 -06:00
errors: []
2021-04-16 05:13:06 -06:00
}
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 {
2022-09-10 04:16:50 -06:00
res.result = await runJSON(
2021-04-16 05:13:06 -06:00
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 {
2022-09-10 04:16:50 -06:00
res.result = await containsProof(proofData,
utils.generateClaim(fingerprint, claimData.claim.format))
2021-04-16 05:13:06 -06:00
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