Skip to content

Commit

Permalink
Merge ff1cc6f into 103dbff
Browse files Browse the repository at this point in the history
  • Loading branch information
dpbackes committed Jan 8, 2019
2 parents 103dbff + ff1cc6f commit a338d0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const jwks = require('jwks-rsa')
const jwt = require('jsonwebtoken')

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

const { promisify, rename, tapP } = require('@articulate/funky')
Expand All @@ -27,7 +27,10 @@ const chooseKey = key =>
const decode = partialRight(jwt.decode, [{ complete: true }])

const enforce = token =>
token || Promise.reject(new Error('null token not allowed'))
token || Promise.reject(Boom.unauthorized('null token not allowed'))

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

const unauthorized = err =>
Promise.reject(Boom.wrap(err, 401))
Expand All @@ -54,15 +57,14 @@ const factory = opts => {

const authentic = token =>
Promise.resolve(token)
.then(tapP(enforce))
.then(decode)
.then(tapP(checkIss))
.then(getSigningKey)
.then(chooseKey)
.then(verify(token))
.catch(unauthorized)

return authentic
return composeP(authentic, stripBearer, tapP(enforce))
}

module.exports = factory
38 changes: 34 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const jwt = require('jsonwebtoken')
const nock = require('nock')
const property = require('prop-factory')

const bad = require('./fixtures/bad-iss')
const keys = require('./fixtures/keys')
const oidc = require('./fixtures/oidc')
const token = require('./fixtures/token')
const bad = require('./fixtures/bad-iss')
const keys = require('./fixtures/keys')
const oidc = require('./fixtures/oidc')
const token = require('./fixtures/token')
const capitalBearerToken = 'Bearer ' + token
const lowerBearerToken = 'bearer ' + token

const { issuer } = oidc

Expand Down Expand Up @@ -47,6 +49,34 @@ describe('authentic', () => {
)
})

describe('with a valid jwt that starts with Bearer', () => {
beforeEach(() =>
authentic(capitalBearerToken).then(res)
)

it('validates the jwt against the jwks', () =>
expect(res().sub).to.equal('00udjyjssbt2S1QVr0h7')
)

it('caches the jwks client', () =>
expect(res().sub).to.equal('00udjyjssbt2S1QVr0h7')
)
})

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

it('validates the jwt against the jwks', () =>
expect(res().sub).to.equal('00udjyjssbt2S1QVr0h7')
)

it('caches the jwks client', () =>
expect(res().sub).to.equal('00udjyjssbt2S1QVr0h7')
)
})

describe('with an invalid jwt', () => {
beforeEach(() =>
authentic('invalid').catch(res)
Expand Down

0 comments on commit a338d0f

Please sign in to comment.