fix(wallets): Adding increased rate limits for wallet apps

This commit is contained in:
Chris Troutner
2020-06-18 21:46:34 -07:00
parent 8089d73d00
commit abbf688fa6
+16 -6
View File
@@ -74,7 +74,10 @@ class RateLimits {
// Decode the JWT token if one exists.
if (req.locals.jwtToken) {
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)}`)
userId = decoded.id
@@ -110,9 +113,20 @@ class RateLimits {
// const pointsToConsume = userId ? 1 : 30
decoded.resource = resource
const pointsToConsume = _this.calcPoints(decoded)
let pointsToConsume = _this.calcPoints(decoded)
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(
`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.
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)
} catch (err) {
// console.log('err: ', err)