From c7ac3a9d341598e63cb09c3cf0329f8e43cd1cbb Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Tue, 17 Nov 2020 00:50:26 +0100 Subject: [PATCH] Put proofs logic in separate file --- src/index.js | 78 ++------------------------------------ src/proofs.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ test/verify.test.js | 10 ++--- 3 files changed, 101 insertions(+), 79 deletions(-) create mode 100644 src/proofs.js diff --git a/src/index.js b/src/index.js index 894a580..28b3e1e 100644 --- a/src/index.js +++ b/src/index.js @@ -13,84 +13,14 @@ 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 mergeOptions = require('merge-options') -const validUrl = require('valid-url') +const proofs = require('./proofs') +const keys = require('./keys') const serviceproviders = require('./serviceproviders') const claimVerification = require('./claimVerification') const utils = require('./utils') -const verify = async (uri, fingerprint, opts) => { - if (!fingerprint) { - fingerprint = null - } - if (!opts) { - opts = {} - } - - const defaultOpts = { - returnMatchesOnly: false, - proxyPolicy: 'adaptive', - doipProxyHostname: 'proxy.keyoxide.org', - } - opts = mergeOptions(defaultOpts, opts) - - if (!validUrl.isUri(uri)) { - throw new Error('Not a valid URI') - } - - const spMatches = serviceproviders.match(uri, opts) - - if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { - return spMatches - } - let claimVerificationDone = false, - claimVerificationResult, - sp, - iSp = 0, - res, - proofData, - spData - while (!claimVerificationDone && iSp < spMatches.length) { - spData = spMatches[iSp] - spData.claim.fingerprint = fingerprint - - res = null - - if (spData.customRequestHandler instanceof Function) { - proofData = await spData.customRequestHandler(spData, opts) - } else if ( - !spData.proof.useProxy || - ('proxyPolicy' in opts && !opts.useProxyWhenNeeded) - ) { - proofData = await serviceproviders.directRequestHandler(spData, opts) - } else { - proofData = await serviceproviders.proxyRequestHandler(spData, opts) - } - - if (proofData) { - claimVerificationResult = claimVerification.run(proofData, spData) - - if (claimVerificationResult.errors.length == 0) { - claimVerificationDone = true - } - } - - iSp++ - } - - if (!claimVerificationResult) { - claimVerificationResult = { - isVerified: false, - } - } - - return { - isVerified: claimVerificationResult.isVerified, - serviceproviderData: spData, - } -} - -exports.verify = verify +exports.proofs = proofs +exports.keys = keys exports.serviceproviders = serviceproviders exports.claimVerification = claimVerification exports.utils = utils diff --git a/src/proofs.js b/src/proofs.js new file mode 100644 index 0000000..f71fb84 --- /dev/null +++ b/src/proofs.js @@ -0,0 +1,92 @@ +/* +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 mergeOptions = require('merge-options') +const validUrl = require('valid-url') +const serviceproviders = require('./serviceproviders') +const claimVerification = require('./claimVerification') + +const verify = async (uri, fingerprint, opts) => { + if (!fingerprint) { + fingerprint = null + } + if (!opts) { + opts = {} + } + + const defaultOpts = { + returnMatchesOnly: false, + proxyPolicy: 'adaptive', + doipProxyHostname: 'proxy.keyoxide.org', + } + opts = mergeOptions(defaultOpts, opts) + + if (!validUrl.isUri(uri)) { + throw new Error('Not a valid URI') + } + + const spMatches = serviceproviders.match(uri, opts) + + if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { + return spMatches + } + let claimVerificationDone = false, + claimVerificationResult, + sp, + iSp = 0, + res, + proofData, + spData + while (!claimVerificationDone && iSp < spMatches.length) { + spData = spMatches[iSp] + spData.claim.fingerprint = fingerprint + + res = null + + if (spData.customRequestHandler instanceof Function) { + proofData = await spData.customRequestHandler(spData, opts) + } else if ( + !spData.proof.useProxy || + ('proxyPolicy' in opts && !opts.useProxyWhenNeeded) + ) { + proofData = await serviceproviders.directRequestHandler(spData, opts) + } else { + proofData = await serviceproviders.proxyRequestHandler(spData, opts) + } + + if (proofData) { + claimVerificationResult = claimVerification.run(proofData, spData) + + if (claimVerificationResult.errors.length == 0) { + claimVerificationDone = true + } + } + + iSp++ + } + + if (!claimVerificationResult) { + claimVerificationResult = { + isVerified: false, + } + } + + return { + isVerified: claimVerificationResult.isVerified, + serviceproviderData: spData, + } +} + +exports.verify = verify diff --git a/test/verify.test.js b/test/verify.test.js index cdd801d..f9cf312 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -59,14 +59,14 @@ const pattern = { describe('verify', () => { it('should be a function (3 arguments)', () => { - expect(doipjs.verify).to.be.a('function') - expect(doipjs.verify).to.have.length(3) + expect(doipjs.proofs.verify).to.be.a('function') + expect(doipjs.proofs.verify).to.have.length(3) }) it('should throw an error for non-valid URIs', () => { - return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith( + return expect(doipjs.proofs.verify('noURI')).to.eventually.be.rejectedWith( 'Not a valid URI' ) - return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith( + return expect(doipjs.proofs.verify('domain.org')).to.eventually.be.rejectedWith( 'Not a valid URI' ) }) @@ -79,7 +79,7 @@ describe('verify', () => { } it(`should return a valid object for the "${spName}" service provider`, async () => { - const matches = await doipjs.verify(sp.tests[0].uri, null, { + const matches = await doipjs.proofs.verify(sp.tests[0].uri, null, { returnMatchesOnly: true, }) expect(matches).to.be.a('array')