Skip to content

Commit dd78fbd

Browse files
committed
fix error handling
1 parent 592954f commit dd78fbd

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

src/index.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { GrantManager } = require('keycloak-auth-utils')
33
const KeycloakToken = require('keycloak-auth-utils/lib/token')
44
const cache = require('./cache')
55
const token = require('./token')
6-
const { raiseError, errors, fakeToolkit, verify } = require('./utils')
6+
const { raiseUnauthorized, errors, fakeToolkit, verify } = require('./utils')
77
const pkg = require('../package.json')
88

99
/**
@@ -45,7 +45,7 @@ async function verifySignedJwt (tkn) {
4545
* @param {string} tkn The token to be validated
4646
* @returns {Promise} The error-handled promise
4747
*
48-
* @throws {Error} If token is invalid or request fails
48+
* @throws {Error} If token is invalid or request failed
4949
*/
5050
async function introspect (tkn) {
5151
try {
@@ -67,7 +67,7 @@ async function introspect (tkn) {
6767
* @param {string} tkn The token to be used for authentication
6868
* @returns {Promise} The modified, non-error-handling promise
6969
*
70-
* @throws {Error} If request failed or token is invalid
70+
* @throws {Error} If token is invalid or request failed
7171
*/
7272
async function getRpt (tkn) {
7373
let data = {}
@@ -109,7 +109,7 @@ function getValidateFn () {
109109
* @param {string} tkn The token to be validated
110110
* @param {Function} h The toolkit
111111
*
112-
* @throws {Boom.unauthorized} If validation fails
112+
* @throws {Boom.unauthorized} If previous validation fails
113113
*/
114114
async function handleKeycloakValidation (tkn, h) {
115115
try {
@@ -120,7 +120,7 @@ async function handleKeycloakValidation (tkn, h) {
120120
await cache.set(store, tkn, userData, expiresIn)
121121
return h.authenticated(userData)
122122
} catch (err) {
123-
throw raiseError('unauthorized', err, errors.invalid)
123+
throw raiseUnauthorized(err, errors.invalid)
124124
}
125125
}
126126

@@ -142,7 +142,7 @@ async function validate (field, h = (data) => data) {
142142
const reply = fakeToolkit(h)
143143

144144
if (!tkn) {
145-
throw raiseError('unauthorized', null, errors.missing)
145+
throw raiseUnauthorized(null, errors.missing)
146146
}
147147

148148
const cached = await cache.get(store, tkn)
@@ -180,7 +180,7 @@ function strategy (server) {
180180
* @param {Hapi.Server} server The created server instance
181181
* @param {Object} opts The plugin related options
182182
*/
183-
function plugin (server, opts) {
183+
function register (server, opts) {
184184
options = verify(opts)
185185
manager = new GrantManager(options)
186186
store = cache.create(server, options.cache)
@@ -189,7 +189,4 @@ function plugin (server, opts) {
189189
server.decorate('server', 'kjwt', { validate })
190190
}
191191

192-
module.exports = {
193-
register: plugin,
194-
pkg
195-
}
192+
module.exports = { register, pkg }

src/utils.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ function verify (opts) {
6464
* @function
6565
* @public
6666
*
67-
* Get `Boom` error with bound scheme.
68-
* If error is available, use its message.
69-
* Otherwise the provided message.
67+
* Get `Boom.unauthorized` error with bound scheme and
68+
* further attributes If error is available, use its
69+
* message. Otherwise the provided message.
7070
*
71-
* @param {string} type The `Boom` error type
7271
* @param {Error|null} err The error object
7372
* @param {string} msg The error message
7473
* @returns {Boom} The created `Boom` error
7574
*/
76-
function raiseError (type, err, msg) {
77-
return boom[type](err ? err.message : msg, 'Bearer')
75+
function raiseUnauthorized (err, msg) {
76+
return boom.unauthorized(err ? err.message : msg, 'Bearer', {
77+
strategy: 'keycloak-jwt'
78+
})
7879
}
7980

8081
const errors = {
@@ -101,7 +102,7 @@ function fakeToolkit (h) {
101102
}
102103

103104
module.exports = {
104-
raiseError,
105+
raiseUnauthorized,
105106
errors,
106107
fakeToolkit,
107108
verify

test/index.entitlement.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test('authentication does fail – invalid token', async (t) => {
8484

8585
t.truthy(res)
8686
t.is(res.statusCode, 401)
87-
t.is(res.headers['www-authenticate'], 'Bearer error="Retrieving the RPT failed"')
87+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Retrieving the RPT failed"')
8888
})
8989

9090
test('authentication does fail – invalid header', async (t) => {
@@ -95,5 +95,5 @@ test('authentication does fail – invalid header', async (t) => {
9595

9696
t.truthy(res)
9797
t.is(res.statusCode, 401)
98-
t.is(res.headers['www-authenticate'], 'Bearer error="Missing or invalid authorization header"')
98+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Missing or invalid authorization header"')
9999
})

test/index.introspect.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test('authentication does fail – invalid token', async (t) => {
6666

6767
t.truthy(res)
6868
t.is(res.statusCode, 401)
69-
t.is(res.headers['www-authenticate'], 'Bearer error="Invalid credentials"')
69+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
7070
})
7171

7272
test('authentication does fail – invalid header', async (t) => {
@@ -77,5 +77,5 @@ test('authentication does fail – invalid header', async (t) => {
7777

7878
t.truthy(res)
7979
t.is(res.statusCode, 401)
80-
t.is(res.headers['www-authenticate'], 'Bearer error="Missing or invalid authorization header"')
80+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Missing or invalid authorization header"')
8181
})

test/index.server.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('server method – authentication does fail – invalid token', async (t) =
4242
t.truthy(err)
4343
t.truthy(err.isBoom)
4444
t.is(err.output.statusCode, 401)
45-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="Invalid credentials"')
45+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
4646
})
4747

4848
test('server method – authentication does fail – invalid header', async (t) => {
@@ -52,7 +52,7 @@ test('server method – authentication does fail – invalid header', async (t)
5252
t.truthy(err)
5353
t.truthy(err.isBoom)
5454
t.is(err.output.statusCode, 401)
55-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="Missing or invalid authorization header"')
55+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Missing or invalid authorization header"')
5656
})
5757

5858
test('server method – authentication does fail – error', async (t) => {
@@ -64,5 +64,5 @@ test('server method – authentication does fail – error', async (t) => {
6464
t.truthy(err)
6565
t.truthy(err.isBoom)
6666
t.is(err.output.statusCode, 401)
67-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="Invalid credentials"')
67+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
6868
})

test/index.verify.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test('authentication does fail – expired token', async (t) => {
4949

5050
t.truthy(res)
5151
t.is(res.statusCode, 401)
52-
t.is(res.headers['www-authenticate'], 'Bearer error="invalid token (expired)"')
52+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="invalid token (expired)"')
5353
})
5454

5555
test('authentication does fail – invalid header', async (t) => {
@@ -59,5 +59,5 @@ test('authentication does fail – invalid header', async (t) => {
5959

6060
t.truthy(res)
6161
t.is(res.statusCode, 401)
62-
t.is(res.headers['www-authenticate'], 'Bearer error="Missing or invalid authorization header"')
62+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Missing or invalid authorization header"')
6363
})

test/utils.spec.js

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

55
test('get boom error with default message', (t) => {
6-
const result = utils.raiseError('badRequest')
6+
const result = utils.raiseUnauthorized()
77
t.truthy(result)
8-
t.deepEqual(result, boom.badRequest(undefined, 'Bearer'))
8+
t.deepEqual(result, boom.unauthorized(undefined, 'Bearer', {
9+
strategy: 'keycloak-jwt'
10+
}))
911
})
1012

1113
test('get boom error with default message', (t) => {
12-
const result = utils.raiseError('badRequest', undefined, 'foobar')
14+
const result = utils.raiseUnauthorized(undefined, 'foobar')
1315
t.truthy(result)
14-
t.deepEqual(result, boom.badRequest('foobar', 'Bearer'))
16+
t.deepEqual(result, boom.unauthorized('foobar', 'Bearer', {
17+
strategy: 'keycloak-jwt'
18+
}))
1519
})
1620

1721
test('get boom error with error message', (t) => {
1822
const mockErr = new Error('barfoo')
19-
const result = utils.raiseError('badRequest', mockErr, 'foobar')
23+
const result = utils.raiseUnauthorized(mockErr, 'foobar')
2024
t.truthy(result)
21-
t.deepEqual(result, boom.badRequest(mockErr.message, 'Bearer'))
25+
t.deepEqual(result, boom.unauthorized(mockErr.message, 'Bearer', {
26+
strategy: 'keycloak-jwt'
27+
}))
2228
})
2329

2430
test('decorate callback function with `authenticated`', (t) => {

0 commit comments

Comments
 (0)