Skip to content

Commit 7b01e33

Browse files
author
Max Marttinen
committed
implement mode:'optional' and fix broken tests
1 parent d973824 commit 7b01e33

10 files changed

+32
-48
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,15 @@ async function handleKeycloakValidation (tkn, h) {
139139
* @throws {Boom.unauthorized} If header is missing or has an invalid format
140140
*/
141141
async function validate (field, h = (data) => data) {
142+
if (!field) {
143+
throw raiseUnauthorized(errorMessages.missing)
144+
}
145+
142146
const tkn = token.create(field)
143147
const reply = fakeToolkit(h)
144148

145149
if (!tkn) {
146-
throw raiseUnauthorized(errorMessages.missing)
150+
throw raiseUnauthorized(errorMessages.invalid)
147151
}
148152

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

src/utils.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ function verify (opts) {
117117
* @returns {Boom.unauthorized} The created `Boom` error
118118
*/
119119
function raiseUnauthorized (error, reason, scheme = 'Bearer') {
120-
return boom.unauthorized(null, scheme, {
121-
strategy: 'keycloak-jwt',
122-
...(error ? { error } : {}),
123-
...(reason && error !== reason ? { reason } : {})
124-
})
120+
return boom.unauthorized(
121+
error !== errorMessages.missing ? error : null,
122+
scheme,
123+
{
124+
strategy: 'keycloak-jwt',
125+
...(error === errorMessages.missing ? { error } : {}),
126+
...(reason && error !== reason ? { reason } : {})
127+
}
128+
)
125129
}
126130

127131
/**
@@ -132,7 +136,7 @@ function raiseUnauthorized (error, reason, scheme = 'Bearer') {
132136
*/
133137
const errorMessages = {
134138
invalid: 'Invalid credentials',
135-
missing: 'Missing or invalid authorization header',
139+
missing: 'Missing authorization header',
136140
rpt: 'Retrieving the RPT failed',
137141
apiKey: 'Retrieving the token with the api key failed'
138142
}

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 strategy="keycloak-jwt", error="Invalid credentials", reason="Retrieving the RPT failed"')
87+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", reason="Retrieving the RPT failed", error="Invalid credentials"')
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 strategy="keycloak-jwt", error="Missing or invalid authorization header"')
98+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
9999
})

test/index.hapi-modes.spec.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,28 @@ test.afterEach.always('reset instances and prototypes', () => {
1111

1212
test('server method – authentication supports mode: "optional" - will succeed without auth', async (t) => {
1313
const server = await helpers.getServer(cfg)
14-
1514
const res = await server.inject({
1615
method: 'GET',
17-
url: '/mode-optional',
16+
url: '/mode-optional'
1817
})
1918

2019
t.truthy(res)
2120
t.is(res.statusCode, 200)
2221
})
2322

2423
test('server method – authentication supports mode: "optional" - will fail with invalid auth', async (t) => {
24+
const mockReq = helpers.mockRequest(fixtures.common.token, '/mode-optional')
2525
const server = await helpers.getServer(cfg)
26-
27-
const res = await server.inject({
28-
method: 'GET',
29-
url: '/mode-optional',
30-
headers: {
31-
authorization: `bearer invalid-token`
32-
}
33-
})
26+
const res = await server.inject(mockReq)
3427

3528
t.truthy(res)
3629
t.is(res.statusCode, 401)
37-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
3830
})
3931

4032
test('server method – authentication supports mode: "try" - will succeed with invalid auth', async (t) => {
33+
const mockReq = helpers.mockRequest(fixtures.common.token, '/mode-try')
4134
const server = await helpers.getServer(cfg)
42-
43-
const res = await server.inject({
44-
method: 'GET',
45-
url: '/mode-try',
46-
headers: {
47-
authorization: `bearer ${fixtures.composeJwt('expired')}`
48-
}
49-
})
35+
const res = await server.inject(mockReq)
5036

5137
t.truthy(res)
5238
t.is(res.statusCode, 200)

test/index.introspect.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 strategy="keycloak-jwt", error="Missing or invalid authorization header"')
80+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
8181
})

test/index.server.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 strategy="keycloak-jwt", error="Missing or invalid authorization header"')
55+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
5656
})
5757

5858
test('server method – authentication does fail – error', async (t) => {

test/index.verify.buffer.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 strategy="keycloak-jwt", error="Invalid credentials", reason="invalid token (expired)"')
52+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", reason="invalid token (expired)", error="Invalid credentials"')
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 strategy="keycloak-jwt", error="Missing or invalid authorization header"')
62+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
6363
})

test/index.verify.jwk.spec.js

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

5454
t.truthy(res)
5555
t.is(res.statusCode, 401)
56-
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials", reason="invalid token (expired)"')
56+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", reason="invalid token (expired)", error="Invalid credentials"')
5757
})
5858

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

6464
t.truthy(res)
6565
t.is(res.statusCode, 401)
66-
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Missing or invalid authorization header"')
66+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
6767
})

test/index.verify.pem.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 strategy="keycloak-jwt", error="Invalid credentials", reason="invalid token (expired)"')
52+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", reason="invalid token (expired)", error="Invalid credentials"')
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 strategy="keycloak-jwt", error="Missing or invalid authorization header"')
62+
t.is(res.headers['www-authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
6363
})

test/utils.spec.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,11 @@ test('get boom error with custom scheme', (t) => {
2727
}))
2828
})
2929

30-
test('get boom error with default message', (t) => {
31-
const result = utils.raiseUnauthorized('foobar')
32-
t.truthy(result)
33-
t.deepEqual(result, boom.unauthorized(null, 'Bearer', {
34-
strategy: 'keycloak-jwt',
35-
error: 'foobar'
36-
}))
37-
})
38-
3930
test('get boom error with error message', (t) => {
4031
const result = utils.raiseUnauthorized('foobar')
4132
t.truthy(result)
42-
t.deepEqual(result, boom.unauthorized(null, 'Bearer', {
43-
strategy: 'keycloak-jwt',
44-
error: 'foobar'
33+
t.deepEqual(result, boom.unauthorized('foobar', 'Bearer', {
34+
strategy: 'keycloak-jwt'
4535
}))
4636
})
4737

0 commit comments

Comments
 (0)