keycloak-api-manager 3.1.0 β 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/workspace.xml +2 -1
- package/README.md +95 -0
- package/index.js +1 -1
- package/package.json +1 -1
package/.idea/workspace.xml
CHANGED
|
@@ -83,7 +83,8 @@
|
|
|
83
83
|
<updated>1759849149064</updated>
|
|
84
84
|
<workItem from="1759849150239" duration="1214000" />
|
|
85
85
|
<workItem from="1759917554117" duration="69806000" />
|
|
86
|
-
<workItem from="1761132079959" duration="
|
|
86
|
+
<workItem from="1761132079959" duration="3320000" />
|
|
87
|
+
<workItem from="1762186403063" duration="2616000" />
|
|
87
88
|
</task>
|
|
88
89
|
<servers />
|
|
89
90
|
</component>
|
package/README.md
CHANGED
|
@@ -200,6 +200,101 @@ Parameters:
|
|
|
200
200
|
- refreshToken: [Optional] string containing a valid refresh token to request a new access token when using the refresh_token grant type.
|
|
201
201
|
---
|
|
202
202
|
|
|
203
|
+
## π§° Available Helper Functions
|
|
204
|
+
|
|
205
|
+
### `function setConfig(config)`
|
|
206
|
+
This function updates the runtime configuration of the Keycloak-api-manager Admin Client instance.
|
|
207
|
+
It allows switching the target realm, base URL, or HTTP request options without reinitializing the client or re-authenticating.
|
|
208
|
+
Itβs useful when you need to interact with multiple realms or environments dynamically using the same admin client instance.
|
|
209
|
+
|
|
210
|
+
**` -- @parameters -- `**
|
|
211
|
+
- config: is a JSON object that accepts the following parameters:
|
|
212
|
+
- realmName: [optional] The name of the target realm for subsequent API requests.
|
|
213
|
+
- baseUrl: [optional] The base URL of the Keycloak server (e.g., https://auth.example.com).
|
|
214
|
+
- requestOptions: [optional] Custom HTTP options (headers, timeout, etc.) applied to API calls.
|
|
215
|
+
- realmPath: [optional] A custom realm path if your Keycloak instance uses a non-standard realm route.
|
|
216
|
+
- other fields
|
|
217
|
+
|
|
218
|
+
**` -- @notes -- `**
|
|
219
|
+
Calling setConfig does not perform authentication
|
|
220
|
+
- it only changes configuration values in memory.
|
|
221
|
+
- The authentication token already stored in the admin client remains active until it expires.
|
|
222
|
+
- Only the properties explicitly passed in the config object are updated; all others remain unchanged.
|
|
223
|
+
|
|
224
|
+
If the authenticated user does not have permissions in the new realmName, subsequent calls may fail with a 403 or 404.
|
|
225
|
+
|
|
226
|
+
Typically used in multi-realm or multi-environment management scripts.
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
const KcAdminClient = require('keycloak-api-manager');
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
// Switch context to another realm dynamically
|
|
233
|
+
kcAdminClient.setConfig({
|
|
234
|
+
realmName: 'customer-realm',
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// All subsequent API calls will target "customer-realm"
|
|
238
|
+
const users = await kcAdminClient.users.find();
|
|
239
|
+
console.log(users);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### `function getToken()`
|
|
243
|
+
This function retrieves the current authentication tokens used by the Keycloak-api-manager Admin Client to communicate with the Keycloak REST API.
|
|
244
|
+
It returns both the access token (used for API authorization) and the refresh token (used to renew the session when the access token expires).
|
|
245
|
+
|
|
246
|
+
**` -- @returns -- `**
|
|
247
|
+
A JSON object containing:
|
|
248
|
+
- accessToken: The active access token string currently held by the Keycloak Admin Client.
|
|
249
|
+
- refreshToken: The corresponding refresh token string, if available, used to request a new access token without re-authentication.
|
|
250
|
+
|
|
251
|
+
**` -- @notes -- `**
|
|
252
|
+
The tokens are managed internally by the Keycloak Admin Client after successful authentication via kcAdminClient.auth().
|
|
253
|
+
The accessToken typically expires after a short period (e.g., 60 seconds by default).
|
|
254
|
+
You can use these tokens to call Keycloak REST endpoints manually or to debug authorization issues.
|
|
255
|
+
If the client is not authenticated or the session has expired, both values may be undefined.
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
const KcAdminClient = require('keycloak-api-manager');
|
|
259
|
+
|
|
260
|
+
// Example: retrieve and print current tokens
|
|
261
|
+
try {
|
|
262
|
+
const tokens = KcAdminClient.getToken();
|
|
263
|
+
console.log('Access Token:', tokens.accessToken);
|
|
264
|
+
console.log('Refresh Token:', tokens.refreshToken);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.error('Failed to retrieve tokens:', error);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
```
|
|
270
|
+
### `function auth(credentials)`
|
|
271
|
+
This function allows a user or client to authenticate against a Keycloak realm and obtain an access token (and optionally a refresh token).
|
|
272
|
+
It sends a direct HTTP POST request to the Keycloak OpenID Connect token endpoint using the provided credentials.
|
|
273
|
+
|
|
274
|
+
**` -- @parameters -- `**
|
|
275
|
+
credentials: a JSON object containing authentication details. Supported fields include:
|
|
276
|
+
- username: [optional] Username of the user (required for password grant).
|
|
277
|
+
- password: [optional] Password of the user (required for password grant).
|
|
278
|
+
- grant_type: [required] The OAuth2 grant type (e.g. "password", "client_credentials", "refresh_token").
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
const KeycloakManager = require('keycloak-api-manager');
|
|
283
|
+
|
|
284
|
+
// Example: authenticate a user via password grant
|
|
285
|
+
try {
|
|
286
|
+
const tokenResponse = await KeycloakManager.auth({
|
|
287
|
+
username: "demo",
|
|
288
|
+
password: "demo123",
|
|
289
|
+
grant_type: "password",
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
console.log("Access Token:", tokenResponse.access_token);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
console.error("Authentication failed:", error);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
```
|
|
203
298
|
|
|
204
299
|
## π§ Available Admin Functions
|
|
205
300
|
All administrative functions that rely on Keycloak's Admin API must be invoked using the
|
package/index.js
CHANGED
|
@@ -96,7 +96,7 @@ exports.setConfig=function(configToOverride){
|
|
|
96
96
|
}
|
|
97
97
|
//TODO: Remove da documentare
|
|
98
98
|
// restituisce il token utilizzato dalla libreria per comunicare con la keycloak API
|
|
99
|
-
exports.getToken=function(
|
|
99
|
+
exports.getToken=function(){
|
|
100
100
|
return({
|
|
101
101
|
accessToken:kcAdminClient.accessToken,
|
|
102
102
|
refreshToken:kcAdminClient.refreshToken,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keycloak-api-manager",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Keycloak-api-manager is a lightweight Node.js wrapper for the Keycloak Admin REST API. It provides an easy-to-use functional methods and functions to manage realms, users, roles, clients, groups, and permissions directly from your application code β just like you would from the Keycloak admin console.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|