From 13b41a50c678659772db391f6ec1694804a81e4f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 4 Feb 2020 16:12:38 -0800 Subject: [PATCH] Editing comments and notes --- package-lock.json | 64 ++++++++++++++++++++++++++ package.json | 6 ++- src/app.js | 5 ++- src/middleware/auth.js | 3 ++ src/middleware/jwt-auth.js | 2 + src/middleware/route-ratelimit.js | 2 +- test/v3/rate-limits.js | 74 ------------------------------- 7 files changed, 78 insertions(+), 78 deletions(-) diff --git a/package-lock.json b/package-lock.json index c00d725..9608aa0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2256,6 +2256,11 @@ "wrap-ansi": "^5.1.0" } }, + "cluster-key-slot": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", + "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==" + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -3041,6 +3046,11 @@ "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", "dev": true }, + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -5690,6 +5700,22 @@ "p-is-promise": "^3.0.0" } }, + "ioredis": { + "version": "4.14.1", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.14.1.tgz", + "integrity": "sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==", + "requires": { + "cluster-key-slot": "^1.1.0", + "debug": "^4.1.1", + "denque": "^1.1.0", + "lodash.defaults": "^4.2.0", + "lodash.flatten": "^4.4.0", + "redis-commands": "1.5.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0", + "standard-as-callback": "^2.0.1" + } + }, "ipaddr.js": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", @@ -6473,12 +6499,22 @@ "integrity": "sha1-+CbJtOKoUR2E46yinbBeGk87cqk=", "dev": true }, + "lodash.defaults": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", + "integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=" + }, "lodash.escaperegexp": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz", "integrity": "sha1-ZHYsSGGAglGKw99Mz11YhtriA0c=", "dev": true }, + "lodash.flatten": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", + "integrity": "sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=" + }, "lodash.flattendeep": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz", @@ -12165,6 +12201,11 @@ "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" }, + "rate-limiter-flexible": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/rate-limiter-flexible/-/rate-limiter-flexible-1.3.2.tgz", + "integrity": "sha512-f+xNvGn+52G4nZVok9VB3LTE1kfDmqbnWKRayX5n2k/LEQ7doWrYvzmzFVh7ltmLgwEdEzoYwxGaXlfCjFg4Ag==" + }, "raw-body": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", @@ -12332,6 +12373,24 @@ "esprima": "~4.0.0" } }, + "redis-commands": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", + "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, "referrer-policy": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/referrer-policy/-/referrer-policy-1.2.0.tgz", @@ -13628,6 +13687,11 @@ } } }, + "standard-as-callback": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.0.1.tgz", + "integrity": "sha512-NQOxSeB8gOI5WjSaxjBgog2QFw55FV8TkS6Y07BiB3VJ8xNTvUYm0wl0s8ObgQ5NhdpnNfigMIKjgPESzgr4tg==" + }, "standard-engine": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/standard-engine/-/standard-engine-8.0.1.tgz", diff --git a/package.json b/package.json index 8d848f3..7ea61fb 100644 --- a/package.json +++ b/package.json @@ -15,15 +15,15 @@ "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/blockchain.js", "test:integration": "mocha test/v3/integration", "coverage": "nyc report --reporter=text-lcov | coveralls", - "coverage:report": "export NETWORK=testnet && nyc --reporter=html mocha test/v2/", + "coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v3/", "docs": "./node_modules/.bin/apidoc -i src/routes/v3 -o docs" }, "engines": { "node": ">=10.15.1" }, "dependencies": { - "apidoc": "^0.20.0", "@chris.troutner/bch-js": "^2.0.0", + "apidoc": "^0.20.0", "axios": "^0.19.0", "body-parser": "^1.18.3", "cookie-parser": "~1.4.3", @@ -34,6 +34,7 @@ "express-basic-auth": "^1.1.3", "express-rate-limit": "^5.0.0", "helmet": "^3.21.2", + "ioredis": "^4.14.1", "level": "^6.0.0", "mkdirp": "^1.0.0", "mocha": "^7.0.1", @@ -44,6 +45,7 @@ "passport-http": "^0.3.0", "pg": "^7.11.0", "pg-hstore": "^2.3.2", + "rate-limiter-flexible": "^1.3.2", "strftime": "^0.10.0", "winston": "^3.2.1", "winston-daily-rotate-file": "^4.0.0" diff --git a/src/app.js b/src/app.js index 5e4dc52..95597a8 100644 --- a/src/app.js +++ b/src/app.js @@ -78,11 +78,14 @@ const v3prefix = "v3" app.use(`/${v3prefix}/`, jwtAuth.getTokenFromHeaders) // Instantiate the authorization middleware, used to implement pro-tier rate limiting. +// Handles Anonymous and Basic Authorization schemes used by passport.js const auth = new AuthMW() app.use(`/${v3prefix}/`, auth.mw()) // Rate limit on all v3 routes -app.use(`/${v3prefix}/`, routeRateLimit) // Establish and enforce rate limits. +// Establish and enforce rate limits. +app.use(`/${v3prefix}/`, routeRateLimit) + app.use(`/${v3prefix}/` + `health-check`, healthCheckV3) app.use(`/${v3prefix}/` + `blockchain`, blockchainV3.router) app.use(`/${v3prefix}/` + `control`, controlV3.router) diff --git a/src/middleware/auth.js b/src/middleware/auth.js index 307a583..77510ff 100644 --- a/src/middleware/auth.js +++ b/src/middleware/auth.js @@ -1,4 +1,7 @@ /* + CT 2/4/20 Note: This library handles anonymous and Basic auth. This library + can be phased out with the chage to JWT tokens and the new rate-limit library. + Handle authorization for bypassing rate limits. 1) Default is 'Anonymous Authentication', which unlocks the freemimum tier by diff --git a/src/middleware/jwt-auth.js b/src/middleware/jwt-auth.js index 6880ea0..27b90cd 100644 --- a/src/middleware/jwt-auth.js +++ b/src/middleware/jwt-auth.js @@ -1,4 +1,6 @@ /* + This is a middleware library for handling and processing JWT tokens. + This middleware inspects the request header for a JWT token. If found, will populate req.locals.jwtToken with the JWT token. */ diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index cdad7a6..3dd8b6a 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -159,7 +159,7 @@ const routeRateLimit = async function(req, res, next) { uniqueRateLimits[route](req, res, next) } -// This function returns the an object with proLimit and apiLevel properties. +// This function returns an object with proLimit and apiLevel properties. // It does fine-grane analysis on the data coming from the auth servers and // uses its output to adjust rate limits on-the-fly based on the users // permission level. diff --git a/test/v3/rate-limits.js b/test/v3/rate-limits.js index 50d7ca6..3753ccc 100644 --- a/test/v3/rate-limits.js +++ b/test/v3/rate-limits.js @@ -71,80 +71,6 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(req.locals.jwtToken, jwt) }) }) - - // TODO: This code has been refactored and these unit tests no longer apply. - // I think I forgot to create new unit tests to reflect the change in code though. - /* - describe("#routeAccess", () => { - it("should do nothing if req.locals.jwtToken is undefined", () => { - // Initialize req.locals - req.locals = { - proLimit: false, - apiLevel: 0 - } - req.url = "/insight/address/details" - - // Reset the history of the stub and assert it has not been called. - res.status.resetHistory() - assert.equal(res.status.called, false, "stub history reset") - - jwtAuth.routeAccess(req, res, next) - - // console.log(`req.locals: ${JSON.stringify(req.locals, null, 2)}`) - assert.equal( - res.status.called, - false, - "stub should NOT have been called." - ) - }) - - it("should throw error if full-node tier tries to access indexer", () => { - // Initialize req.locals - req.locals = { - proLimit: true, - apiLevel: 10, - jwtToken: jwt - } - req.url = "/insight/address/details" - - try { - res.status.resetHistory() - assert.equal(res.status.called, false, "stub history reset") - - jwtAuth.routeAccess(req, res, next) - - assert.equal(res.status.called, true, "stub should have been called.") - } catch (err) { - console.log(`caught error: `, err) - } - }) - - it("should allow indexer tier tries access indexer endpoints", () => { - // Initialize req.locals - req.locals = { - proLimit: true, - apiLevel: 20, - jwtToken: jwt - } - req.url = "/insight/address/details" - - try { - res.status.resetHistory() - assert.equal(res.status.called, false, "stub history reset") - - jwtAuth.routeAccess(req, res, next) - - assert.equal( - res.status.called, - false, - "stub should NOT have been called." - ) - } catch (err) { - console.log(`caught error: `, err) - } - }) - }) - */ }) describe("#routeRateLimit", () => {