Skip to content

Commit 1f41741

Browse files
committed
fix docs
1 parent dd78fbd commit 1f41741

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

README.md

Lines changed: 27 additions & 22 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] 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/authorization 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 even though the user data don't get changed until the token expires. It plays well with the [hapi.js][hapijs]-integrated [authentication/authorization 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
The modules [`standard`][standardjs] and [`ava`][avajs] are used to grant a high quality implementation.<br/>
2121
This major release supports just [hapi.js](https://github.com/hapijs/hapi) `>=v17.0.0` and node `>=v8.0.0` — to support older versions please use `v2.1.0`.
@@ -43,10 +43,7 @@ Afterwards create your hapi server if not already done:
4343
``` js
4444
const hapi = require('hapi');
4545

46-
const server = hapi.server({
47-
port: 8888,
48-
host: 'localhost',
49-
});
46+
const server = hapi.server({ port: 8888 });
5047
```
5148

5249
#### Registration
@@ -92,21 +89,24 @@ server.route([
9289
}
9390
}
9491
},
95-
])
92+
]);
9693
```
9794

9895
## API
9996
#### Plugin Options
10097

10198
> By default, the Keycloak server has built-in [two ways to authenticate][client-auth] the client: client ID and client secret **(1)**, or with a signed JWT **(2)**. This plugin supports both. If a non-live strategy is used, ensure that the identifier of the related realm key is included in their header as `kid`. Check the description of `secret`/`publicKey`/`entitlement` and the [terminology][rpt-terms] for further information.
10299
>
103-
> | Strategies | Online | Live |[Scopes][rpt] | Truthy Option | Note |
100+
> | Strategies | Online* | Live** |[Scopes][rpt] | Truthy Option | Note |
104101
> |:-----------|:------:|:----:|:-------------:|:---------------|:-------------|
105102
> | (1) + (2) | | | | `publicKey` | fast |
106103
> | (1) + (2) | x | | | | flexible |
107104
> | (1) | x | x | | `secret` | accurate |
108105
> | (1) + (2) | x | x | x | `entitlement` | fine-grained |
109106
>
107+
> **\***: Plugin interacts with the Keycloak API<br/>
108+
> **\*\***: Plugin validates token with help of the Keycloak API<br/>
109+
>
110110
> Please mind that the accurate strategy is 4-5x faster than the fine-grained one.<br/>
111111
> **Hint:** If you define neither `secret` nor `public` nor `entitlement`, the plugin retrieves the public key itself from `{realmUrl}/protocol/openid-connect/certs`.
112112
@@ -135,16 +135,17 @@ Optional. Default: `0`.
135135
- `userInfo {Array.<?string>}` — List of properties which should be included in the `request.auth.credentials` object besides `scope` and `sub`.<br/>
136136
Optional. Default: `[]`.<br/>
137137

138-
- `cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox]. If the property `exp` (expiresAt) is undefined, the plugin uses 60 seconds as default TTL. Otherwise the cache entry expires as soon as the token itself expires.<br/>
139-
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache.<br/>
138+
- `cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox]. If the property `exp` (= 'expires at') is undefined, the plugin uses 60 seconds as default TTL. Otherwise the cache entry expires as soon as the token itself expires.<br/>
139+
Please mind that an enabled cache leads to disabled live validation after the related token is cached once.<br/>
140+
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache. Otherwise just drop in your own cache configuration.<br/>
140141
Optional. Default: `false`.
141142

142143
#### `await server.kjwt.validate(field {string})`
143144
- `field {string}` — The `Bearer` field, including the scheme (`bearer`) itself.<br/>
144145
Example: `bearer 12345.abcde.67890`.<br/>
145146
Required.
146147

147-
If an error occurs, it get thrown — so take care and implement a kind of catching.<br/>
148+
If an error occurs, it gets thrown — so take care and implement a kind of catching.<br/>
148149
If the token is invalid, the `result` is `false`. Otherwise it is an object containing all relevant credentials.
149150

150151
## Example
@@ -164,39 +165,43 @@ async function register (server, options) {
164165
}
165166
},
166167
handler (req, reply) {
167-
reply(req.auth.credentials)
168+
reply(req.auth.credentials);
168169
}
169170
}
170171
}
171-
])
172+
]);
172173
}
173174

174175
module.exports = {
175176
register,
176177
name: 'example-routes',
177178
version: '0.0.1'
178-
}
179+
};
179180
```
180181

181182
#### `index.js`
182183
``` js
183-
const hapi = require('hapi')
184-
const authKeycloak = require('hapi-auth-keycloak')
185-
const routes = require('./routes')
184+
const hapi = require('hapi');
185+
const authKeycloak = require('hapi-auth-keycloak');
186+
const routes = require('./routes');
186187

187-
const server = hapi.server({ port: 3000, host: 'localhost' })
188+
const server = hapi.server({ port: 3000 });
188189

189190
const options = {
190191
realmUrl: 'https://localhost:8080/auth/realms/testme',
191192
clientId: 'foobar',
192193
minTimeBetweenJwksRequests: 15,
193194
cache: true,
194195
userInfo: ['name', 'email']
195-
}
196+
};
196197

197-
process.on('SIGINT', () => {
198-
server.stop().then((err) => process.exit(err ? 1 : 0))
199-
})
198+
process.on('SIGINT', async () => {
199+
try
200+
await server.stop();
201+
} catch (err) {
202+
process.exit(err ? 1 : 0);
203+
}
204+
});
200205

201206
(async () => {
202207
try {
@@ -208,7 +213,7 @@ process.on('SIGINT', () => {
208213
} catch (err) {
209214
console.error(err);
210215
}
211-
})()
216+
})();
212217
```
213218

214219
## Developing and Testing

0 commit comments

Comments
 (0)