Integrated remote JWT auth server

This commit is contained in:
Chris Troutner
2019-10-20 12:33:16 -07:00
parent b9b5c52c40
commit ccb0b3bfab
3 changed files with 126 additions and 117 deletions
-1
View File
@@ -22,7 +22,6 @@ const passport = require("passport")
const BasicStrategy = require("passport-http").BasicStrategy
const AnonymousStrategy = require("passport-anonymous")
const wlogger = require("../util/winston-logging")
const axios = require("axios")
// Used for debugging and iterrogating JS objects.
const util = require("util")
+27 -17
View File
@@ -11,11 +11,12 @@
const express = require("express")
const RateLimit = require("express-rate-limit")
const axios = require("axios")
// Set max requests per minute
const maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS
? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS)
: 30
: 10
// Pro-tier rate limits are 10x the freemium limits.
const PRO_RPM = 10 * maxRequests
@@ -23,29 +24,38 @@ const PRO_RPM = 10 * maxRequests
// Unique route mapped to its rate limit
const uniqueRateLimits = {}
const routeRateLimit = function(req, res, next) {
const routeRateLimit = async function(req, res, next) {
// Disable rate limiting if 0 passed from RATE_LIMIT_MAX_REQUESTS
if (maxRequests === 0) return next()
// Create a res.locals object if not passed in.
if (!req.locals) req.locals = {}
if (req.locals.jwtToken)
console.log(`req.locals.jwtToken: ${req.locals.jwtToken}`)
// Warn if JWT_AUTH_SERVER env var is not set.
const authServer = process.env.JWT_AUTH_SERVER
if (!authServer || authServer === "") {
console.warn(
"JWT_AUTH_SERVER env var is not set. JWT tokens not being evaluated."
)
} else {
// If a JWT token is passed in, validate it and enable pro-tier rate limits
// if it's valid.
if (req.locals.jwtToken) {
// console.log(`req.locals.jwtToken: ${req.locals.jwtToken}`)
// If no JWT token was provided, skip
if (req.payload)
console.log(`req.payload: ${JSON.stringify(req.payload, null, 2)}`)
// // Unlock the pro-tier rate limits if the user passed in a valid JWT token.
// if (!proRateLimits) {
// const user = await getUserFromJWT(req)
//
// // Enable pro-tier rate limits for this user.
// if(user) {
// console.log(`${user.email} (${user.id}) passed in valid JWT`)
// proRateLimits = true
// }
// }
const path = `${authServer}apitoken/isvalid/${req.locals.jwtToken}`
let isValidJwt = await axios.get(path)
isValidJwt = isValidJwt.data
// console.log(`isValidJwt: ${JSON.stringify(isValidJwt, null, 2)}`)
// Enable pro-tier rate limits if JWT if valid.
if (isValidJwt) {
// console.log(`JWT is valid. Enabling pro-tier rate limits.`)
req.locals.proLimit = true
}
}
}
// Current route
const rateLimitTier = req.locals.proLimit ? "PRO" : "BASIC"