Decoded JWT token

This commit is contained in:
Chris Troutner
2020-02-04 17:11:19 -08:00
parent 5ecc321b3e
commit dcf8455162
4 changed files with 170 additions and 5 deletions
+2 -1
View File
@@ -86,7 +86,8 @@ app.use(`/${v3prefix}/`, auth.mw())
// Rate limit on all v3 routes
// Establish and enforce rate limits.
app.use(`/${v3prefix}/`, rateLimits.routeRateLimit)
// app.use(`/${v3prefix}/`, rateLimits.routeRateLimit)
app.use(`/${v3prefix}/`, rateLimits.newRateLimit)
app.use(`/${v3prefix}/` + `health-check`, healthCheckV3)
app.use(`/${v3prefix}/` + `blockchain`, blockchainV3.router)
+45
View File
@@ -21,6 +21,10 @@ const express = require("express")
const RateLimit = require("express-rate-limit")
const axios = require("axios")
const jwt = require("jsonwebtoken")
const KeyEncoder = require("key-encoder").default
const keyEncoder = new KeyEncoder("secp256k1")
// Set max requests per minute
const maxRequests = process.env.RATE_LIMIT_MAX_REQUESTS
? parseInt(process.env.RATE_LIMIT_MAX_REQUESTS)
@@ -203,6 +207,47 @@ class RateLimits {
return retObj
}
newRateLimit(req, res, next) {
try {
// Create a res.locals object if not passed in.
if (!req.locals) {
req.locals = {
// default values
jwtToken: "",
proLimit: false,
apiLevel: 0
}
}
if (req.locals.jwtToken) {
// Hexadecimal
const publicKey =
"03e6c358092a459f7da9420de770eef3e16cf3c9c54a3d3d14ac2d7f0b82af4d7d"
const jwtOptions = {
algorithms: ["ES256"]
}
const pemPublicKey = keyEncoder.encodePublic(publicKey, "raw", "pem")
// Validate the JWT token.
const decoded = jwt.verify(
req.locals.jwtToken,
pemPublicKey,
jwtOptions
)
console.log(`decoded: ${JSON.stringify(decoded, null, 2)}`)
} else {
console.log(`No JWT token found!`)
}
} catch (err) {
console.error(`Error in route-ratelimit.js/newRateLimit(): `, err)
// throw err
}
next()
}
}
module.exports = RateLimits