Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for jwks-rsa to enable cache and rate limiting #6

Merged
merged 11 commits into from
Feb 25, 2019
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- '10'
- '8'
- '7'
- '6'
cdwills marked this conversation as resolved.
Show resolved Hide resolved
before_install:
- npm install -g yarn@1.3.2
Expand Down
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const jwks = require('jwks-rsa')
const jwt = require('jsonwebtoken')

const {
applyTo: thrush, composeP, curryN, dissoc, partialRight, prop, replace
applyTo: thrush, compose, composeP, curryN, omit, merge,
partialRight, prop, replace
} = require('ramda')

const { promisify, rename, tapP } = require('@articulate/funky')
Expand All @@ -14,11 +15,11 @@ const wellKnown = '/.well-known/openid-configuration'
const bindFunction = client =>
promisify(client.getSigningKey, client)

const buildClient = url =>
const buildClient = (jwksOpts, url) =>
gimme({ url })
.then(prop('body'))
.then(rename('jwks_uri', 'jwksUri'))
.then(jwks)
.then(compose(jwks, merge(jwksOpts)))
.then(bindFunction)

const chooseKey = key =>
Expand All @@ -29,15 +30,19 @@ const decode = partialRight(jwt.decode, [{ complete: true }])
const enforce = token =>
token || Promise.reject(Boom.unauthorized('null token not allowed'))

const stripBearer =
const stripBearer =
replace(/^Bearer /i, '')

const unauthorized = err =>
Promise.reject(Boom.wrap(err, 401))
Promise.reject(Boom.unauthorized(err))

const factory = opts => {
const jwksOptsDefaults = { jwks: { cache: true, rateLimit: true } }

const factory = options => {
const clients = {}
const verifyOpts = dissoc('issWhitelist', opts)
const opts = merge(options, jwksOptsDefaults)
cdwills marked this conversation as resolved.
Show resolved Hide resolved
const verifyOpts = omit([ 'issWhitelist', 'jwks' ], opts)
const jwksOpts = prop('jwks', opts)

const cacheClient = iss => client =>
clients[iss] = client
Expand All @@ -49,7 +54,7 @@ const factory = opts => {
const getSigningKey = ({ header: { kid }, payload: { iss } }) =>
clients[iss]
? clients[iss](kid)
: buildClient(iss.replace(/\/$/, '') + wellKnown)
: buildClient(jwksOpts, iss.replace(/\/$/, '') + wellKnown)
.then(cacheClient(iss))
.then(thrush(kid))

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"dependencies": {
"@articulate/funky": "^1.2.0",
"@articulate/gimme": "^1.0.0",
"boom": "5.2.0",
"boom": "7.3.x",
"jsonwebtoken": "^8.1.1",
"jwks-rsa": "^1.2.1",
"ramda": "^0.25.0"
},
"devDependencies": {
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"eslint": "^4.16.0",
"eslint": "5.13.x",
"mocha": "^5.0.0",
"nock": "^9.1.6",
"nyc": "^11.4.1",
"nock": "10.x.x",
"nyc": "13.x.x",
"prop-factory": "^1.0.0"
}
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('authentic', () => {
expect(res().sub).to.equal('00udjyjssbt2S1QVr0h7')
)
})

describe('with a valid jwt that starts with bearer', () => {
beforeEach(() =>
authentic(lowerBearerToken).then(res)
Expand Down
Loading