Skip to content

Commit d8c27f6

Browse files
committed
Add more context to 401 errors
1 parent 8078c90 commit d8c27f6

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/apiKey.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ function getRequestOptions (request, options) {
7070
* additional api key interceptor.
7171
*
7272
* @param {Hapi.server} server The related hapi server object
73+
* @param {Object} pluginOptions The plugin related options
7374
* @param {Object} options The api key related options
7475
* @param {string} url The url to be requested
7576
*
7677
* @throws {Boom.unauthorized} If requesting the access token failed
7778
*/
78-
function extendLifeCycle (server, options, url) {
79+
function extendLifeCycle (server, pluginOptions, options, url) {
7980
server.ext('onRequest', async (request, h) => {
8081
const requestOptions = getRequestOptions(request, options)
8182

@@ -87,7 +88,12 @@ function extendLifeCycle (server, options, url) {
8788

8889
request.headers.authorization = `Bearer ${token}`
8990
} catch (err) {
90-
throw raiseUnauthorized(errorMessages.apiKey, err.message, options.prefix.trim())
91+
throw raiseUnauthorized(
92+
errorMessages.apiKey,
93+
err.message,
94+
pluginOptions.schemeName,
95+
options.prefix.trim()
96+
)
9197
}
9298
}
9399

@@ -111,7 +117,7 @@ function init (server, pluginOptions) {
111117
const url = parseUrl(pluginOptions)
112118

113119
if (options) {
114-
extendLifeCycle(server, options, url)
120+
extendLifeCycle(server, pluginOptions, options, url)
115121
}
116122
}
117123

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async function handleKeycloakValidation (tkn, h) {
121121
await cache.set(store, tkn, userData, expiresIn)
122122
return h.authenticated(userData)
123123
} catch (err) {
124-
throw raiseUnauthorized(errorMessages.invalid, err.message)
124+
throw raiseUnauthorized(errorMessages.invalid, err.message, options.schemeName)
125125
}
126126
}
127127

@@ -140,14 +140,14 @@ async function handleKeycloakValidation (tkn, h) {
140140
*/
141141
async function validate (field, h = (data) => data) {
142142
if (!field) {
143-
throw raiseUnauthorized(errorMessages.missing)
143+
throw raiseUnauthorized(errorMessages.missing, null, options.schemeName)
144144
}
145145

146146
const tkn = token.create(field)
147147
const reply = fakeToolkit(h)
148148

149149
if (!tkn) {
150-
throw raiseUnauthorized(errorMessages.invalid)
150+
throw raiseUnauthorized(errorMessages.invalid, null, options.schemeName)
151151
}
152152

153153
const cached = await cache.get(store, tkn)

src/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ function verify (opts) {
117117
* @param {Error|null|undefined} err The error object
118118
* @param {string} message The error message
119119
* @param {string} reason The reason for the thrown error
120+
* @param {string} strategy The strategy name
120121
* @param {string} [scheme = 'Bearer'] The related scheme
121122
* @returns {Boom.unauthorized} The created `Boom` error
122123
*/
123-
function raiseUnauthorized (error, reason, scheme = 'Bearer') {
124+
function raiseUnauthorized (error, reason, strategy, scheme = 'Bearer') {
124125
return boom.unauthorized(
125126
error !== errorMessages.missing ? error : null,
126-
scheme,
127+
`${scheme} (${strategy})`,
127128
{
128-
strategy: 'keycloak-jwt',
129+
strategy,
129130
...(error === errorMessages.missing ? { error } : {}),
130131
...(reason && error !== reason ? { reason } : {})
131132
}

test/utils.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const test = require('ava')
33
const utils = require('../src/utils')
44

55
test('get boom error with default message', (t) => {
6-
const result = utils.raiseUnauthorized()
6+
const result = utils.raiseUnauthorized(null, null, 'keycloak-jwt')
77
t.truthy(result)
88
t.deepEqual(result, boom.unauthorized(undefined, 'Bearer', {
99
strategy: 'keycloak-jwt'
1010
}))
1111
})
1212

1313
test('get boom error with reason', (t) => {
14-
const result = utils.raiseUnauthorized(null, 'foobar')
14+
const result = utils.raiseUnauthorized(null, 'foobar', 'keycloak-jwt')
1515
t.truthy(result)
1616
t.deepEqual(result, boom.unauthorized(undefined, 'Bearer', {
1717
strategy: 'keycloak-jwt',
@@ -20,15 +20,15 @@ test('get boom error with reason', (t) => {
2020
})
2121

2222
test('get boom error with custom scheme', (t) => {
23-
const result = utils.raiseUnauthorized(null, null, 'custom')
23+
const result = utils.raiseUnauthorized(null, null, 'keycloak-jwt', 'custom')
2424
t.truthy(result)
2525
t.deepEqual(result, boom.unauthorized(undefined, 'custom', {
2626
strategy: 'keycloak-jwt'
2727
}))
2828
})
2929

3030
test('get boom error with error message', (t) => {
31-
const result = utils.raiseUnauthorized('foobar')
31+
const result = utils.raiseUnauthorized('foobar', null, 'keycloak-jwt')
3232
t.truthy(result)
3333
t.deepEqual(result, boom.unauthorized('foobar', 'Bearer', {
3434
strategy: 'keycloak-jwt'

0 commit comments

Comments
 (0)