2020-10-24 03:57:27 -06:00
|
|
|
/*
|
2020-10-24 07:11:14 -06:00
|
|
|
Copyright 2020 Yarmo Mackenbach
|
2020-10-24 03:57:27 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
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
|
2020-10-24 03:57:27 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-10-24 03:57:27 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
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.
|
2020-10-24 03:57:27 -06:00
|
|
|
*/
|
2020-11-05 17:16:15 -07:00
|
|
|
const mergeOptions = require('merge-options')
|
2020-10-24 03:19:35 -06:00
|
|
|
const validUrl = require('valid-url')
|
2020-10-24 11:10:28 -06:00
|
|
|
const serviceproviders = require('./serviceproviders')
|
2020-10-24 16:32:12 -06:00
|
|
|
const claimVerification = require('./claimVerification')
|
2020-10-24 16:41:06 -06:00
|
|
|
const utils = require('./utils')
|
2020-10-23 14:35:53 -06:00
|
|
|
|
2020-10-24 10:43:41 -06:00
|
|
|
const verify = async (uri, fingerprint, opts) => {
|
2020-11-07 18:07:02 -07:00
|
|
|
if (!fingerprint) {
|
|
|
|
fingerprint = null
|
|
|
|
}
|
|
|
|
if (!opts) {
|
|
|
|
opts = {}
|
|
|
|
}
|
2020-10-24 06:36:22 -06:00
|
|
|
|
2020-11-05 17:16:15 -07:00
|
|
|
const defaultOpts = {
|
|
|
|
returnMatchesOnly: false,
|
|
|
|
proxyPolicy: 'adaptive',
|
2020-11-07 18:07:02 -07:00
|
|
|
doipProxyHostname: 'proxy.keyoxide.org',
|
2020-11-05 16:43:32 -07:00
|
|
|
}
|
2020-11-05 17:16:15 -07:00
|
|
|
opts = mergeOptions(defaultOpts, opts)
|
2020-11-05 16:43:32 -07:00
|
|
|
|
2020-10-24 03:45:30 -06:00
|
|
|
if (!validUrl.isUri(uri)) {
|
2020-10-24 03:56:30 -06:00
|
|
|
throw new Error('Not a valid URI')
|
2020-10-24 03:19:35 -06:00
|
|
|
}
|
2020-10-24 03:45:30 -06:00
|
|
|
|
2020-10-24 17:02:08 -06:00
|
|
|
const spMatches = serviceproviders.match(uri, opts)
|
2020-10-24 03:45:30 -06:00
|
|
|
|
2020-10-24 06:36:22 -06:00
|
|
|
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
|
2020-10-24 03:45:30 -06:00
|
|
|
return spMatches
|
|
|
|
}
|
2020-11-07 18:07:02 -07:00
|
|
|
let claimVerificationDone = false,
|
|
|
|
claimVerificationResult,
|
|
|
|
sp,
|
|
|
|
iSp = 0,
|
|
|
|
res,
|
|
|
|
proofData,
|
|
|
|
spData
|
2020-11-02 16:25:02 -07:00
|
|
|
while (!claimVerificationDone && iSp < spMatches.length) {
|
2020-10-24 16:32:12 -06:00
|
|
|
spData = spMatches[iSp]
|
|
|
|
spData.claim.fingerprint = fingerprint
|
2020-10-24 10:43:41 -06:00
|
|
|
|
|
|
|
res = null
|
|
|
|
|
2020-10-26 14:18:17 -06:00
|
|
|
if (spData.customRequestHandler instanceof Function) {
|
2020-10-26 15:33:05 -06:00
|
|
|
proofData = await spData.customRequestHandler(spData, opts)
|
2020-11-07 18:07:02 -07:00
|
|
|
} else if (
|
|
|
|
!spData.proof.useProxy ||
|
|
|
|
('proxyPolicy' in opts && !opts.useProxyWhenNeeded)
|
|
|
|
) {
|
2020-11-05 16:43:32 -07:00
|
|
|
proofData = await serviceproviders.directRequestHandler(spData, opts)
|
2020-10-26 14:18:17 -06:00
|
|
|
} else {
|
2020-11-05 16:43:32 -07:00
|
|
|
proofData = await serviceproviders.proxyRequestHandler(spData, opts)
|
2020-10-24 10:43:41 -06:00
|
|
|
}
|
2020-11-05 03:33:03 -07:00
|
|
|
|
2020-11-03 14:50:35 -07:00
|
|
|
if (proofData) {
|
|
|
|
claimVerificationResult = claimVerification.run(proofData, spData)
|
2020-10-24 10:43:41 -06:00
|
|
|
|
2020-11-03 14:50:35 -07:00
|
|
|
if (claimVerificationResult.errors.length == 0) {
|
|
|
|
claimVerificationDone = true
|
|
|
|
}
|
2020-11-02 16:25:02 -07:00
|
|
|
}
|
2020-10-24 16:32:12 -06:00
|
|
|
|
2020-10-24 10:43:41 -06:00
|
|
|
iSp++
|
|
|
|
}
|
2020-10-24 16:32:12 -06:00
|
|
|
|
2020-11-03 14:50:35 -07:00
|
|
|
if (!claimVerificationResult) {
|
|
|
|
claimVerificationResult = {
|
2020-11-07 18:07:02 -07:00
|
|
|
isVerified: false,
|
2020-11-03 14:50:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:32:12 -06:00
|
|
|
return {
|
2020-11-02 16:25:02 -07:00
|
|
|
isVerified: claimVerificationResult.isVerified,
|
2020-11-07 18:07:02 -07:00
|
|
|
serviceproviderData: spData,
|
2020-10-24 16:32:12 -06:00
|
|
|
}
|
2020-10-23 14:35:53 -06:00
|
|
|
}
|
2020-10-23 17:25:01 -06:00
|
|
|
|
|
|
|
exports.verify = verify
|
2020-10-24 03:45:30 -06:00
|
|
|
exports.serviceproviders = serviceproviders
|
2020-10-24 16:41:06 -06:00
|
|
|
exports.claimVerification = claimVerification
|
|
|
|
exports.utils = utils
|