diff --git a/src/serviceproviders.js b/src/serviceproviders.js index d574185..72fee94 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -16,9 +16,11 @@ limitations under the License. exports.serviceprovidersList = [ 'dns', 'xmpp', + 'twitter', ] exports.serviceproviders = { dns: require('./serviceproviders/dns'), - xmpp: require('./serviceproviders/xmpp') + xmpp: require('./serviceproviders/xmpp'), + twitter: require('./serviceproviders/twitter'), } diff --git a/src/serviceproviders/twitter.js b/src/serviceproviders/twitter.js new file mode 100644 index 0000000..5747f8d --- /dev/null +++ b/src/serviceproviders/twitter.js @@ -0,0 +1,58 @@ +/* +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 reURI = /^https:\/\/twitter\.com\/(.*)\/status\/([0-9]*)(?:\?.*)?/ + +const processURI = (uri, opts) => { + if (!opts) { opts = {} } + const match = uri.match(reURI) + + return { + type: "twitter", + profile: { + display: `@${match[1]}`, + uri: `https://twitter.com/${match[1]}` + }, + proof: { + uri: uri, + fetch: 'DOIP_PROXY_SERVER_DOMAIN' in opts + ? `${opts.DOIP_PROXY_SERVER_DOMAIN}/server/verify/twitter +?tweetId=${encodeURIComponent(match[2])} +&account=${encodeURIComponent(match[1])} +&fingerprint=${fingerprint}` + : null + }, + qr: null + } +} + +const tests = [ + { + uri: 'https://twitter.com/alice/status/1234567890123456789', + shouldMatch: true + }, + { + uri: 'https://twitter.com/alice/status/1234567890123456789/', + shouldMatch: true + }, + { + uri: 'https://domain.org/alice/status/1234567890123456789', + shouldMatch: false + } +] + +exports.reURI = reURI +exports.processURI = processURI +exports.tests = tests diff --git a/test/verify.test.js b/test/verify.test.js index 45018a3..2126e0c 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -39,4 +39,10 @@ describe('verify', () => { expect(matches).to.be.length(1) expect(matches[0].type).to.be.equal('xmpp') }) + it('should match "https://twitter.com/alice/status/1234567890123456789" to the Twitter service provider', () => { + const matches = doipjs.verify('https://twitter.com/alice/status/1234567890123456789', null, {returnMatchesOnly: true}) + expect(matches).to.be.a('array') + expect(matches).to.be.length(1) + expect(matches[0].type).to.be.equal('twitter') + }) })