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 10:43:41 -06:00
|
|
|
const bent = require('bent')
|
|
|
|
const req = bent('GET')
|
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 }
|
2020-10-24 17:02:08 -06:00
|
|
|
if (!opts) { opts = {} }
|
2020-10-24 06:36:22 -06: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-10-24 10:43:41 -06:00
|
|
|
|
2020-10-24 17:06:45 -06:00
|
|
|
let claimHasBeenVerified = false, sp, iSp = 0, res, proofData, spData = null
|
2020-10-24 10:43:41 -06:00
|
|
|
while (!claimHasBeenVerified && 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-24 17:12:57 -06:00
|
|
|
if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) {
|
2020-10-24 16:32:12 -06:00
|
|
|
res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri)
|
2020-10-24 10:43:41 -06:00
|
|
|
|
2020-10-24 16:32:12 -06:00
|
|
|
switch (spData.proof.format) {
|
2020-10-24 10:43:41 -06:00
|
|
|
case 'json':
|
|
|
|
proofData = await res.json()
|
|
|
|
break
|
|
|
|
case 'text':
|
|
|
|
proofData = await res.text()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error('No specified proof data format')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:32:12 -06:00
|
|
|
claimHasBeenVerified = claimVerification.run(proofData, spData)
|
|
|
|
|
2020-10-24 10:43:41 -06:00
|
|
|
iSp++
|
|
|
|
}
|
2020-10-24 16:32:12 -06:00
|
|
|
|
|
|
|
return {
|
|
|
|
isVerified: claimHasBeenVerified,
|
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
|
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
|