Put proofs logic in separate file

This commit is contained in:
Yarmo Mackenbach 2020-11-17 00:50:26 +01:00
parent f18acec164
commit c7ac3a9d34
3 changed files with 101 additions and 79 deletions

View file

@ -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 See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const mergeOptions = require('merge-options') const proofs = require('./proofs')
const validUrl = require('valid-url') const keys = require('./keys')
const serviceproviders = require('./serviceproviders') const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification') const claimVerification = require('./claimVerification')
const utils = require('./utils') const utils = require('./utils')
const verify = async (uri, fingerprint, opts) => { exports.proofs = proofs
if (!fingerprint) { exports.keys = keys
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.serviceproviders = serviceproviders exports.serviceproviders = serviceproviders
exports.claimVerification = claimVerification exports.claimVerification = claimVerification
exports.utils = utils exports.utils = utils

92
src/proofs.js Normal file
View file

@ -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

View file

@ -59,14 +59,14 @@ const pattern = {
describe('verify', () => { describe('verify', () => {
it('should be a function (3 arguments)', () => { it('should be a function (3 arguments)', () => {
expect(doipjs.verify).to.be.a('function') expect(doipjs.proofs.verify).to.be.a('function')
expect(doipjs.verify).to.have.length(3) expect(doipjs.proofs.verify).to.have.length(3)
}) })
it('should throw an error for non-valid URIs', () => { 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' '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' 'Not a valid URI'
) )
}) })
@ -79,7 +79,7 @@ describe('verify', () => {
} }
it(`should return a valid object for the "${spName}" service provider`, async () => { 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, returnMatchesOnly: true,
}) })
expect(matches).to.be.a('array') expect(matches).to.be.a('array')