mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-22 14:39:28 -07:00
Put proofs logic in separate file
This commit is contained in:
parent
f18acec164
commit
c7ac3a9d34
3 changed files with 101 additions and 79 deletions
78
src/index.js
78
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
|
||||
|
|
92
src/proofs.js
Normal file
92
src/proofs.js
Normal 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
|
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue