Skip to content

Commit f55ad5d

Browse files
authored
Merge pull request #5 from felixheck/release/2.0.0-rc
Release/2.0.0-rc
2 parents d9fbe5c + 93b05d9 commit f55ad5d

16 files changed

+1441
-3519
lines changed

README.md

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
---
1616

1717
## Introduction
18-
**hapi-auth-keycloak** is a plugin for [hapi.js][hapijs] which enables to protect your endpoints in a smart but professional manner using [Keycloak][keycloak] as authentication service. It is inspired by the related [express.js middleware][keycloak-node]. The plugin validates the passed [`Bearer` token][bearer] online with help of the [Keycloak][keycloak] server and optionally caches successfully validated tokens and the related user data using [`catbox`][catbox]. The caching enables a fast processing although the user data don't get changed until the token expires. It plays well with the [hapi.js][hapijs]-integrated [authentication feature][hapi-route-options]. Besides the authentication strategy it is possible to validate tokens by yourself, e.g. to authenticate incoming websocket or queue messages.
18+
**hapi-auth-keycloak** is a plugin for [hapi.js][hapijs] which enables to protect your endpoints in a smart but professional manner using [Keycloak][keycloak] as authentication service. It is inspired by the related [express.js middleware][keycloak-node]. The plugin validates the passed [`Bearer` token][bearer] offline with a provided public key or online with help of the [Keycloak][keycloak] server. Optionally, the successfully validated tokens and the related user data get cached using [`catbox`][catbox]. The caching enables a fast processing although the user data don't get changed until the token expires. It plays well with the [hapi.js][hapijs]-integrated [authentication feature][hapi-route-options]. Besides the authentication strategy it is possible to validate tokens by yourself, e.g. to authenticate incoming websocket or queue messages.
1919

2020
This plugin is implemented in ECMAScript 6 without any transpilers like [`babel`][babel].<br/>
2121
Additionally [`standard`][standardjs] and [`ava`][avajs] are used to grant a high quality implementation.<br/>
@@ -60,11 +60,10 @@ Finally register the plugin, set the correct options and the authentication stra
6060
server.register({
6161
register: authKeycloak,
6262
options: {
63-
client: {
64-
realmUrl: 'https://localhost:8080/auth/realms/testme',
65-
clientId: 'foobar',
66-
secret: '1234-bar-4321-foo'
67-
},
63+
realmUrl: 'https://localhost:8080/auth/realms/testme',
64+
clientId: 'foobar',
65+
secret: '1234-bar-4321-foo',
66+
minTimeBetweenJwksRequests: 15,
6867
cache: {},
6968
userInfo: ['name', 'email']
7069
}
@@ -108,27 +107,42 @@ server.route([
108107
## API
109108
#### Plugin Options
110109

111-
- `client {Object}` — The configuration of [`keycloak-auth-utils`][keycloak-auth-utils] its [`GrantManager`][keycloak-auth-utils-gm]. The configuration requires at least:
112-
- `realmUrl {string}`: The absolute uri of the Keycloak realm<br/>
113-
Example: `https://localhost:8080/auth/realms/testme`
114-
- `clientId {string}` The identifier of the Keycloak client<br/>
115-
Example: `foobar`
116-
- `secret {string}` The related secret of the Keycloak client<br/>
117-
Example: `1234-bar-4321-foo`
110+
> **Hint**: By default, the Keycloak server has built-in two ways to authenticate the client: client ID and client secret, or with a signed JWT. This plugin supports both. Check the description of `secret` and `publicKey` for further information.
111+
>
112+
> If the signed JWTs are used as online strategy, ensure that the identifier of the related realm key (`kid`) is included in their header.
113+
>
114+
> | Strategy | Online | Option |
115+
> |:------------|:------:|:------------|
116+
> | ID + Secret | x | `secret` |
117+
> | Signed JWT | x | |
118+
> | Signed JWT | | `publicKey` |
119+
120+
- `realmUrl {string}`: The absolute uri of the Keycloak realm.<br/>
121+
Required. Example: `https://localhost:8080/auth/realms/testme`<br/>
122+
123+
- `clientId {string}` The identifier of the Keycloak client/application.<br/>
124+
Required. Example: `foobar`<br/>
125+
126+
- `secret {string}` The related secret of the Keycloak client/application.<br/>
127+
Defining this option enables the traditional method described in the OAuth2 specification. To perform an almost offline validation enable the cache — a simple offline verfication with symmetric keys is not provided for security reasons.<br/>
128+
Optional. Example: `1234-bar-4321-foo`<br/>
118129

119-
Furthermore it may be necessary to reduce `minTimeBetweenJwksRequests`.<br/>
120-
Required.
130+
- `publicKey {string}` The related public key of the Keycloak client/application.<br/>
131+
Defining this option enables the offline validation using signed JWTs. The public key has to be in [PEM][pem] or [JWK][jwk] format. If you define neither `secret` nor `public` key, the plugin assumes that a signed JWT has to be validated – it retrieves the public key itself from `{realmUrl}/protocol/openid-connect/certs`. The offline strategy its performance is higher but the online strategy is the most flexible one.<br/>
132+
Optional.
121133

122-
- `cache {Object|false}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox].<br/>
123-
If `false` the cache is disabled. Use an empty object (`{}`) to use the built-in default cache.<br/>
134+
- `minTimeBetweenJwksRequests {number}` – The minimum time between JWKS requests in seconds.<br/>
135+
The value have to be a positive integer.<br/>
136+
Optional. Default: `0`.
137+
138+
- `cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox].<br/>
139+
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache.<br/>
124140
Optional. Default: `false`.
125141

126142
- `userInfo {Array.<?string>}` — List of properties which should be included in the `request.auth.credentials` object besides `scope` and `sub`.<br/>
127143
Optional. Default: `[]`.<br/>
128144

129145
#### `server.kjwt.validate(field {string}, done {Function})`
130-
Uses internally [`GrantManager.prototype.validateAccessToken()`][keycloak-auth-utils-gm-validate].
131-
132146
- `field {string}` — The `Bearer` field, including the scheme (`bearer`) itself.<br/>
133147
Example: `bearer 12345.abcde.67890`.<br/>
134148
Required.
@@ -173,11 +187,10 @@ process.on('SIGINT', () => {
173187
server.register({
174188
register: authKeycloak,
175189
options: {
176-
client: {
177-
realmUrl: 'https://localhost:8080/auth/realms/testme',
178-
clientId: 'foobar',
179-
secret: '1234-bar-4321-foo'
180-
},
190+
realmUrl: 'https://localhost:8080/auth/realms/testme',
191+
clientId: 'foobar',
192+
secret: '1234-bar-4321-foo',
193+
minTimeBetweenJwksRequests: 15,
181194
cache: {},
182195
userInfo: ['name', 'email']
183196
}
@@ -227,7 +240,5 @@ For further information read the [contributing guideline](CONTRIBUTING.md).
227240
[catbox]: https://github.com/hapijs/catbox
228241
[bearer]: https://tools.ietf.org/html/rfc6750
229242
[hapi-route-options]: https://hapijs.com/api#route-options
230-
[keycloak-auth-utils]: http://www.keycloak.org/keycloak-nodejs-auth-utils/
231-
[keycloak-auth-utils-gm]: http://www.keycloak.org/keycloak-nodejs-auth-utils/grant-manager.js.html
232-
[keycloak-auth-utils-gm-obtain]: http://www.keycloak.org/keycloak-nodejs-auth-utils/grant-manager.js.html#obtainDirectly
233-
[keycloak-auth-utils-gm-validate]: http://www.keycloak.org/keycloak-nodejs-auth-utils/grant-manager.js.html#validateAccessToken
243+
[jwk]: https://tools.ietf.org/html/rfc7517
244+
[pem]: https://tools.ietf.org/html/rfc1421

0 commit comments

Comments
 (0)