Skip to content

Commit

Permalink
refactored tokenblacklist to have less if nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed Mar 4, 2020
1 parent 17581d6 commit d6b4507
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/util/token-blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@ const LRU = require('lru-cache')
const consoleLogger = require('./logger.js')

class TokenBlacklist {
constructor(eventEmitter, maxIndexingErrors, errorRegex) {
constructor (eventEmitter, maxIndexingErrors, errorRegex) {
this.errorRegex = errorRegex || /Application not found for token (\S+),/
this.maxIndexingErrors = maxIndexingErrors || 1 // failures before blacklisting tokens
this.maxIndexingErrors = maxIndexingErrors || 1 // failures before blacklisting tokens
this.invalidTokens = new LRU({
max: 5000,
maxAge: 10 * 60 * 1000
})

eventEmitter.on('error', function (err) {
// blacklist tokens ony when the token is unknown
let match = String(err).match(this.errorRegex)
if (match && match.length > 1) {
if (!this.invalidTokens.get(match[1])) {
this.invalidTokens.set(match[1], 1)
} else {
this.invalidTokens.set(match[1], this.invalidTokens.get(match[1]))
}
if (this.invalidTokens.get(match[1]) >= this.maxIndexingErrors) {
consoleLogger.log(`Invalid token added to blacklist ${match[1]}`)
}
// blacklist tokens only when the token is unknown
const match = String(err).match(this.errorRegex)
const token = match && match.length > 1 && match[1]
if (!token) {
return
}

if (!this.invalidTokens.get(token)) {
this.invalidTokens.set(token, 1)
} else {
this.invalidTokens.set(token, this.invalidTokens.get(token))
}

if (this.invalidTokens.get(token) >= this.maxIndexingErrors) {
consoleLogger.log(`Invalid token added to blacklist ${token}`)
}
}.bind(this))
}
isTokenInvalid(token) {

isTokenInvalid (token) {
return this.invalidTokens.get(token) >= this.maxIndexingErrors
}
}

module.exports = TokenBlacklist
module.exports = TokenBlacklist

0 comments on commit d6b4507

Please sign in to comment.