Added env var to turn off rate limits for local installation

This commit is contained in:
Chris Troutner
2021-03-09 06:47:26 -08:00
parent 8345311284
commit 2fa911c04e
4 changed files with 42 additions and 29 deletions
+11 -6
View File
@@ -100,15 +100,18 @@ app.use('/', logReqInfo)
const v4prefix = 'v4'
// START Rate Limits
// Allow users to turn off rate limits with an environment variable.
const USE_RATE_LIMITS = process.env.USE_RATE_LIMITS
? process.env.USE_RATE_LIMITS
: true
const auth = new AuthMW()
console.log(`USE_RATE_LIMITS: ${USE_RATE_LIMITS}`)
// Allow users to turn off rate limits with an environment variable.
const DO_NOT_USE_RATE_LIMITS = process.env.DO_NOT_USE_RATE_LIMITS || false
if (USE_RATE_LIMITS) {
console.log(`DO_NOT_USE_RATE_LIMITS: ${DO_NOT_USE_RATE_LIMITS}`)
// Ensure req.locals and res.locals objects exist.
app.use(`/${v4prefix}/`, rateLimits2.populateLocals)
if (!DO_NOT_USE_RATE_LIMITS) {
console.log('Rate limits are being used')
// Inspect the header for a JWT token.
app.use(`/${v4prefix}/`, jwtAuth.getTokenFromHeaders)
@@ -122,6 +125,8 @@ if (USE_RATE_LIMITS) {
// Rate limit on all v4 routes
// Establish and enforce rate limits.
// app.use(`/${v4prefix}/`, rateLimits.rateLimitByResource)
} else {
console.log('Rate limits are NOT being used')
}
// END Rate Limits
+28 -22
View File
@@ -68,28 +68,6 @@ class RateLimits {
// support this function.
async applyRateLimits (req, res, next) {
try {
// console.log('Starting applyRateLimits()')
// let userId
// const decoded = {}
// Create a re*Q*.locals object if not passed in.
// req.locals.proLimit will be true if the user is using Basic Authentication.
if (!req.locals) {
req.locals = {
// default values
jwtToken: '',
proLimit: false,
apiLevel: 0
}
}
// Create a re*S*.locals object if it does not exist.
if (!res.locals) {
res.locals = {
rateLimitTriggered: false
}
}
// Exit if the user has already authenticated with Basic Authentication.
if (req.locals.proLimit) {
console.log('External call, basic auth, skipping rate limits.')
@@ -390,6 +368,34 @@ class RateLimits {
throw err
}
}
// Called when rate limits are not used.
populateLocals (req, res, next) {
try {
// Create a re*Q*.locals object if not passed in.
// req.locals.proLimit will be true if the user is using Basic Authentication.
if (!req.locals) {
req.locals = {
// default values
jwtToken: '',
proLimit: false,
apiLevel: 0
}
}
// Create a re*S*.locals object if it does not exist.
if (!res.locals) {
res.locals = {
rateLimitTriggered: false
}
}
next()
} catch (err) {
console.error('Error in populateLocals(): ', err)
throw err
}
}
}
module.exports = RateLimits
+2
View File
@@ -114,6 +114,7 @@ class Encryption {
})
}
// console.log(
wlogger.debug(
'Executing encryption/getPublicKey with this address: ',
cashAddr
@@ -180,6 +181,7 @@ class Encryption {
publicKey: 'not found'
})
} catch (err) {
console.log('Error in encryption.js/getPublicKey().', err)
wlogger.error('Error in encryption.js/getPublicKey().', err)
return _this.errorHandler(err, res)