Use request handlers

This commit is contained in:
Yarmo Mackenbach 2020-10-26 21:18:17 +01:00
parent bb58fc3614
commit 8d44b4f9bc
2 changed files with 31 additions and 16 deletions

View file

@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
const validUrl = require('valid-url')
const bent = require('bent')
const req = bent('GET')
const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification')
const utils = require('./utils')
@ -41,20 +39,12 @@ const verify = async (uri, fingerprint, opts) => {
res = null
if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) {
res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri)
switch (spData.proof.format) {
case 'json':
proofData = await res.json()
break
case 'text':
proofData = await res.text()
break
default:
throw new Error('No specified proof data format')
break
}
if (spData.customRequestHandler instanceof Function) {
proofData = spData.customRequestHandler(spData, opts)
} else if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) {
proofData = serviceproviders.directRequestHandler(spData)
} else {
proofData = serviceproviders.proxyRequestHandler(spData)
}
claimHasBeenVerified = claimVerification.run(proofData, spData)

View file

@ -13,6 +13,9 @@ 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 bent = require('bent')
const req = bent('GET')
const list = [
'dns',
'xmpp',
@ -50,6 +53,28 @@ const match = (uri, opts) => {
return matches
}
const directRequestHandler = (spData) => {
const res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri)
switch (spData.proof.format) {
case 'json':
return await res.json()
break
case 'text':
return await res.text()
break
default:
throw new Error('No specified proof data format')
break
}
}
const proxyRequestHandler = (spData) => {
return null
}
exports.list = list
exports.data = data
exports.match = match
exports.directRequestHandler = directRequestHandler
exports.proxyRequestHandler = proxyRequestHandler