Merge pull request #143 from christroutner/unstable

fix(wallets): Adding increased rate limits for wallet apps
This commit is contained in:
Chris Troutner
2020-06-18 21:49:06 -07:00
committed by GitHub
+16 -6
View File
@@ -74,7 +74,10 @@ class RateLimits {
// Decode the JWT token if one exists. // Decode the JWT token if one exists.
if (req.locals.jwtToken) { if (req.locals.jwtToken) {
try { try {
decoded = _this.jwt.verify(req.locals.jwtToken, _this.config.apiTokenSecret) decoded = _this.jwt.verify(
req.locals.jwtToken,
_this.config.apiTokenSecret
)
// console.log(`decoded: ${JSON.stringify(decoded, null, 2)}`) // console.log(`decoded: ${JSON.stringify(decoded, null, 2)}`)
userId = decoded.id userId = decoded.id
@@ -110,9 +113,20 @@ class RateLimits {
// const pointsToConsume = userId ? 1 : 30 // const pointsToConsume = userId ? 1 : 30
decoded.resource = resource decoded.resource = resource
const pointsToConsume = _this.calcPoints(decoded) let pointsToConsume = _this.calcPoints(decoded)
res.locals.pointsToConsume = pointsToConsume // Feedback for tests. res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
// If the request originates from one of the approved wallet apps, then
// apply paid-access rate limits.
const origin = req.get('origin')
if (
origin.toString().indexOf('wallet.fullstack.cash') > -1 ||
origin.toString().indexOf('sandbox.fullstack.cash') > -1
) {
pointsToConsume = 1
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
}
wlogger.info( wlogger.info(
`User ${key} consuming ${pointsToConsume} point for resource ${resource}.` `User ${key} consuming ${pointsToConsume} point for resource ${resource}.`
) )
@@ -122,10 +136,6 @@ class RateLimits {
// Update the key so that rate limits track both the user and the resource. // Update the key so that rate limits track both the user and the resource.
key = `${key}-${resource}` key = `${key}-${resource}`
const host = req.get('host')
const origin = req.get('origin')
console.log(`host: ${host}, origin: ${origin}`)
await _this.rateLimiter.consume(key, pointsToConsume) await _this.rateLimiter.consume(key, pointsToConsume)
} catch (err) { } catch (err) {
// console.log('err: ', err) // console.log('err: ', err)