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
+83-59Lines changed: 83 additions & 59 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 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/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
This plugin is implemented in ECMAScript 6 without any transpilers like [`babel`][babel].<br/>
21
21
Additionally [`standard`][standardjs] and [`ava`][avajs] are used to grant a high quality implementation.<br/>
Define your routes and add `keycloak-jwt` when necessary. It is possible to define the necessary scope like documented by the [express.js middleware][keycloak-node]:
81
80
82
-
- To secure a resource with an application role for the current app, use the role name (e.g. `editor`).
83
-
- To secure a resource with an application role for a different app, prefix the role name (e.g. `other-app:creator`)
84
-
- To secure a resource with a realm role, prefix the role name with `realm:` (e.g. `realm:admin`).
81
+
- To secure an endpoint with a resource's role , use the role name (e.g. `editor`).
82
+
- To secure an endpoint with another resource's role, prefix the role name (e.g. `other-resource:creator`)
83
+
- To secure an endpoint with a realm role, prefix the role name with `realm:` (e.g. `realm:admin`).
84
+
- To secure an endpoint with [fine-grained scope definitions][rpt], prefix the Keycloak scopes with `scope:` (e.g. `scope:foo.READ`).
> By default, the Keycloak server has built-in [two ways to authenticate][client-auth] 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. If the signed JWTs are used as online strategy, ensure that the identifier of the related realm key is included in their header as `kid`.
110
+
> 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 |
118
+
>
119
+
> Please mind that the accurate strategy is 4-5x faster than the fine-grained one.<br/>
120
+
> **Hint:** If you define neither `secret` nor `public` nor `entitlement`, the plugin retrieves the public key itself from `{realmUrl}/protocol/openid-connect/certs`.
117
121
118
122
-`realmUrl {string}` – The absolute uri of the Keycloak realm.<br/>
-`secret {string}` – The related secret of the Keycloak client/application.<br/>
125
-
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/>
129
+
Defining this option enables the traditional method described in the OAuth2 specification and performs an [introspect][introspect] request.<br/>
126
130
Optional. Example: `1234-bar-4321-foo`<br/>
127
-
128
-
-`publicKey {string}` – The related public key of the Keycloak client/application.<br/>
129
-
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/>
130
-
Optional.
131
+
132
+
-`publicKey {string}` – The realm its public key related to the private key used to sign the token.<br/>
133
+
Defining this option enables the offline and non-live validation. The public key has to be in [PEM][pem] or [JWK][jwk] format.<br/>
134
+
Optional.
135
+
136
+
-`entitlement {boolean=true}` – The token should be validated with the entitlement API to enable fine-grained authorization. Enabling this option decelerates the process marginally. Mind that `false` is an invalid value.<br/>
137
+
Optional. Default: `undefined`.
131
138
132
139
-`minTimeBetweenJwksRequests {number}` – The minimum time between JWKS requests in seconds.<br/>
140
+
This is relevant for the online/non-live strategy retrieving JWKS from the Keycloak server.<br/>
133
141
The value have to be a positive integer.<br/>
134
142
Optional. Default: `0`.
135
143
136
-
-`cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox].<br/>
137
-
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache.<br/>
138
-
Optional. Default: `false`.
139
-
140
144
-`userInfo {Array.<?string>}` — List of properties which should be included in the `request.auth.credentials` object besides `scope` and `sub`.<br/>
141
145
Optional. Default: `[]`.<br/>
142
146
147
+
-`cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox]. If the property `iat` (issued at) or `exp` (expiresAt) is undefined, the plugin uses 60 seconds as default TTL.<br/>
148
+
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache.<br/>
0 commit comments