diff --git a/src/proxy/api/v2/index.js b/src/proxy/api/v2/index.js index 55de780..2b157d6 100644 --- a/src/proxy/api/v2/index.js +++ b/src/proxy/api/v2/index.js @@ -15,34 +15,30 @@ limitations under the License. */ const router = require('express').Router() const fetcher = require('../../../fetcher') +const E = require('../../../enums') require('dotenv').config() -const xmpp_service = process.env.XMPP_SERVICE || null -const xmpp_username = process.env.XMPP_USERNAME || null -const xmpp_password = process.env.XMPP_PASSWORD || null -const twitter_bearer_token = process.env.TWITTER_BEARER_TOKEN || null -const matrix_instance = process.env.MATRIX_INSTANCE || null -const matrix_access_token = process.env.MATRIX_ACCESS_TOKEN || null -const irc_nick = process.env.IRC_NICK || null - -let xmpp_enabled = true, - twitter_enabled = false, - matrix_enabled = false, - irc_enabled = false - -if (!xmpp_service || !xmpp_username || !xmpp_password) { - xmpp_enabled = false -} -if (twitter_bearer_token) { - twitter_enabled = true -} -if (matrix_instance && matrix_access_token) { - matrix_enabled = true -} -if (irc_nick) { - irc_enabled = true +const opts = { + claims: { + xmpp: { + service: process.env.XMPP_SERVICE || null, + username: process.env.XMPP_USERNAME || null, + password: process.env.XMPP_PASSWORD || null, + }, + twitter: { + bearerToken: process.env.TWITTER_BEARER_TOKEN || null, + }, + matrix: { + instance: process.env.MATRIX_INSTANCE || null, + accessToken: process.env.MATRIX_ACCESS_TOKEN || null, + }, + irc: { + nick: process.env.IRC_NICK || null, + }, + }, } +// Root route router.get('/', async (req, res) => { return res.status(400).json({ data: [], @@ -51,30 +47,67 @@ router.get('/', async (req, res) => { }) }) -router.param('url', async (req, res, next, url) => { - req.params.url = decodeURI(url) - - if (!validUrl.isUri(req.params.url)) { - return res.status(400).json({ error: 'URL provided was not valid' }) +// HTTP route +router.get('/get/http', (req, res) => { + if (!req.query.url) { + return res.status(400).json({ error: 'Missing parameter(s)' }) + } + if (!validUrl.isUri(req.query.url)) { + return res.status(400).json({ error: 'Invalid URL' }) } - next() + fetcher.http(req.query, opts) + .then(result => { + switch (req.query.format) { + case E.ProofFormat.JSON: + return res.status(200).json(result) + break; + + case E.ProofFormat.TEXT: + return res.status(200).send(result) + break; + + default: + throw new Error('Invalid proof format') + break; + } + }) + .catch(err => { + return res.status(400).json({ error: err.message ? err.message : err }) + }) }) -router.param('xmppid', async (req, res, next, xmppid) => { - req.params.xmppid = xmppid - - if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.params.xmppid)) { - next() - } else { - return res.status(400).json({ error: 'XMPP_ID was not valid' }) +// DNS route +router.get('/get/dns', (req, res) => { + if (!req.query.domain) { + return res.status(400).json({ error: 'Missing parameter(s)' }) } + + fetcher + .dns(req.query, opts) + .then((data) => { + return res.status(200).send(data) + }) + .catch((err) => { + return res.status(400).json({ error: err.message ? err.message : err }) + }) }) -router.param('xmppdata', async (req, res, next, xmppdata) => { - req.params.xmppdata = xmppdata.toUpperCase() +// XMPP route +router.get('/get/xmpp', async (req, res) => { + if (!opts.claims.xmpp.service || !opts.claims.xmpp.username || !opts.claims.xmpp.password) { + return res.status(501).json({ error: 'XMPP not enabled on server' }) + } - const allowedData = [ + if (!req.query.id || !req.query.field) { + return res.status(400).json({ error: 'Missing parameter(s)' }) + } + + if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.query.id))) { + return res.status(400).json({ error: 'Invalid XMPP ID' }) + } + + const allowedField = [ 'FN', 'NUMBER', 'USERID', @@ -84,76 +117,12 @@ router.param('xmppdata', async (req, res, next, xmppdata) => { 'NOTE', 'DESC', ] - - if (!allowedData.includes(req.params.xmppdata)) { - return res.status(400).send({ - data: [], - error: - 'Allowed data are: FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC', - }) - } - - next() -}) - -router.get('/get/json/:url', (req, res) => { - bentReq(req.params.url, 'json', { - Accept: 'application/json', - }) - .then(async (result) => { - return await result.json() - }) - .then(async (data) => { - return res.status(200).send(data) - }) - .catch((err) => { - return res.status(400).json({ error: err }) - }) -}) - -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).send(result) - }) - .catch((err) => { - return res.status(400).json({ error: err }) - }) -}) - -router.get('/get/dns/:hostname', (req, res) => { - fetcher - .dns(req.params.hostname) - .then((data) => { - return res.status(200).send(data) - }) - .catch((err) => { - return res.status(400).json({ error: err }) - }) -}) - -router.get('/get/xmpp/:xmppid', async (req, res) => { - return res.status(400).send({ - data: [], - error: - 'Data request parameter missing (FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC)', - }) -}) - -router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => { - if (!xmpp_enabled) { - return res.status(501).json({ error: 'XMPP not enabled on server' }) + if (!allowedField.includes(req.query.field)) { + return res.status(400).json({ error: 'Invalid XMPP vCard field' }) } fetcher - .xmpp(req.params.xmppid, req.params.xmppdata, { - service: xmpp_service, - username: xmpp_username, - password: xmpp_password, - }) + .xmpp(req.query, opts) .then((data) => { return res.status(200).send(data) }) @@ -162,55 +131,63 @@ router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => { }) }) -router.get('/get/twitter/:tweetid', async (req, res) => { - if (!twitter_enabled) { +// Twitter route +router.get('/get/twitter', async (req, res) => { + if (!opts.claims.twitter.bearerToken) { return res.status(501).json({ error: 'Twitter not enabled on server' }) } + if (!req.query.tweetId) { + return res.status(400).json({ error: 'Missing parameter(s)' }) + } + fetcher - .twitter(req.params.tweetid, { - bearerToken: twitter_bearer_token, - }) + .twitter(req.query, opts) .then((data) => { return res.status(200).send(data) }) .catch((err) => { - return res.status(400).json({ error: err }) + return res.status(400).json({ error: err.message ? err.message : err }) }) }) +// Matrix route router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => { - if (!matrix_enabled) { + if (!opts.claims.matrix.instance || !opts.claims.matrix.accessToken) { return res.status(501).json({ error: 'Matrix not enabled on server' }) } - fetcher - .matrix(req.params.matrixroomid, req.params.matrixeventid, { - instance: process.env.MATRIX_INSTANCE, - accessToken: process.env.MATRIX_ACCESS_TOKEN, - }) - .then((data) => { - return res.status(200).send(data) - }) - .catch((err) => { - return res.status(400).json({ error: err }) - }) -}) - -router.get('/get/irc/:ircserver/:ircnick', async (req, res) => { - if (!irc_enabled) { - return res.status(501).json({ error: 'IRC not enabled on server' }) + if (!req.query.id || !req.query.field) { + return res.status(400).json({ error: 'Missing parameter(s)' }) } fetcher - .irc(req.params.ircserver, req.params.ircnick, { - nick: 'doipver148927', - }) + .matrix(req.params, opts) .then((data) => { return res.status(200).send(data) }) .catch((err) => { - return res.status(400).json({ error: err }) + return res.status(400).json({ error: err.message ? err.message : err }) + }) +}) + +// IRC route +router.get('/get/irc/:ircserver/:ircnick', async (req, res) => { + if (!opts.claims.irc.nick) { + return res.status(501).json({ error: 'IRC not enabled on server' }) + } + + if (!req.query.nick) { + return res.status(400).json({ error: 'Missing parameter(s)' }) + } + + fetcher + .irc(req.params, opts) + .then((data) => { + return res.status(200).send(data) + }) + .catch((err) => { + return res.status(400).json({ error: err.message ? err.message : err }) }) })