Skip to content

Commit

Permalink
Null token not allowed (#4)
Browse files Browse the repository at this point in the history
* Null token not allowed

* Move the tapP to avoid anonymous functions
  • Loading branch information
flintinatux committed Jun 28, 2018
1 parent 139e512 commit 69062f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
applyTo: thrush, curryN, dissoc, partialRight, prop
} = require('ramda')

const { promisify, rename } = require('@articulate/funky')
const { promisify, rename, tapP } = require('@articulate/funky')

const wellKnown = '/.well-known/openid-configuration'

Expand All @@ -26,6 +26,9 @@ const chooseKey = key =>

const decode = partialRight(jwt.decode, [{ complete: true }])

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

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

Expand All @@ -37,9 +40,8 @@ const factory = opts => {
clients[iss] = client

const checkIss = token =>
opts.issWhitelist.indexOf(token.payload.iss) > -1
? Promise.resolve(token)
: Promise.reject(new Error(`iss '${token.payload.iss}' not in issWhitelist`))
opts.issWhitelist.indexOf(token.payload.iss) > -1 ||
Promise.reject(new Error(`iss '${token.payload.iss}' not in issWhitelist`))

const getSigningKey = ({ header: { kid }, payload: { iss } }) =>
clients[iss]
Expand All @@ -52,8 +54,9 @@ const factory = opts => {

const authentic = token =>
Promise.resolve(token)
.then(tapP(enforce))
.then(decode)
.then(checkIss)
.then(tapP(checkIss))
.then(getSigningKey)
.then(chooseKey)
.then(verify(token))
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ describe('authentic', () => {
expect(res().output.payload.message).to.contain(badIss)
)
})

describe('with a null token', () => {
beforeEach(() =>
authentic(null).catch(res)
)

it('booms with a 401', () => {
expect(res().isBoom).to.be.true
expect(res().output.statusCode).to.equal(401)
})

it('mentions that the token was null', () =>
expect(res().output.payload.message).to.contain('null token')
)
})
})

0 comments on commit 69062f6

Please sign in to comment.