You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-22Lines changed: 27 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@
15
15
---
16
16
17
17
## 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.
19
19
20
20
The modules [`standard`][standardjs] and [`ava`][avajs] are used to grant a high quality implementation.<br/>
21
21
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:
43
43
```js
44
44
consthapi=require('hapi');
45
45
46
-
constserver=hapi.server({
47
-
port:8888,
48
-
host:'localhost',
49
-
});
46
+
constserver=hapi.server({ port:8888 });
50
47
```
51
48
52
49
#### Registration
@@ -92,21 +89,24 @@ server.route([
92
89
}
93
90
}
94
91
},
95
-
])
92
+
]);
96
93
```
97
94
98
95
## API
99
96
#### Plugin Options
100
97
101
98
> 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.
> | (1) + (2) | x | x | x |`entitlement`| fine-grained |
109
106
>
107
+
> **\***: Plugin interacts with the Keycloak API<br/>
108
+
> **\*\***: Plugin validates token with help of the Keycloak API<br/>
109
+
>
110
110
> Please mind that the accurate strategy is 4-5x faster than the fine-grained one.<br/>
111
111
> **Hint:** If you define neither `secret` nor `public` nor `entitlement`, the plugin retrieves the public key itself from `{realmUrl}/protocol/openid-connect/certs`.
112
112
@@ -135,16 +135,17 @@ Optional. Default: `0`.
135
135
-`userInfo {Array.<?string>}` — List of properties which should be included in the `request.auth.credentials` object besides `scope` and `sub`.<br/>
136
136
Optional. Default: `[]`.<br/>
137
137
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/>
140
141
Optional. Default: `false`.
141
142
142
143
#### `await server.kjwt.validate(field {string})`
143
144
-`field {string}` — The `Bearer` field, including the scheme (`bearer`) itself.<br/>
144
145
Example: `bearer 12345.abcde.67890`.<br/>
145
146
Required.
146
147
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/>
148
149
If the token is invalid, the `result` is `false`. Otherwise it is an object containing all relevant credentials.
149
150
150
151
## Example
@@ -164,39 +165,43 @@ async function register (server, options) {
0 commit comments