diff --git a/.drone.yml b/.drone.yml index d1f99de..c1f1bf9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,55 +1,53 @@ --- - kind: pipeline name: build-stable-proxy-container steps: - - name: build stable proxy container - image: plugins/docker - settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - dockerfile: docker/proxy/Dockerfile - repo: keyoxide/doip-proxy - tags: stable - - name: build tag proxy container - image: plugins/docker - settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - dockerfile: docker/proxy/Dockerfile - repo: keyoxide/doip-proxy - auto_tag: true + - name: build stable proxy container + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + dockerfile: docker/proxy/Dockerfile + repo: keyoxide/doip-proxy + tags: stable + - name: build tag proxy container + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + dockerfile: docker/proxy/Dockerfile + repo: keyoxide/doip-proxy + auto_tag: true trigger: - branch: - - main - event: - - tag + branch: + - main + event: + - tag --- - kind: pipeline name: build-dev-proxy-container steps: - - name: build dev proxy container - image: plugins/docker - settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - dockerfile: docker/proxy/Dockerfile - repo: keyoxide/doip-proxy - tags: dev + - name: build dev proxy container + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + dockerfile: docker/proxy/Dockerfile + repo: keyoxide/doip-proxy + tags: dev trigger: - branch: - - main - event: - - push + branch: + - main + event: + - push diff --git a/src/claim.js b/src/claim.js index d47b21d..e8f9755 100644 --- a/src/claim.js +++ b/src/claim.js @@ -45,7 +45,7 @@ class Claim { this.dataMatches = data.dataMatches this.verification = data.verification break - + default: throw new Error('Invalid claim version') break @@ -101,7 +101,7 @@ class Claim { } /** - * Get the candidate claim definitions the URI matched against + * Get the candidate claim definitions the URI matched against * @function * @returns {object} */ @@ -131,7 +131,9 @@ class Claim { */ set uri(uri) { if (this.state !== E.ClaimState.INIT) { - throw new Error('Cannot change the URI, this claim has already been matched') + throw new Error( + 'Cannot change the URI, this claim has already been matched' + ) } // Verify validity of URI if (uri && !validUrl.isUri(uri)) { @@ -150,7 +152,9 @@ class Claim { */ set fingerprint(fingerprint) { if (this.state === E.ClaimState.VERIFIED) { - throw new Error('Cannot change the fingerprint, this claim has already been verified') + throw new Error( + 'Cannot change the fingerprint, this claim has already been verified' + ) } this.fingerprint = fingerprint } @@ -161,7 +165,7 @@ class Claim { * @param anything - Anything will throw an error */ set state(anything) { - throw new Error('Cannot change a claim\'s state') + throw new Error("Cannot change a claim's state") } /** @@ -170,7 +174,7 @@ class Claim { * @param anything - Anything will throw an error */ set dataMatches(anything) { - throw new Error('Cannot change a claim\'s dataMatches') + throw new Error("Cannot change a claim's dataMatches") } /** @@ -179,7 +183,7 @@ class Claim { * @param anything - Anything will throw an error */ set verification(anything) { - throw new Error('Cannot change a claim\'s verification data') + throw new Error("Cannot change a claim's verification data") } /** @@ -210,9 +214,7 @@ class Claim { this.dataMatches.push(candidate) } else { // Set a single candidate and stop - this.dataMatches = [ - candidate - ] + this.dataMatches = [candidate] return false } @@ -249,7 +251,7 @@ class Claim { // For each match for (let index = 0; index < this.dataMatches.length; index++) { const claimData = this.dataMatches[index] - + let verificationResult, proofData = null, proofFetchError @@ -262,7 +264,11 @@ class Claim { if (proofData) { // Run the verification process - verificationResult = verifications.run(proofData.result, claimData, this.fingerprint) + verificationResult = verifications.run( + proofData.result, + claimData, + this.fingerprint + ) verificationResult.proof = { fetcher: proofData.fetcher, viaProxy: proofData.viaProxy, @@ -285,9 +291,7 @@ class Claim { if (verificationResult.completed) { // Store the result, keep a single match and stop verifying this.verification = verificationResult - this.dataMatches = [ - claimData - ] + this.dataMatches = [claimData] index = this.dataMatches.length } } @@ -308,12 +312,12 @@ class Claim { } if (this.dataMatches.length === 0) { throw new Error('The claim has no matches') - } + } return this.dataMatches.length > 1 || this.dataMatches[0].match.isAmbiguous } /** - * Get a JSON representation of the Claim object. Useful when transferring + * Get a JSON representation of the Claim object. Useful when transferring * data between instances/machines. * @function * @returns {object} @@ -330,4 +334,4 @@ class Claim { } } -module.exports = Claim \ No newline at end of file +module.exports = Claim diff --git a/src/claimDefinitions/devto.js b/src/claimDefinitions/devto.js index 09522e2..fa77d34 100644 --- a/src/claimDefinitions/devto.js +++ b/src/claimDefinitions/devto.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://dev.to/api/articles/${match[1]}/${match[2]}`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/discourse.js b/src/claimDefinitions/discourse.js index 4f63374..8945de4 100644 --- a/src/claimDefinitions/discourse.js +++ b/src/claimDefinitions/discourse.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://${match[1]}/u/${match[2]}.json`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/dns.js b/src/claimDefinitions/dns.js index c6eb96b..dcf828f 100644 --- a/src/claimDefinitions/dns.js +++ b/src/claimDefinitions/dns.js @@ -42,8 +42,8 @@ const processURI = (uri) => { format: E.ProofFormat.JSON, data: { domain: match[1], - } - } + }, + }, }, claim: { format: E.ClaimFormat.URI, diff --git a/src/claimDefinitions/fediverse.js b/src/claimDefinitions/fediverse.js index da1cdb1..434af1a 100644 --- a/src/claimDefinitions/fediverse.js +++ b/src/claimDefinitions/fediverse.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: uri, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.FINGERPRINT, diff --git a/src/claimDefinitions/gitea.js b/src/claimDefinitions/gitea.js index fc8235f..743236d 100644 --- a/src/claimDefinitions/gitea.js +++ b/src/claimDefinitions/gitea.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://${match[1]}/api/v1/repos/${match[2]}/gitea_proof`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/github.js b/src/claimDefinitions/github.js index 1676b62..57327a2 100644 --- a/src/claimDefinitions/github.js +++ b/src/claimDefinitions/github.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://api.github.com/gists/${match[2]}`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/gitlab.js b/src/claimDefinitions/gitlab.js index e4e9007..79c5d31 100644 --- a/src/claimDefinitions/gitlab.js +++ b/src/claimDefinitions/gitlab.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { domain: match[1], username: match[2], - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/hackernews.js b/src/claimDefinitions/hackernews.js index 67d8ef4..efe7a84 100644 --- a/src/claimDefinitions/hackernews.js +++ b/src/claimDefinitions/hackernews.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://hacker-news.firebaseio.com/v0/user/${match[1]}.json`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.URI, diff --git a/src/claimDefinitions/index.js b/src/claimDefinitions/index.js index c9557fa..42cbc4d 100644 --- a/src/claimDefinitions/index.js +++ b/src/claimDefinitions/index.js @@ -54,4 +54,4 @@ const data = { } exports.list = list -exports.data = data \ No newline at end of file +exports.data = data diff --git a/src/claimDefinitions/irc.js b/src/claimDefinitions/irc.js index 2308f8c..dbf0804 100644 --- a/src/claimDefinitions/irc.js +++ b/src/claimDefinitions/irc.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { domain: match[1], nick: match[2], - } - } + }, + }, }, claim: { format: E.ClaimFormat.URI, diff --git a/src/claimDefinitions/liberapay.js b/src/claimDefinitions/liberapay.js index a251977..975e0e3 100644 --- a/src/claimDefinitions/liberapay.js +++ b/src/claimDefinitions/liberapay.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://liberapay.com/${match[1]}/public.json`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/lobsters.js b/src/claimDefinitions/lobsters.js index c6025d6..cea719e 100644 --- a/src/claimDefinitions/lobsters.js +++ b/src/claimDefinitions/lobsters.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://lobste.rs/u/${match[1]}.json`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/mastodon.js b/src/claimDefinitions/mastodon.js index 6bc97bb..44dcf27 100644 --- a/src/claimDefinitions/mastodon.js +++ b/src/claimDefinitions/mastodon.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: uri, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.FINGERPRINT, diff --git a/src/claimDefinitions/matrix.js b/src/claimDefinitions/matrix.js index 9ef4fee..0df8e12 100644 --- a/src/claimDefinitions/matrix.js +++ b/src/claimDefinitions/matrix.js @@ -57,8 +57,8 @@ const processURI = (uri) => { data: { eventId: params['org.keyoxide.e'], roomId: params['org.keyoxide.r'], - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/owncast.js b/src/claimDefinitions/owncast.js index 17708f5..4870825 100644 --- a/src/claimDefinitions/owncast.js +++ b/src/claimDefinitions/owncast.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `${uri}/api/config`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.FINGERPRINT, diff --git a/src/claimDefinitions/reddit.js b/src/claimDefinitions/reddit.js index 2ea53f6..d9d4091 100644 --- a/src/claimDefinitions/reddit.js +++ b/src/claimDefinitions/reddit.js @@ -43,8 +43,8 @@ const processURI = (uri) => { data: { url: `https://www.reddit.com/user/${match[1]}/comments/${match[2]}.json`, format: E.ProofFormat.JSON, - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/twitter.js b/src/claimDefinitions/twitter.js index 6089c84..f868faa 100644 --- a/src/claimDefinitions/twitter.js +++ b/src/claimDefinitions/twitter.js @@ -42,8 +42,8 @@ const processURI = (uri) => { format: E.ProofFormat.TEXT, data: { tweetId: match[2], - } - } + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/claimDefinitions/xmpp.js b/src/claimDefinitions/xmpp.js index 4742604..1b14a87 100644 --- a/src/claimDefinitions/xmpp.js +++ b/src/claimDefinitions/xmpp.js @@ -42,9 +42,9 @@ const processURI = (uri) => { format: E.ProofFormat.TEXT, data: { id: `${match[1]}@${match[2]}`, - field: 'note' - } - } + field: 'note', + }, + }, }, claim: { format: E.ClaimFormat.MESSAGE, diff --git a/src/defaults.js b/src/defaults.js index be07b6c..8bb4e07 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -15,28 +15,28 @@ limitations under the License. */ const E = require('./enums') -const opts = { +const opts = { proxy: { hostname: null, - policy: E.ProxyPolicy.NEVER + policy: E.ProxyPolicy.NEVER, }, claims: { irc: { - nick: null + nick: null, }, matrix: { instance: null, - accessToken: null + accessToken: null, }, xmpp: { service: null, username: null, - password: null + password: null, }, twitter: { - bearerToken: null + bearerToken: null, }, - } + }, } -exports.opts = opts \ No newline at end of file +exports.opts = opts diff --git a/src/enums.js b/src/enums.js index 73ca1b7..a2ccd9c 100644 --- a/src/enums.js +++ b/src/enums.js @@ -13,14 +13,14 @@ 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 ProxyPolicy = { +const ProxyPolicy = { ADAPTIVE: 'adaptive', ALWAYS: 'always', NEVER: 'never', } Object.freeze(ProxyPolicy) -const Fetcher = { +const Fetcher = { HTTP: 'http', DNS: 'dns', IRC: 'irc', @@ -31,7 +31,7 @@ const Fetcher = { } Object.freeze(Fetcher) -const ProofAccess = { +const ProofAccess = { GENERIC: 0, NOCORS: 1, GRANTED: 2, @@ -39,31 +39,31 @@ const ProofAccess = { } Object.freeze(ProofAccess) -const ProofFormat = { +const ProofFormat = { JSON: 'json', TEXT: 'text', } Object.freeze(ProofFormat) -const ClaimFormat = { +const ClaimFormat = { URI: 0, FINGERPRINT: 1, MESSAGE: 2, } Object.freeze(ClaimFormat) -const ClaimRelation = { +const ClaimRelation = { CONTAINS: 0, EQUALS: 1, ONEOF: 2, } Object.freeze(ClaimRelation) -const VerificationStatus = { +const VerificationStatus = { INIT: 0, INPROGRESS: 1, FAILED: 2, - COMPLETED: 3 + COMPLETED: 3, } Object.freeze(VerificationStatus) @@ -73,4 +73,4 @@ exports.ProofAccess = ProofAccess exports.ProofFormat = ProofFormat exports.ClaimFormat = ClaimFormat exports.ClaimRelation = ClaimRelation -exports.VerificationStatus = ClaimRelation \ No newline at end of file +exports.VerificationStatus = ClaimRelation diff --git a/src/fetcher/gitlab.js b/src/fetcher/gitlab.js index f4afcc2..948a2c3 100644 --- a/src/fetcher/gitlab.js +++ b/src/fetcher/gitlab.js @@ -38,7 +38,9 @@ module.exports.fn = async (data, opts) => { } const urlProject = `https://${data.domain}/api/v4/users/${user.id}/projects` - const resProject = await req(urlProject, null, { Accept: 'application/json' }) + const resProject = await req(urlProject, null, { + Accept: 'application/json', + }) const jsonProject = await resProject.json() const project = jsonProject.find((proj) => proj.path === 'gitlab_proof') diff --git a/src/fetcher/twitter.js b/src/fetcher/twitter.js index be5d602..4f16ffa 100644 --- a/src/fetcher/twitter.js +++ b/src/fetcher/twitter.js @@ -32,7 +32,9 @@ module.exports.fn = async (data, opts) => { try { validator.isAscii(opts.claims.twitter.bearerToken) } catch (err) { - throw new Error(`Twitter fetcher was not set up properly (${err.message})`) + throw new Error( + `Twitter fetcher was not set up properly (${err.message})` + ) } bentReq( diff --git a/src/keys.js b/src/keys.js index 00c8cc8..aa941a6 100644 --- a/src/keys.js +++ b/src/keys.js @@ -206,8 +206,8 @@ const process = (publicKey) => { key: { data: publicKey, fetchMethod: null, - uri: null - } + uri: null, + }, }) }) } diff --git a/src/proofs.js b/src/proofs.js index d96cb49..524db5e 100644 --- a/src/proofs.js +++ b/src/proofs.js @@ -23,7 +23,7 @@ const fetch = (data, opts) => { case E.Fetcher.HTTP: data.proof.request.data.format = data.proof.request.format break - + default: break } @@ -40,7 +40,7 @@ const handleBrowserRequests = (data, opts) => { case E.ProxyPolicy.ALWAYS: return createProxyRequestPromise(data, opts) break - + case E.ProxyPolicy.NEVER: switch (data.proof.request.access) { case E.ProofAccess.GENERIC: @@ -49,14 +49,16 @@ const handleBrowserRequests = (data, opts) => { break case E.ProofAccess.NOCORS: case E.ProofAccess.SERVER: - throw new Error('Impossible to fetch proof (bad combination of service access and proxy policy)') + throw new Error( + 'Impossible to fetch proof (bad combination of service access and proxy policy)' + ) break default: throw new Error('Invalid proof access value') break } break - + case E.ProxyPolicy.ADAPTIVE: switch (data.proof.request.access) { case E.ProofAccess.GENERIC: @@ -69,14 +71,16 @@ const handleBrowserRequests = (data, opts) => { return createFallbackRequestPromise(data, opts) break case E.ProofAccess.SERVER: - throw new Error('Impossible to fetch proof (bad combination of service access and proxy policy)') + throw new Error( + 'Impossible to fetch proof (bad combination of service access and proxy policy)' + ) break default: throw new Error('Invalid proof access value') break } break - + default: throw new Error('Invalid proxy policy') break @@ -88,15 +92,15 @@ const handleNodeRequests = (data, opts) => { case E.ProxyPolicy.ALWAYS: return createProxyRequestPromise(data, opts) break - + case E.ProxyPolicy.NEVER: return createDefaultRequestPromise(data, opts) break - + case E.ProxyPolicy.ADAPTIVE: return createFallbackRequestPromise(data, opts) break - + default: throw new Error('Invalid proxy policy') break @@ -105,16 +109,17 @@ const handleNodeRequests = (data, opts) => { const createDefaultRequestPromise = (data, opts) => { return new Promise((resolve, reject) => { - fetcher[data.proof.request.fetcher].fn(data.proof.request.data, opts) - .then(res => { + fetcher[data.proof.request.fetcher] + .fn(data.proof.request.data, opts) + .then((res) => { return resolve({ fetcher: data.proof.request.fetcher, data: data, viaProxy: false, - result: res + result: res, }) }) - .catch(err => { + .catch((err) => { return reject(err) }) }) @@ -124,7 +129,11 @@ const createProxyRequestPromise = (data, opts) => { return new Promise((resolve, reject) => { let proxyUrl try { - proxyUrl = utils.generateProxyURL(data.proof.request.fetcher, data.proof.request.data, opts); + proxyUrl = utils.generateProxyURL( + data.proof.request.fetcher, + data.proof.request.data, + opts + ) } catch (err) { reject(err) } @@ -132,18 +141,19 @@ const createProxyRequestPromise = (data, opts) => { const requestData = { url: proxyUrl, format: data.proof.request.format, - fetcherTimeout: fetcher[data.proof.request.fetcher].timeout + fetcherTimeout: fetcher[data.proof.request.fetcher].timeout, } - fetcher.http.fn(requestData, opts) - .then(res => { + fetcher.http + .fn(requestData, opts) + .then((res) => { return resolve({ fetcher: 'http', data: data, viaProxy: true, - result: res + result: res, }) }) - .catch(err => { + .catch((err) => { return reject(err) }) }) @@ -152,19 +162,19 @@ const createProxyRequestPromise = (data, opts) => { const createFallbackRequestPromise = (data, opts) => { return new Promise((resolve, reject) => { createDefaultRequestPromise(data, opts) - .then(res => { + .then((res) => { return resolve(res) }) - .catch(err1 => { + .catch((err1) => { createProxyRequestPromise(data, opts) - .then(res => { + .then((res) => { return resolve(res) }) - .catch(err2 => { + .catch((err2) => { return reject([err1, err2]) }) }) }) } -exports.fetch = fetch \ No newline at end of file +exports.fetch = fetch diff --git a/src/proxy/api/v1/index.js b/src/proxy/api/v1/index.js index 0b29d7d..cb700c0 100644 --- a/src/proxy/api/v1/index.js +++ b/src/proxy/api/v1/index.js @@ -21,7 +21,7 @@ const validUrl = require('valid-url') const jsdom = require('jsdom') const { client, xml } = require('@xmpp/client') const debug = require('@xmpp/debug') -const irc = require("irc-upd") +const irc = require('irc-upd') require('dotenv').config() const xmpp_service = process.env.XMPP_SERVICE || null @@ -129,28 +129,28 @@ router.get('/get/json/:url', (req, res) => { bentReq(req.params.url, 'json', { Accept: 'application/json', }) - .then(async (result) => { - return await result.json() - }) - .then(async (result) => { - return res.status(200).json({ url: req.params.url, content: result }) - }) - .catch((e) => { - return res.status(400).send({ error: e }) - }) + .then(async (result) => { + return await result.json() + }) + .then(async (result) => { + return res.status(200).json({ url: req.params.url, content: result }) + }) + .catch((e) => { + return res.status(400).send({ error: e }) + }) }) router.get('/get/text/:url', (req, res) => { bentReq(req.params.url) - .then(async (result) => { - return await result.text() - }) - .then(async (result) => { - return res.status(200).json({ url: req.params.url, content: result }) - }) - .catch((e) => { - return res.status(400).send({ error: e }) - }) + .then(async (result) => { + return await result.text() + }) + .then(async (result) => { + return res.status(200).json({ url: req.params.url, content: result }) + }) + .catch((e) => { + return res.status(400).send({ error: e }) + }) }) router.get('/get/dns/:hostname', async (req, res) => { @@ -216,14 +216,17 @@ router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => { throw new Error('No DESC or NOTE field found in vCard') } break - + default: - vcard = dom.window.document.querySelector(req.params.xmppdata).textContent + vcard = dom.window.document.querySelector(req.params.xmppdata) + .textContent break } return res.status(200).json(vcard) } catch (error) { - return res.status(400).json({ message: 'Request could not be fulfilled', error: error }) + return res + .status(400) + .json({ message: 'Request could not be fulfilled', error: error }) } }) @@ -232,19 +235,29 @@ router.get('/get/twitter/:tweetid', async (req, res) => { return res.status(500).json('Twitter not enabled on server') } - bentReq(`https://api.twitter.com/1.1/statuses/show.json?id=${req.params.tweetid}`, null, { - Accept: 'application/json', - Authorization: `Bearer ${twitter_bearer_token}` - }) - .then(async (data) => { - return await data.json() - }) - .then((data) => { - return res.status(200).json({ data: data, message: 'Success', error: {} }) - }) - .catch((error) => { - return res.status(error.statusCode || 400).json({ data: [], message: 'Request could not be fulfilled', error: error }) - }) + bentReq( + `https://api.twitter.com/1.1/statuses/show.json?id=${req.params.tweetid}`, + null, + { + Accept: 'application/json', + Authorization: `Bearer ${twitter_bearer_token}`, + } + ) + .then(async (data) => { + return await data.json() + }) + .then((data) => { + return res.status(200).json({ data: data, message: 'Success', error: {} }) + }) + .catch((error) => { + return res + .status(error.statusCode || 400) + .json({ + data: [], + message: 'Request could not be fulfilled', + error: error, + }) + }) }) router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => { @@ -255,24 +268,30 @@ router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => { const url = `https://${matrix_instance}/_matrix/client/r0/rooms/${req.params.matrixroomid}/event/${req.params.matrixeventid}?access_token=${matrix_access_token}` bentReq(url, null, { - Accept: 'application/json' - }) - .then(async (data) => { - return await data.json() - }) - .then((data) => { - return res.status(200).json({ data: data, message: 'Success', error: {} }) - }) - .catch((error) => { - return res.status(error.statusCode || 400).json({ data: [], message: 'Request could not be fulfilled', error: error }) + Accept: 'application/json', }) + .then(async (data) => { + return await data.json() + }) + .then((data) => { + return res.status(200).json({ data: data, message: 'Success', error: {} }) + }) + .catch((error) => { + return res + .status(error.statusCode || 400) + .json({ + data: [], + message: 'Request could not be fulfilled', + error: error, + }) + }) }) router.get('/get/irc/:ircserver/:ircnick', async (req, res) => { if (!irc_enabled) { return res.status(500).json('IRC not enabled on server') } - + try { const client = new irc.Client(req.params.ircserver, irc_nick, { port: 6697, @@ -289,15 +308,23 @@ router.get('/get/irc/:ircserver/:ircnick', async (req, res) => { client.addListener('notice', (nick, to, text, message) => { if (reKey.test(text)) { const match = text.match(reKey) - keys.push(match[1]); + keys.push(match[1]) } if (reEnd.test(text)) { client.disconnect() - return res.status(200).json({ data: keys, message: 'Success', error: {} }) + return res + .status(200) + .json({ data: keys, message: 'Success', error: {} }) } }) } catch (error) { - return res.status(400).json({ data: [], message: 'Request could not be fulfilled', error: error }) + return res + .status(400) + .json({ + data: [], + message: 'Request could not be fulfilled', + error: error, + }) } }) diff --git a/src/proxy/api/v2/index.js b/src/proxy/api/v2/index.js index 77a5aea..77f4a0b 100644 --- a/src/proxy/api/v2/index.js +++ b/src/proxy/api/v2/index.js @@ -45,7 +45,8 @@ router.get('/', async (req, res) => { }) // HTTP route -router.get('/get/http', +router.get( + '/get/http', query('url').isURL(), query('format').isIn([E.ProofFormat.JSON, E.ProofFormat.TEXT]), (req, res) => { @@ -54,49 +55,62 @@ router.get('/get/http', return res.status(400).json({ errors: errors.array() }) } - fetcher - .http.fn(req.query, opts) - .then(result => { + fetcher.http + .fn(req.query, opts) + .then((result) => { switch (req.query.format) { case E.ProofFormat.JSON: return res.status(200).json(result) - break; + break case E.ProofFormat.TEXT: return res.status(200).send(result) - break; + break } }) - .catch(err => { - return res.status(400).json({ errors: err.message ? err.message : err }) - }) -}) - -// DNS route -router.get('/get/dns', - query('domain').isFQDN(), - (req, res) => { - const errors = validationResult(req) - if (!errors.isEmpty()) { - return res.status(400).json({ errors: errors.array() }) - } - - fetcher - .dns.fn(req.query, opts) - .then((data) => { - return res.status(200).send(data) - }) .catch((err) => { return res.status(400).json({ errors: err.message ? err.message : err }) }) + } +) + +// DNS route +router.get('/get/dns', query('domain').isFQDN(), (req, res) => { + const errors = validationResult(req) + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }) + } + + fetcher.dns + .fn(req.query, opts) + .then((data) => { + return res.status(200).send(data) + }) + .catch((err) => { + return res.status(400).json({ errors: err.message ? err.message : err }) + }) }) // XMPP route -router.get('/get/xmpp', +router.get( + '/get/xmpp', query('id').isEmail(), - query('field').isIn(['fn','number','userid','url','bday','nickname','note','desc']), + query('field').isIn([ + 'fn', + 'number', + 'userid', + 'url', + 'bday', + 'nickname', + 'note', + 'desc', + ]), async (req, res) => { - if (!opts.claims.xmpp.service || !opts.claims.xmpp.username || !opts.claims.xmpp.password) { + if ( + !opts.claims.xmpp.service || + !opts.claims.xmpp.username || + !opts.claims.xmpp.password + ) { return res.status(501).json({ errors: 'XMPP not enabled on server' }) } const errors = validationResult(req) @@ -104,40 +118,40 @@ router.get('/get/xmpp', return res.status(400).json({ errors: errors.array() }) } - fetcher - .xmpp.fn(req.query, opts) + fetcher.xmpp + .fn(req.query, opts) .then((data) => { return res.status(200).send(data) }) .catch((err) => { return res.status(400).json({ errors: err.message ? err.message : err }) }) -}) + } +) // Twitter route -router.get('/get/twitter', - query('tweetId').isInt(), - async (req, res) => { - if (!opts.claims.twitter.bearerToken) { - return res.status(501).json({ errors: 'Twitter not enabled on server' }) - } - const errors = validationResult(req) - if (!errors.isEmpty()) { - return res.status(400).json({ errors: errors.array() }) - } +router.get('/get/twitter', query('tweetId').isInt(), async (req, res) => { + if (!opts.claims.twitter.bearerToken) { + return res.status(501).json({ errors: 'Twitter not enabled on server' }) + } + const errors = validationResult(req) + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }) + } - fetcher - .twitter.fn(req.query, opts) - .then((data) => { - return res.status(200).send(data) - }) - .catch((err) => { - return res.status(400).json({ errors: err.message ? err.message : err }) - }) + fetcher.twitter + .fn(req.query, opts) + .then((data) => { + return res.status(200).send(data) + }) + .catch((err) => { + return res.status(400).json({ errors: err.message ? err.message : err }) + }) }) // Matrix route -router.get('/get/matrix', +router.get( + '/get/matrix', query('roomId').isString(), query('eventId').isString(), async (req, res) => { @@ -149,40 +163,40 @@ router.get('/get/matrix', return res.status(400).json({ errors: errors.array() }) } - fetcher - .matrix.fn(req.query, opts) + fetcher.matrix + .fn(req.query, opts) .then((data) => { return res.status(200).send(data) }) .catch((err) => { return res.status(400).json({ errors: err.message ? err.message : err }) }) -}) + } +) // IRC route -router.get('/get/irc', - query('nick').isString(), - async (req, res) => { - if (!opts.claims.irc.nick) { - return res.status(501).json({ errors: 'IRC not enabled on server' }) - } - const errors = validationResult(req) - if (!errors.isEmpty()) { - return res.status(400).json({ errors: errors.array() }) - } +router.get('/get/irc', query('nick').isString(), async (req, res) => { + if (!opts.claims.irc.nick) { + return res.status(501).json({ errors: 'IRC not enabled on server' }) + } + const errors = validationResult(req) + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }) + } - fetcher - .irc.fn(req.query, opts) - .then((data) => { - return res.status(200).send(data) - }) - .catch((err) => { - return res.status(400).json({ errors: err.message ? err.message : err }) - }) + fetcher.irc + .fn(req.query, opts) + .then((data) => { + return res.status(200).send(data) + }) + .catch((err) => { + return res.status(400).json({ errors: err.message ? err.message : err }) + }) }) // Gitlab route -router.get('/get/gitlab', +router.get( + '/get/gitlab', query('domain').isFQDN(), query('username').isString(), async (req, res) => { @@ -191,14 +205,15 @@ router.get('/get/gitlab', return res.status(400).json({ errors: errors.array() }) } - fetcher - .gitlab.fn(req.query, opts) + fetcher.gitlab + .fn(req.query, opts) .then((data) => { return res.status(200).send(data) }) .catch((err) => { return res.status(400).json({ errors: err.message ? err.message : err }) }) -}) + } +) module.exports = router diff --git a/src/proxy/index.js b/src/proxy/index.js index 481bfca..6bd7589 100644 --- a/src/proxy/index.js +++ b/src/proxy/index.js @@ -28,7 +28,9 @@ app.get('/', (req, res) => { return res.status(200).json({ message: 'Available endpoints: /api' }) }) app.get('/api', (req, res) => { - return res.status(200).json({ message: 'Available API versions: /api/1, /api/2' }) + return res + .status(200) + .json({ message: 'Available API versions: /api/1, /api/2' }) }) app.all('*', (req, res) => { return res.status(404).json({ message: 'API endpoint not found' }) diff --git a/src/signatures.js b/src/signatures.js index 49cbe7b..a2ccba1 100644 --- a/src/signatures.js +++ b/src/signatures.js @@ -22,10 +22,12 @@ const process = (signature) => { let sigData, result = { fingerprint: null, - users: [{ - userData: {}, - claims: [], - }], + users: [ + { + userData: {}, + claims: [], + }, + ], primaryUserIndex: null, key: { data: null, @@ -88,7 +90,9 @@ const process = (signature) => { if (!result.key.data) { try { const match = preferredKeyServer.match(/^(.*\:\/\/)?([^/]*)(?:\/)?$/i) - result.key.uri = `hkp:${match[2]}:${issuerKeyId ? issuerKeyId : signersUserId}` + result.key.uri = `hkp:${match[2]}:${ + issuerKeyId ? issuerKeyId : signersUserId + }` result.key.data = await keys.fetch.uri(result.key.uri) result.key.fetchMethod = 'hkp' } catch (e) { @@ -99,7 +103,7 @@ const process = (signature) => { result.fingerprint = result.key.data.keyPacket.getFingerprint() - result.users[0].claims.forEach(claim => { + result.users[0].claims.forEach((claim) => { claim.fingerprint = result.fingerprint }) @@ -107,12 +111,12 @@ const process = (signature) => { let userData if (signersUserId) { - result.key.data.users.forEach(user => { + result.key.data.users.forEach((user) => { if (user.userId.email == signersUserId) { userData = user } }) - } + } if (!userData) { userData = primaryUserData.user } diff --git a/src/utils.js b/src/utils.js index b1ae21e..6374bcf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -25,13 +25,13 @@ const generateProxyURL = (type, data, opts) => { let queryStrings = [] - Object.keys(data).forEach(key => { + Object.keys(data).forEach((key) => { queryStrings.push(`${key}=${encodeURIComponent(data[key])}`) }) - return `http://${ - opts.proxy.hostname - }/api/2/get/${type}?${queryStrings.join('&')}` + return `http://${opts.proxy.hostname}/api/2/get/${type}?${queryStrings.join( + '&' + )}` } const generateClaim = (fingerprint, format) => { diff --git a/src/verifications.js b/src/verifications.js index 2d582bf..eec4625 100644 --- a/src/verifications.js +++ b/src/verifications.js @@ -16,12 +16,7 @@ limitations under the License. const utils = require('./utils') const E = require('./enums') -const runJSON = ( - proofData, - checkPath, - checkClaim, - checkRelation -) => { +const runJSON = (proofData, checkPath, checkClaim, checkRelation) => { let re if (!proofData) { @@ -31,7 +26,9 @@ const runJSON = ( if (Array.isArray(proofData)) { let result = false proofData.forEach((item, i) => { - if (result) { return } + if (result) { + return + } result = runJSON(item, checkPath, checkClaim, checkRelation) }) return result @@ -46,8 +43,10 @@ const runJSON = ( break case E.ClaimRelation.EQUALS: - return proofData.replace(/\r?\n|\r|\\/g, '').toLowerCase() == + return ( + proofData.replace(/\r?\n|\r|\\/g, '').toLowerCase() == checkClaim.toLowerCase() + ) break case E.ClaimRelation.ONEOF: @@ -77,7 +76,7 @@ const run = (proofData, claimData, fingerprint) => { completed: false, errors: [], } - + switch (claimData.proof.request.format) { case E.ProofFormat.JSON: try { @@ -112,4 +111,4 @@ const run = (proofData, claimData, fingerprint) => { return res } -exports.run = run \ No newline at end of file +exports.run = run