From 28f0a1de569a1a89e5836ba70be90cd90b66ca19 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 11 Nov 2020 14:56:24 -0800 Subject: [PATCH] feat(Basic Authentication): Adding back in for controlling access --- src/middleware/auth.js | 6 ++- src/middleware/route-ratelimit.js | 5 ++- test/v3/integration/rate-limits.js | 61 +++++++++++++++--------------- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/middleware/auth.js b/src/middleware/auth.js index df85235..b6bd2a6 100644 --- a/src/middleware/auth.js +++ b/src/middleware/auth.js @@ -33,7 +33,7 @@ util.inspect.defaultOptions = { depth: 1 } // let _this // Set default rate limit value for testing -const PRO_PASSES = process.env.PRO_PASS ? process.env.PRO_PASS : 'BITBOX' +const PRO_PASSES = process.env.PRO_PASSES ? process.env.PRO_PASSES : 'testpassword' // Convert the pro-tier password string into an array split by ':'. const PRO_PASS = PRO_PASSES.split(':') @@ -82,6 +82,7 @@ class AuthMW { // Success req.locals.proLimit = true + console.log(`User ${req.ip} authenticated using Basic Auth`) break } } @@ -96,7 +97,8 @@ class AuthMW { // Middleware called by the route. mw () { - return passport.authenticate(['anonymous', 'basic'], { + console.log('Initializing passport') + return passport.authenticate(['basic', 'anonymous'], { session: false }) } diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index 83c2c6c..10e4020 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -154,8 +154,9 @@ class RateLimits { // For internal calls, increase rate limits to as fast as possible. if ( - key.toString().indexOf('172.17.') > -1 || - key.toString().indexOf('::ffff:127.0.0.1') > -1 + key.toString().indexOf('172.17.') > -1 + // Comment out the line below when running bch-js e2e rate limit tests. + // key.toString().indexOf('::ffff:127.0.0.1') > -1 ) { pointsToConsume = 1 res.locals.pointsToConsume = pointsToConsume // Feedback for tests. diff --git a/test/v3/integration/rate-limits.js b/test/v3/integration/rate-limits.js index e741dba..8279e4e 100644 --- a/test/v3/integration/rate-limits.js +++ b/test/v3/integration/rate-limits.js @@ -1,7 +1,6 @@ /* - This integration tests should be run against a live bch-api REST server. It - tests to ensure the rate-limits are working as expected. Adjust the values - in the tests below to match the rate limit setting in your own installation. + These tests have been deprecated. To test bch-api rate limits, run the e2e + tests in the bch-js repository. */ 'use strict' @@ -134,32 +133,32 @@ describe('#JWT rate limits', () => { // } // }) - it('should unlock pro-tier for a valid JWT token', async () => { - try { - // Actual rate limit is 60 per minute X 4 nodes = 240 rpm. - const options = { - method: 'GET', - url: `${SERVER}control/`, - headers: { - Authorization: `Token ${TEST_JWT}` - } - } - - const promises = [] - for (let i = 0; i < 60; i++) { - const promise = axios(options) - promises.push(promise) - } - - await Promise.all(promises) - - // assert.equal(true, false, "Unexpected result!") - assert.equal(true, true, 'Not throwing an error is a pass!') - } catch (err) { - console.log(`err.response: ${util.inspect(err.response)}`) - - assert.equal(true, false, 'Unexpected result!') - } - // Override default timeout for this test. - }).timeout(20000) + // it('should unlock pro-tier for a valid JWT token', async () => { + // try { + // // Actual rate limit is 60 per minute X 4 nodes = 240 rpm. + // const options = { + // method: 'GET', + // url: `${SERVER}control/`, + // headers: { + // Authorization: `Token ${TEST_JWT}` + // } + // } + // + // const promises = [] + // for (let i = 0; i < 60; i++) { + // const promise = axios(options) + // promises.push(promise) + // } + // + // await Promise.all(promises) + // + // // assert.equal(true, false, "Unexpected result!") + // assert.equal(true, true, 'Not throwing an error is a pass!') + // } catch (err) { + // console.log(`err.response: ${util.inspect(err.response)}`) + // + // assert.equal(true, false, 'Unexpected result!') + // } + // // Override default timeout for this test. + // }).timeout(20000) })