diff --git a/src/index.js b/src/index.js index 7c80195..4a4a865 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/test/verify.test.js b/test/verify.test.js index 92285e2..3194f7e 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -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') + }) })