Allow verify to match the URI to a service provider

This commit is contained in:
Yarmo Mackenbach 2020-10-24 11:45:30 +02:00
parent 2521225a9b
commit 7f24354c1d
2 changed files with 47 additions and 5 deletions

View file

@ -1,12 +1,42 @@
const validUrl = require('valid-url')
const serviceprovidersList = require('./serviceproviders').serviceprovidersList
const { serviceprovidersList, serviceproviders } = require('./serviceproviders')
const matchSp = (uri) => {
let matches = [], sp
serviceprovidersList.forEach((spName, i) => {
sp = serviceproviders[spName]
if (sp.reURI.test(uri)) {
matches.push(sp.processURI(uri))
}
})
return matches
}
const verify = (uri, fingerprint, opts) => {
if !(validUrl.isUri(uri)) {
if (!validUrl.isUri(uri)) {
throw new Error('The provided URI was not valid')
}
const spMatches = matchSp(uri)
if (opts.returnMatchesOnly) {
return spMatches
}
// let claimHasBeenVerified = false
// let iSp = 0, sp
// while (!claimHasBeenVerified) {
//
// if (!sp.reURI.test(uri)) {
// continue;
// }
//
// iSP++
// }
}
exports.verify = verify
exports.serviceproviders = require('./serviceproviders').serviceproviders
exports.serviceproviders = serviceproviders
exports.serviceprovidersList = serviceprovidersList

View file

@ -32,9 +32,21 @@ const assert = require('chai').assert
const request = require('supertest')
const doipjs = require('../src')
describe('verify', function() {
it('should be a function (3 arguments)', function() {
describe('verify', () => {
it('should be a function (3 arguments)', () => {
expect(doipjs.verify).to.be.a('function')
expect(doipjs.verify).to.have.length(3)
})
it('should match "dns:domain.org" to the DNS service provider', () => {
const matches = doipjs.verify('dns:domain.org', null, {returnMatchesOnly: true})
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].type).to.be.equal('domain')
})
it('should match "xmpp:alice@domain.org" to the XMPP service provider', () => {
const matches = doipjs.verify('xmpp:alice@domain.org', null, {returnMatchesOnly: true})
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].type).to.be.equal('xmpp')
})
})