@@ -24,17 +24,31 @@ More about Keycloak: http://www.keycloak.org/
2424## Using the keycloak-backend module
2525### Configuration
2626``` js
27- const keycloak = require (' keycloak-backend' )({
27+ const Keycloak = require (' keycloak-backend' ).Keycloak
28+ const keycloak = new Keycloak ({
2829 " realm" : " realm-name" ,
2930 " keycloak_base_url" : " https://keycloak.example.org" ,
3031 " client_id" : " super-secure-client" ,
3132 " username" : " user@example.org" ,
3233 " password" : " passw0rd" ,
3334 " is_legacy_endpoint" : false
34- });
35+ })
3536```
3637> The ` is_legacy_endpoint ` configuration property should be TRUE for older Keycloak versions (under 18)
3738
39+ For TypeScript:
40+ ``` ts
41+ import { Keycloak } from " keycloak-backend"
42+ const keycloak = new Keycloak ({
43+ " realm" : " realm-name" ,
44+ " keycloak_base_url" : " https://keycloak.example.org" ,
45+ " client_id" : " super-secure-client" ,
46+ " username" : " user@example.org" ,
47+ " password" : " passw0rd" ,
48+ " is_legacy_endpoint" : false
49+ })
50+ ```
51+
3852### Generating access tokens
3953``` js
4054const accessToken = await keycloak .accessToken .get ()
@@ -45,30 +59,33 @@ request.get('http://service.example.org/api/endpoint', {
4559 ' auth' : {
4660 ' bearer' : await keycloak .accessToken .get ()
4761 }
48- });
62+ })
4963```
5064
5165### Validating access tokens
5266#### Online validation
5367This method requires online connection to the Keycloak service to validate the access token. It is highly secure since it also check for possible token invalidation. The disadvantage is that a request to the Keycloak service happens on every validation:
5468``` js
55- const token = await keycloak .jwt .verify (accessToken);
56- // console.log(token.isExpired());
57- // console.log(token.hasRealmRole('user'));
58- // console.log(token.hasApplicationRole('app-client-name', 'some-role'));
69+ const token = await keycloak .jwt .verify (accessToken)
70+ // console.log(token.isExpired())
71+ // console.log(token.hasRealmRole('user'))
72+ // console.log(token.hasApplicationRole('app-client-name', 'some-role'))
5973```
6074
6175#### Offline validation
6276This method perform offline JWT verification against the access token using the Keycloak Realm public key. Performance is higher compared to the online method, as a disadvantage no access token invalidation on Keycloak server is checked:
6377``` js
64- const cert = fs .readFileSync (' public_cert.pem' );
65- const token = await keycloak .jwt .verifyOffline (accessToken, cert);
66- // console.log(token.isExpired());
67- // console.log(token.hasRealmRole('user'));
68- // console.log(token.hasApplicationRole('app-client-name', 'some-role'));
78+ const cert = fs .readFileSync (' public_cert.pem' )
79+ const token = await keycloak .jwt .verifyOffline (accessToken, cert)
80+ // console.log(token.isExpired())
81+ // console.log(token.hasRealmRole('user'))
82+ // console.log(token.hasApplicationRole('app-client-name', 'some-role'))
6983```
7084
7185## Breaking changes
86+ ### v4
87+ - Codebase migrated from JavaScript to TypeScript. Many thanks to @neferin12
88+
7289### v3
7390- The ` UserManager ` class was dropped
7491- The ` auth-server-url ` config property was changed to ` keycloak_base_url `
0 commit comments