Add handling of proof data

This commit is contained in:
Yarmo Mackenbach 2020-10-25 00:32:12 +02:00
parent 6e966f4df5
commit 118bfc1c0f

View file

@ -17,15 +17,17 @@ const validUrl = require('valid-url')
const bent = require('bent') const bent = require('bent')
const req = bent('GET') const req = bent('GET')
const serviceproviders = require('./serviceproviders') const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification')
const verify = async (uri, fingerprint, opts) => { const verify = async (uri, fingerprint, opts) => {
if (!opts) { opts = {} } if (!opts) { opts = {} }
if (!fingerprint) { fingerprint = null }
if (!validUrl.isUri(uri)) { if (!validUrl.isUri(uri)) {
throw new Error('Not a valid URI') throw new Error('Not a valid URI')
} }
const spMatches = serviceproviders.match(uri) const spMatches = serviceproviders.match(uri, fingerprint)
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches return spMatches
@ -33,14 +35,15 @@ const verify = async (uri, fingerprint, opts) => {
let claimHasBeenVerified = false, sp, iSp = 0, res, proofData let claimHasBeenVerified = false, sp, iSp = 0, res, proofData
while (!claimHasBeenVerified && iSp < spMatches.length) { while (!claimHasBeenVerified && iSp < spMatches.length) {
sp = spMatches[iSp] spData = spMatches[iSp]
spData.claim.fingerprint = fingerprint
res = null res = null
if (!sp.proof.useProxy || 'forceDirectRequest' in opts && opts.forceDirectRequest) { if (!spData.proof.useProxy || 'forceDirectRequest' in opts && opts.forceDirectRequest) {
res = await req(sp.proof.fetch ? sp.proof.fetch : sp.proof.uri) res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri)
switch (sp.proof.format) { switch (spData.proof.format) {
case 'json': case 'json':
proofData = await res.json() proofData = await res.json()
break break
@ -53,8 +56,15 @@ const verify = async (uri, fingerprint, opts) => {
} }
} }
claimHasBeenVerified = claimVerification.run(proofData, spData)
iSp++ iSp++
} }
return {
isVerified: claimHasBeenVerified,
verificationData: spData
}
} }
exports.verify = verify exports.verify = verify