doipjs/src/index.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

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-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-10-24 16:32:12 -06:00
if (!fingerprint) { fingerprint = null }
if (!opts) { opts = {} }
2020-10-24 06:36:22 -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
}
const spMatches = serviceproviders.match(uri, opts)
2020-10-24 06:36:22 -06:00
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches
}
2020-11-02 16:25:02 -07:00
let claimVerificationDone = false, claimVerificationResult, sp, iSp = 0, res, proofData, spData
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-10-26 14:18:17 -06:00
} else if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) {
2020-10-26 15:33:05 -06:00
proofData = await serviceproviders.directRequestHandler(spData)
2020-10-26 14:18:17 -06:00
} else {
2020-10-26 15:33:05 -06:00
proofData = await serviceproviders.proxyRequestHandler(spData)
2020-10-24 10:43:41 -06:00
}
2020-11-02 16:25:02 -07:00
if (!proofData) { continue }
claimVerificationResult = claimVerification.run(proofData, spData)
if (claimVerificationResult.errors.length == 0) {
claimVerificationDone = true
}
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
return {
2020-11-02 16:25:02 -07:00
isVerified: claimVerificationResult.isVerified,
2020-10-24 17:06:45 -06:00
matchedServiceprovider: spData ? spData.serviceprovider.name : null,
2020-10-24 16:32:12 -06:00
verificationData: spData
}
2020-10-23 14:35:53 -06:00
}
2020-10-23 17:25:01 -06:00
exports.verify = verify
exports.serviceproviders = serviceproviders
2020-10-24 16:41:06 -06:00
exports.claimVerification = claimVerification
exports.utils = utils