diff --git a/src/middleware/auth.js b/src/middleware/auth.js index 682c23d..307a583 100644 --- a/src/middleware/auth.js +++ b/src/middleware/auth.js @@ -42,18 +42,6 @@ class AuthMW { _this = this // Initialize passport for 'anonymous' authentication. - /* - passport.use( - new AnonymousStrategy({ passReqToCallback: true }, function( - req, - username, - password, - done - ) { - console.log(`anonymous auth handler triggered.`) - }) - ) - */ passport.use(new AnonymousStrategy()) // Initialize passport for 'basic' authentication. @@ -69,7 +57,13 @@ class AuthMW { //console.log(`password: ${password}`) // Create the req.locals property if it does not yet exist. - if (!req.locals) req.locals = {} + if (!req.locals) { + req.locals = { + // default values + proLimit: false, + apiLevel: 0 + } + } // Set pro-tier rate limit to flag to false by default. req.locals.proLimit = false diff --git a/src/middleware/jwt-auth.js b/src/middleware/jwt-auth.js index 485e7fa..b006035 100644 --- a/src/middleware/jwt-auth.js +++ b/src/middleware/jwt-auth.js @@ -25,8 +25,13 @@ const getTokenFromHeaders = (req, res, next) => { // console.log(`JWT found: ${token}`) // Create the req.locals property if it does not yet exist. - if (!req.locals) req.locals = {} - req.locals.jwtToken = token + if (!req.locals) { + req.locals = { + jwtToken: token, + proLimit: false, + apiLevel: 0 + } + } } } } catch (err) { diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index a2d56ee..5bc6886 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -29,7 +29,14 @@ const routeRateLimit = async function(req, res, next) { if (maxRequests === 0) return next() // Create a res.locals object if not passed in. - if (!req.locals) req.locals = {} + if (!req.locals) { + req.locals = { + // default values + jwtToken: "", + proLimit: false, + apiLevel: 0 + } + } // Warn if JWT_AUTH_SERVER env var is not set. const authServer = process.env.JWT_AUTH_SERVER @@ -45,14 +52,15 @@ const routeRateLimit = async function(req, res, next) { const path = `${authServer}apitoken/isvalid/${req.locals.jwtToken}` - let isValidJwt = await axios.get(path) - isValidJwt = isValidJwt.data - // console.log(`isValidJwt: ${JSON.stringify(isValidJwt, null, 2)}`) + let jwtInfo = await axios.get(path) + jwtInfo = jwtInfo.data + // console.log(`jwtInfo: ${JSON.stringify(jwtInfo, null, 2)}`) // Enable pro-tier rate limits if JWT if valid. - if (isValidJwt) { + if (jwtInfo.isValid) { // console.log(`JWT is valid. Enabling pro-tier rate limits.`) req.locals.proLimit = true + req.locals.apiLevel = jwtInfo.apiLevel } } } diff --git a/src/routes/v3/control.js b/src/routes/v3/control.js index da6eb78..6a3f6ee 100644 --- a/src/routes/v3/control.js +++ b/src/routes/v3/control.js @@ -12,64 +12,12 @@ const util = require("util") util.inspect.defaultOptions = { depth: 1 } router.get("/", root) -router.get("/getinfo", getInfo) router.get("/getnetworkinfo", getNetworkInfo) function root(req, res, next) { return res.json({ status: "control" }) } -/** - * @api {get} /control/getinfo Get full node info - * @apiName GetInfo - * @apiGroup Control - * @apiDescription RPC call which gets basic full node information. - * - * @apiExample Example usage: - * curl -X GET "https://mainnet.bchjs.cash/v3/control/getinfo" -H "accept: application/json" - * - * @apiSuccess {Object} object Object containing data - * @apiSuccess {Number} object.version Full node version - * @apiSuccess {Number} object.protocolversion Protocol version - * @apiSuccess {Number} object.blocks Current block - * @apiSuccess {Number} object.timeoffset ? - * @apiSuccess {Number} object.connections Number of connected peers - * @apiSuccess {String} object.proxy ? - * @apiSuccess {Number} object.difficulty Current difficulty setting - * @apiSuccess {Boolean} object.testnet testnet = true, mainnet = false - * @apiSuccess {Number} object.paytxfee ? - * @apiSuccess {Number} object.relayfee ? - * @apiSuccess {String} object.errors Recent errors - */ -async function getInfo(req, res, next) { - const { - BitboxHTTP, - username, - password, - requestConfig - } = routeUtils.setEnvVars() - - requestConfig.data.id = "getinfo" - requestConfig.data.method = "getinfo" - requestConfig.data.params = [] - - try { - const response = await BitboxHTTP(requestConfig) - - return res.json(response.data.result) - } catch (error) { - wlogger.error(`Error in control.ts/getInfo().`, error) - - // Write out error to error log. - //logger.error(`Error in control/getInfo: `, error) - - res.status(500) - if (error.response && error.response.data && error.response.data.error) - return res.json({ error: error.response.data.error }) - return res.json({ error: util.inspect(error) }) - } -} - /** * @api {get} /control/getnetworkinfo Get Network Info * @apiName GetNetworkInfo @@ -88,6 +36,8 @@ async function getNetworkInfo(req, res, next) { requestConfig } = routeUtils.setEnvVars() + console.log(`req.locals: ${JSON.stringify(req.locals, null, 2)}`) + requestConfig.data.id = "getnetworkinfo" requestConfig.data.method = "getnetworkinfo" requestConfig.data.params = [] @@ -150,7 +100,6 @@ module.exports = { router, testableComponents: { root, - getInfo, getNetworkInfo } }