From 1b1d11c802b1ebdeed4439e65a83b1c9f5286fd6 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Sat, 24 Oct 2020 18:43:41 +0200 Subject: [PATCH] WIP Handling of fetched data --- src/index.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 1557d63..d75e23b 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ const validUrl = require('valid-url') +const bent = require('bent') +const req = bent('GET') const { serviceprovidersList, serviceproviders } = require('./serviceproviders') const matchServiceproviders = (uri) => { @@ -29,7 +31,7 @@ const matchServiceproviders = (uri) => { return matches } -const verify = (uri, fingerprint, opts) => { +const verify = async (uri, fingerprint, opts) => { if (!opts) { opts = {} } if (!validUrl.isUri(uri)) { @@ -41,6 +43,31 @@ const verify = (uri, fingerprint, opts) => { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { return spMatches } + + let claimHasBeenVerified = false, sp, iSp = 0, res, proofData + while (!claimHasBeenVerified && iSp < spMatches.length) { + sp = spMatches[iSp] + + res = null + + if (!sp.proof.useProxy || 'forceDirectRequest' in opts && opts.forceDirectRequest) { + res = await req(sp.proof.fetch ? sp.proof.fetch : sp.proof.uri) + + switch (sp.proof.format) { + case 'json': + proofData = await res.json() + break + case 'text': + proofData = await res.text() + break + default: + throw new Error('No specified proof data format') + break + } + } + + iSp++ + } } exports.verify = verify