diff --git a/license-checker-config.json b/license-checker-config.json index 3c6fb89..3cc172c 100644 --- a/license-checker-config.json +++ b/license-checker-config.json @@ -1,11 +1,11 @@ { - "ignore": ".licenseignore", - "license": "LICENSE.HEADER", - "licenseFormats": { - "dotfile|licenseignore|gitignore|yml": { - "eachLine": { - "prepend": "# " - } - } + "ignore": ".licenseignore", + "license": "LICENSE.HEADER", + "licenseFormats": { + "dotfile|licenseignore|gitignore|yml": { + "eachLine": { + "prepend": "# " + } } + } } diff --git a/src/claimVerification.js b/src/claimVerification.js index f6e0cff..ec2ef38 100644 --- a/src/claimVerification.js +++ b/src/claimVerification.js @@ -18,15 +18,20 @@ const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => { switch (checkRelation) { default: case 'contains': - re = new RegExp(checkClaim.replace('[', '\\[').replace(']', '\\]'), "gi") + re = new RegExp( + checkClaim.replace('[', '\\[').replace(']', '\\]'), + 'gi' + ) res.isVerified = re.test(proofData.replace(/\r?\n|\r/, '')) break case 'equals': - res.isVerified = proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase() + res.isVerified = + proofData.replace(/\r?\n|\r/, '').toLowerCase() == + checkClaim.toLowerCase() break case 'oneOf': - re = new RegExp(checkClaim, "gi") - res.isVerified = re.test(proofData.join("|")) + re = new RegExp(checkClaim, 'gi') + res.isVerified = re.test(proofData.join('|')) break } return res @@ -34,27 +39,42 @@ const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => { try { checkPath[0] in proofData - } catch(e) { + } catch (e) { res.errors.push('err_data_structure_incorrect') return res } - res = runOnJson(res, proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation) + res = runOnJson( + res, + proofData[checkPath[0]], + checkPath.slice(1), + checkClaim, + checkRelation + ) return res } const run = (proofData, spData) => { let res = { isVerified: false, - errors: [] + errors: [], } switch (spData.proof.format) { case 'json': - res = runOnJson(res, proofData, spData.claim.path, utils.generateClaim(spData.claim.fingerprint, spData.claim.format), spData.claim.relation) + res = runOnJson( + res, + proofData, + spData.claim.path, + utils.generateClaim(spData.claim.fingerprint, spData.claim.format), + spData.claim.relation + ) break case 'text': - re = new RegExp(utils.generateClaim(spData.claim.fingerprint, spData.claim.format), "gi") + re = new RegExp( + utils.generateClaim(spData.claim.fingerprint, spData.claim.format), + 'gi' + ) res = re.test(proofData.replace(/\r?\n|\r/, '')) break } diff --git a/src/index.js b/src/index.js index ec6cd65..894a580 100644 --- a/src/index.js +++ b/src/index.js @@ -20,13 +20,17 @@ const claimVerification = require('./claimVerification') const utils = require('./utils') const verify = async (uri, fingerprint, opts) => { - if (!fingerprint) { fingerprint = null } - if (!opts) { opts = {} } + if (!fingerprint) { + fingerprint = null + } + if (!opts) { + opts = {} + } const defaultOpts = { returnMatchesOnly: false, proxyPolicy: 'adaptive', - doipProxyHostname: 'proxy.keyoxide.org' + doipProxyHostname: 'proxy.keyoxide.org', } opts = mergeOptions(defaultOpts, opts) @@ -39,7 +43,13 @@ const verify = async (uri, fingerprint, opts) => { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { return spMatches } - let claimVerificationDone = false, claimVerificationResult, sp, iSp = 0, res, proofData, spData + let claimVerificationDone = false, + claimVerificationResult, + sp, + iSp = 0, + res, + proofData, + spData while (!claimVerificationDone && iSp < spMatches.length) { spData = spMatches[iSp] spData.claim.fingerprint = fingerprint @@ -48,7 +58,10 @@ const verify = async (uri, fingerprint, opts) => { if (spData.customRequestHandler instanceof Function) { proofData = await spData.customRequestHandler(spData, opts) - } else if (!spData.proof.useProxy || 'proxyPolicy' in opts && !opts.useProxyWhenNeeded) { + } else if ( + !spData.proof.useProxy || + ('proxyPolicy' in opts && !opts.useProxyWhenNeeded) + ) { proofData = await serviceproviders.directRequestHandler(spData, opts) } else { proofData = await serviceproviders.proxyRequestHandler(spData, opts) @@ -67,13 +80,13 @@ const verify = async (uri, fingerprint, opts) => { if (!claimVerificationResult) { claimVerificationResult = { - isVerified: false + isVerified: false, } } return { isVerified: claimVerificationResult.isVerified, - serviceproviderData: spData + serviceproviderData: spData, } } diff --git a/src/serviceproviders.js b/src/serviceproviders.js index d36bf04..00d66fb 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -52,7 +52,8 @@ const data = { } const match = (uri, opts) => { - let matches = [], sp + let matches = [], + sp list.forEach((spName, i) => { sp = data[spName] @@ -65,7 +66,11 @@ const match = (uri, opts) => { } const directRequestHandler = async (spData, opts) => { - const res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri, null, { Accept: 'application/json' }) + const res = await req( + spData.proof.fetch ? spData.proof.fetch : spData.proof.uri, + null, + { Accept: 'application/json' } + ) switch (spData.proof.format) { case 'json': @@ -82,7 +87,11 @@ const directRequestHandler = async (spData, opts) => { const proxyRequestHandler = async (spData, opts) => { const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri - const res = await req(utils.generateProxyURL(spData.proof.format, url, opts), null, { Accept: 'application/json' }) + const res = await req( + utils.generateProxyURL(spData.proof.format, url, opts), + null, + { Accept: 'application/json' } + ) const json = await res.json() return json.content } diff --git a/src/serviceproviders/devto.js b/src/serviceproviders/devto.js index 06a7b7d..dcc451d 100644 --- a/src/serviceproviders/devto.js +++ b/src/serviceproviders/devto.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/dev\.to\/(.*)\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'devto' + name: 'devto', }, profile: { display: match[1], uri: `https://dev.to/${match[1]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://dev.to/api/articles/${match[1]}/${match[2]}`, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['body_markdown'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://dev.to/alice/post', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://dev.to/alice/post/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice/post', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/discourse.js b/src/serviceproviders/discourse.js index dc90852..74e23ff 100644 --- a/src/serviceproviders/discourse.js +++ b/src/serviceproviders/discourse.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/(.*)\/u\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'discourse' + name: 'discourse', }, profile: { display: `${match[2]}@${match[1]}`, uri: uri, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://${match[1]}/u/${match[2]}.json`, useProxy: true, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['user', 'bio_raw'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://domain.org/u/alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/u/alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/dns.js b/src/serviceproviders/dns.js index be1f9b0..ea5aa05 100644 --- a/src/serviceproviders/dns.js +++ b/src/serviceproviders/dns.js @@ -20,7 +20,7 @@ const utils = require('../utils') const reURI = /^dns:([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/ const customRequestHandler = async (spData, opts) => { - if (('resolveTxt' in dns)) { + if ('resolveTxt' in dns) { const prom = async () => { return new Promise((resolve, reject) => { dns.resolveTxt(spData.profile.display, (err, records) => { @@ -32,59 +32,63 @@ const customRequestHandler = async (spData, opts) => { return { hostname: spData.profile.display, records: { - txt: await prom() - } + txt: await prom(), + }, } } else { - const res = await req(spData.proof.uri, null, { Accept: 'application/json' }) + const res = await req(spData.proof.uri, null, { + Accept: 'application/json', + }) const json = await res.json() return json } } const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'dns' + name: 'dns', }, profile: { display: match[1], uri: `https://${match[1]}`, - qr: null + qr: null, }, proof: { uri: utils.generateProxyURL('dns', match[1]), fetch: null, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'uri', path: ['records', 'txt'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: customRequestHandler + customRequestHandler: customRequestHandler, } } const tests = [ { uri: 'dns:domain.org', - shouldMatch: true + shouldMatch: true, }, { uri: 'dns:domain.org?type=TXT', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/fediverse.js b/src/serviceproviders/fediverse.js index da62f2a..c193247 100644 --- a/src/serviceproviders/fediverse.js +++ b/src/serviceproviders/fediverse.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/(.*)\/users\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'fediverse' + name: 'fediverse', }, profile: { display: `@${match[2]}@${match[1]}`, uri: uri, - qr: null + qr: null, }, proof: { uri: uri, fetch: null, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'fingerprint', path: ['summary'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://domain.org/users/alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/users/alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/gitea.js b/src/serviceproviders/gitea.js index 118c488..c4cb915 100644 --- a/src/serviceproviders/gitea.js +++ b/src/serviceproviders/gitea.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/(.*)\/(.*)\/gitea_proof\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'gitea' + name: 'gitea', }, profile: { display: `${match[2]}@${match[1]}`, uri: `https://${match[1]}/${match[2]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://${match[1]}/api/v1/repos/${match[2]}/gitea_proof`, useProxy: true, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['description'], - relation: 'equals' + relation: 'equals', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://domain.org/alice/gitea_proof', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice/gitea_proof/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice/other_proof', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/github.js b/src/serviceproviders/github.js index 2096697..9b1f6d3 100644 --- a/src/serviceproviders/github.js +++ b/src/serviceproviders/github.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/gist\.github\.com\/(.*)\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'github' + name: 'github', }, profile: { display: match[1], uri: `https://github.com/${match[1]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://api.github.com/gists/${match[2]}`, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['files', 'openpgp.md', 'content'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://gist.github.com/Alice/123456789', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://gist.github.com/Alice/123456789/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/Alice/123456789', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/gitlab.js b/src/serviceproviders/gitlab.js index 16c91c6..b412ace 100644 --- a/src/serviceproviders/gitlab.js +++ b/src/serviceproviders/gitlab.js @@ -25,66 +25,68 @@ const customRequestHandler = async (spData, opts) => { const resUser = await req(urlUser, 'json', { Accept: 'application/json' }) const jsonUser = await resUser.json() - const user = jsonUser.find(user => user.username === match[2]) + const user = jsonUser.find((user) => user.username === match[2]) if (!user) { - throw new Error(`No user with username ${match[2]}`); + throw new Error(`No user with username ${match[2]}`) } const urlProject = `https://${match[1]}/api/v4/users/${user.id}/projects` const resProject = await req(urlProject, {}, { Accept: 'application/json' }) const jsonProject = await resProject.json() - const project = jsonProject.find(proj => proj.path === 'gitlab_proof') + const project = jsonProject.find((proj) => proj.path === 'gitlab_proof') if (!project) { - throw new Error(`No project at ${spData.proof.uri}`); + throw new Error(`No project at ${spData.proof.uri}`) } return project } const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'gitlab' + name: 'gitlab', }, profile: { display: `${match[2]}@${match[1]}`, uri: `https://${match[1]}/${match[2]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: null, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['description'], - relation: 'equals' + relation: 'equals', }, - customRequestHandler: customRequestHandler + customRequestHandler: customRequestHandler, } } const tests = [ { uri: 'https://gitlab.domain.org/alice/gitlab_proof', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://gitlab.domain.org/alice/gitlab_proof/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice/other_proof', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/hackernews.js b/src/serviceproviders/hackernews.js index 2783229..b74762e 100644 --- a/src/serviceproviders/hackernews.js +++ b/src/serviceproviders/hackernews.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/news\.ycombinator\.com\/user\?id=(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'hackernews' + name: 'hackernews', }, profile: { display: match[1], uri: uri, - qr: null + qr: null, }, proof: { uri: `https://hacker-news.firebaseio.com/v0/user/${match[1]}.json`, fetch: null, useProxy: true, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'uri', path: ['about'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://news.ycombinator.com/user?id=Alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://news.ycombinator.com/user?id=Alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/user?id=Alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/liberapay.js b/src/serviceproviders/liberapay.js index 4d9cad9..7e7d359 100644 --- a/src/serviceproviders/liberapay.js +++ b/src/serviceproviders/liberapay.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/liberapay\.com\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'liberapay' + name: 'liberapay', }, profile: { display: match[1], uri: uri, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://liberapay.com/${match[1]}/public.json`, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['statements', 'content'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://liberapay.com/alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://liberapay.com/alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/lobsters.js b/src/serviceproviders/lobsters.js index 4dd65b8..8f0399b 100644 --- a/src/serviceproviders/lobsters.js +++ b/src/serviceproviders/lobsters.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/lobste\.rs\/u\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'lobsters' + name: 'lobsters', }, profile: { display: match[1], uri: uri, - qr: null + qr: null, }, proof: { uri: `https://lobste.rs/u/${match[1]}.json`, fetch: null, useProxy: true, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['about'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://lobste.rs/u/Alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://lobste.rs/u/Alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/u/Alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/mastodon.js b/src/serviceproviders/mastodon.js index 47e4334..f84b70f 100644 --- a/src/serviceproviders/mastodon.js +++ b/src/serviceproviders/mastodon.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/(.*)\/@(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'mastodon' + name: 'mastodon', }, profile: { display: `@${match[2]}@${match[1]}`, uri: uri, - qr: null + qr: null, }, proof: { uri: uri, fetch: null, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'fingerprint', path: ['attachment', 'value'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://domain.org/@alice', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/@alice/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/reddit.js b/src/serviceproviders/reddit.js index 81dc6c9..88cb500 100644 --- a/src/serviceproviders/reddit.js +++ b/src/serviceproviders/reddit.js @@ -16,56 +16,58 @@ limitations under the License. const reURI = /^https:\/\/(?:www\.)?reddit\.com\/user\/(.*)\/comments\/(.*)\/(.*)\/?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'reddit' + name: 'reddit', }, profile: { display: match[1], uri: `https://www.reddit.com/user/${match[1]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://www.reddit.com/user/${match[1]}/comments/${match[2]}.json`, useProxy: true, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: ['data', 'children', 'data', 'selftext'], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://www.reddit.com/user/Alice/comments/123456/post', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://www.reddit.com/user/Alice/comments/123456/post/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://reddit.com/user/Alice/comments/123456/post', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://reddit.com/user/Alice/comments/123456/post/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/user/Alice/comments/123456/post', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/twitter.js b/src/serviceproviders/twitter.js index e6fb4b2..7d9f015 100644 --- a/src/serviceproviders/twitter.js +++ b/src/serviceproviders/twitter.js @@ -16,48 +16,50 @@ limitations under the License. const reURI = /^https:\/\/twitter\.com\/(.*)\/status\/([0-9]*)(?:\?.*)?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'web', - name: 'twitter' + name: 'twitter', }, profile: { display: `@${match[1]}`, uri: `https://twitter.com/${match[1]}`, - qr: null + qr: null, }, proof: { uri: uri, fetch: `https://mobile.twitter.com/${match[1]}/status/${match[2]}`, useProxy: false, - format: 'text' + format: 'text', }, claim: { fingerprint: null, format: 'message', path: [], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'https://twitter.com/alice/status/1234567890123456789', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://twitter.com/alice/status/1234567890123456789/', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org/alice/status/1234567890123456789', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/serviceproviders/xmpp.js b/src/serviceproviders/xmpp.js index a0a0524..477e863 100644 --- a/src/serviceproviders/xmpp.js +++ b/src/serviceproviders/xmpp.js @@ -16,50 +16,53 @@ limitations under the License. const reURI = /^xmpp:([a-zA-Z0-9\.\-\_]*)@([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/ const processURI = (uri, opts) => { - if (!opts) { opts = {} } + if (!opts) { + opts = {} + } const match = uri.match(reURI) return { serviceprovider: { type: 'communication', - name: 'xmpp' + name: 'xmpp', }, profile: { display: `${match[1]}@${match[2]}`, uri: uri, - qr: uri + qr: uri, }, proof: { - uri: 'xmppVcardServerDomain' in opts - ? `https://${opts.xmppVcardServerDomain}/api/vcard/${match[1]}@${match[2]}/DESC` - : null, + uri: + 'xmppVcardServerDomain' in opts + ? `https://${opts.xmppVcardServerDomain}/api/vcard/${match[1]}@${match[2]}/DESC` + : null, fetch: null, useProxy: false, - format: 'json' + format: 'json', }, claim: { fingerprint: null, format: 'message', path: [], - relation: 'contains' + relation: 'contains', }, - customRequestHandler: null + customRequestHandler: null, } } const tests = [ { uri: 'xmpp:alice@domain.org', - shouldMatch: true + shouldMatch: true, }, { uri: 'xmpp:alice@domain.org?omemo-sid-123456789=A1B2C3D4E5F6G7H8I9', - shouldMatch: true + shouldMatch: true, }, { uri: 'https://domain.org', - shouldMatch: false - } + shouldMatch: false, + }, ] exports.reURI = reURI diff --git a/src/utils.js b/src/utils.js index 00bd60f..9050f7d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,19 +1,23 @@ const generateProxyURL = (type, url, opts) => { - if (!opts || !opts.doipProxyHostname) { return null } - return `https://${opts.doipProxyHostname}/api/1/get/${type}/${encodeURIComponent(url)}` + if (!opts || !opts.doipProxyHostname) { + return null + } + return `https://${ + opts.doipProxyHostname + }/api/1/get/${type}/${encodeURIComponent(url)}` } const generateClaim = (fingerprint, format) => { switch (format) { case 'uri': return `openpgp4fpr:${fingerprint}` - break; + break case 'message': return `[Verifying my OpenPGP key: openpgp4fpr:${fingerprint}]` - break; + break case 'fingerprint': return fingerprint - break; + break default: throw new Error('No valid claim format') } diff --git a/test/serviceproviders.test.js b/test/serviceproviders.test.js index df4e606..3c509c0 100644 --- a/test/serviceproviders.test.js +++ b/test/serviceproviders.test.js @@ -37,14 +37,15 @@ doipjs.serviceproviders.list.forEach((sp, i) => { doipjs.serviceproviders.data[sp].tests.forEach((test, j) => { if (test.shouldMatch) { it(`should match "${test.uri}"`, () => { - expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.true + expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be + .true }) } else { it(`should not match "${test.uri}"`, () => { - expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.false + expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be + .false }) } }) - }) }) diff --git a/test/utils.test.js b/test/utils.test.js index 4aa86cd..fb785e8 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -24,12 +24,18 @@ describe('utils.generateClaim', () => { expect(doipjs.utils.generateClaim).to.have.length(2) }) it('should generate a correct "uri" claim', () => { - expect(doipjs.utils.generateClaim('123456789', 'uri')).to.equal('openpgp4fpr:123456789') + expect(doipjs.utils.generateClaim('123456789', 'uri')).to.equal( + 'openpgp4fpr:123456789' + ) }) it('should generate a correct "message" claim', () => { - expect(doipjs.utils.generateClaim('123456789', 'message')).to.equal('[Verifying my OpenPGP key: openpgp4fpr:123456789]') + expect(doipjs.utils.generateClaim('123456789', 'message')).to.equal( + '[Verifying my OpenPGP key: openpgp4fpr:123456789]' + ) }) it('should generate a correct "fingerprint" claim', () => { - expect(doipjs.utils.generateClaim('123456789', 'fingerprint')).to.equal('123456789') + expect(doipjs.utils.generateClaim('123456789', 'fingerprint')).to.equal( + '123456789' + ) }) }) diff --git a/test/verify.test.js b/test/verify.test.js index e43c400..cdd801d 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -17,7 +17,7 @@ const chai = require('chai') const expect = chai.expect const chaiMatchPattern = require('chai-match-pattern') chai.use(chaiMatchPattern) -chai.use(require("chai-as-promised")) +chai.use(require('chai-as-promised')) const _ = chaiMatchPattern.getLodashModule() const doipjs = require('../src') @@ -25,26 +25,36 @@ const doipjs = require('../src') const pattern = { serviceprovider: { type: _.isString, - name: _.isString + name: _.isString, }, profile: { display: _.isString, uri: _.isString, - qr: (x) => { return _.isString(x) || _.isNull(x) } + qr: (x) => { + return _.isString(x) || _.isNull(x) + }, }, proof: { - uri: (x) => { return _.isString(x) || _.isNull(x) }, - fetch: (x) => { return _.isString(x) || _.isNull(x) }, + uri: (x) => { + return _.isString(x) || _.isNull(x) + }, + fetch: (x) => { + return _.isString(x) || _.isNull(x) + }, useProxy: _.isBoolean, - format: _.isString + format: _.isString, }, claim: { - fingerprint: (x) => { return _.isString(x) || _.isNull(x) }, + fingerprint: (x) => { + return _.isString(x) || _.isNull(x) + }, format: _.isString, path: _.isArray, - relation: _.isString + relation: _.isString, + }, + customRequestHandler: (x) => { + return _.isFunction(x) || _.isNull(x) || _.isUndefined(x) }, - customRequestHandler: (x) => { return _.isFunction(x) || _.isNull(x) || _.isUndefined(x) } } describe('verify', () => { @@ -53,17 +63,25 @@ describe('verify', () => { expect(doipjs.verify).to.have.length(3) }) it('should throw an error for non-valid URIs', () => { - return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith('Not a valid URI') - return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith('Not a valid URI') + return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith( + 'Not a valid URI' + ) + return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith( + 'Not a valid URI' + ) }) doipjs.serviceproviders.list.forEach((spName, i) => { const sp = doipjs.serviceproviders.data[spName] - if (sp.tests.length == 0) { return } + if (sp.tests.length == 0) { + return + } it(`should return a valid object for the "${spName}" service provider`, async () => { - const matches = await doipjs.verify(sp.tests[0].uri, null, {returnMatchesOnly: true}) + const matches = await doipjs.verify(sp.tests[0].uri, null, { + returnMatchesOnly: true, + }) expect(matches).to.be.a('array') expect(matches).to.be.length.above(0) expect(matches[0].serviceprovider.name).to.be.equal(spName)