mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2025-01-09 22:29:28 -07:00
93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
|
/*
|
||
|
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
|