keycloak-api-manager 5.0.2 → 5.0.3

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/README.md CHANGED
@@ -68,6 +68,8 @@ In Keycloak 26.x, management-permissions APIs used by group/user fine-grained te
68
68
  - `auth(credentials)`
69
69
  - `stop()`
70
70
 
71
+ `auth(credentials)` is an OIDC token endpoint helper for login/token grant flows (user/client). It does not replace the internal admin session configured by `configure()`.
72
+
71
73
  Configured handler namespaces:
72
74
 
73
75
  - `realms`
@@ -180,7 +180,7 @@ KeycloakManager.setConfig({
180
180
 
181
181
  ### Notes
182
182
 
183
- - `setConfig()` does NOT re-authenticate. It only updates the context.
183
+ - `setConfig()` does NOT perform token/login calls. It only updates the runtime context.
184
184
  - Changing `realmName` affects all subsequent API calls until changed again.
185
185
  - The access token remains valid across realm switches (as long as the authenticated user/service account has permissions).
186
186
 
@@ -236,7 +236,11 @@ const response = await axios.get('https://keycloak.example.com/admin/realms/mast
236
236
 
237
237
  ## auth()
238
238
 
239
- Re-authenticate with new or updated credentials. Use this to switch users or refresh authentication manually.
239
+ Request tokens from Keycloak via the OIDC token endpoint.
240
+
241
+ This method is intended for application-level login/token flows (for users, service clients, or third-party integrations) using this package as a wrapper.
242
+
243
+ It does **not** reconfigure or replace the internal admin session created by `configure()`.
240
244
 
241
245
  **Syntax:**
242
246
  ```javascript
@@ -247,18 +251,32 @@ await KeycloakManager.auth(credentials)
247
251
 
248
252
  #### credentials (Object) ⚠️ Required
249
253
 
250
- Same structure as `configure()` credentials parameter.
254
+ Form parameters sent to `/protocol/openid-connect/token`.
255
+
256
+ Common fields:
257
+
258
+ | Property | Type | Required | Description |
259
+ |----------|------|----------|-------------|
260
+ | `grant_type` | string | ⚠️ Yes | OAuth2 grant type (`password`, `client_credentials`, `refresh_token`, `authorization_code`) |
261
+ | `username` | string | 📋 Conditional | Required for `password` grant |
262
+ | `password` | string | 📋 Conditional | Required for `password` grant |
263
+ | `client_id` | string | 📋 Optional | If omitted, runtime `clientId` from `configure()` is used |
264
+ | `client_secret` | string | 📋 Optional | If omitted, runtime `clientSecret` from `configure()` is used |
265
+ | `refresh_token` | string | 📋 Conditional | Required for `refresh_token` grant |
266
+ | `scope` | string | 📋 Optional | OAuth scopes (e.g. `openid profile email offline_access`) |
267
+ | `code` | string | 📋 Conditional | Required for `authorization_code` grant |
268
+ | `redirect_uri` | string | 📋 Conditional | Required for `authorization_code` grant |
251
269
 
252
270
  ### Returns
253
271
 
254
- **Promise\<void\>** - Resolves when re-authentication succeeds
272
+ **Promise\<Object\>** - Token payload returned by Keycloak (e.g. `access_token`, `refresh_token`, `expires_in`, `token_type`)
255
273
 
256
274
  ### Examples
257
275
 
258
- #### Switch User
276
+ #### Resource Owner Password Login
259
277
 
260
278
  ```javascript
261
- // Initial authentication as admin
279
+ // Admin setup for wrapper/handlers
262
280
  await KeycloakManager.configure({
263
281
  baseUrl: 'https://keycloak.example.com',
264
282
  realmName: 'master',
@@ -268,45 +286,46 @@ await KeycloakManager.configure({
268
286
  clientId: 'admin-cli'
269
287
  });
270
288
 
271
- // Later, re-authenticate as different user
272
- await KeycloakManager.auth({
273
- baseUrl: 'https://keycloak.example.com',
274
- realmName: 'master',
275
- username: 'realm-admin',
276
- password: 'realm-admin-password',
277
- grantType: 'password',
278
- clientId: 'admin-cli'
289
+ // Application login/token request for an end-user
290
+ const tokenResponse = await KeycloakManager.auth({
291
+ grant_type: 'password',
292
+ username: 'end-user',
293
+ password: 'user-password',
294
+ scope: 'openid profile email'
279
295
  });
296
+
297
+ console.log(tokenResponse.access_token);
280
298
  ```
281
299
 
282
- #### Refresh Expired Session
300
+ #### Client Credentials Login
283
301
 
284
302
  ```javascript
285
- // If session expired or token invalidated
286
- try {
287
- await KeycloakManager.users.find();
288
- } catch (error) {
289
- if (error.response && error.response.status === 401) {
290
- // Re-authenticate
291
- await KeycloakManager.auth({
292
- baseUrl: 'https://keycloak.example.com',
293
- realmName: 'master',
294
- username: 'admin',
295
- password: 'admin',
296
- grantType: 'password',
297
- clientId: 'admin-cli'
298
- });
299
-
300
- // Retry operation
301
- const users = await KeycloakManager.users.find();
302
- }
303
- }
303
+ const tokenResponse = await KeycloakManager.auth({
304
+ grant_type: 'client_credentials',
305
+ client_id: 'my-public-api',
306
+ client_secret: process.env.API_CLIENT_SECRET
307
+ });
308
+
309
+ console.log(tokenResponse.access_token);
310
+ ```
311
+
312
+ #### Refresh Token Flow
313
+
314
+ ```javascript
315
+ const refreshed = await KeycloakManager.auth({
316
+ grant_type: 'refresh_token',
317
+ refresh_token: oldRefreshToken
318
+ });
319
+
320
+ console.log(refreshed.access_token);
304
321
  ```
305
322
 
306
323
  ### Notes
307
324
 
308
- - `auth()` stops the existing token refresh timer and starts a new one (if `tokenLifeSpan` is configured).
309
- - Use `auth()` instead of `configure()` when you need to change credentials without reinitializing handlers.
325
+ - `auth()` posts to `${baseUrl}/realms/${realmName}/protocol/openid-connect/token`.
326
+ - `auth()` returns raw token endpoint payload and throws on non-2xx responses.
327
+ - `auth()` does not change handler wiring, runtime config, or the internal admin refresh timer.
328
+ - Runtime `clientId`/`clientSecret` are appended automatically if configured and not overridden in request payload.
310
329
 
311
330
  ---
312
331
 
@@ -76,7 +76,7 @@ KeycloakManager.stop();
76
76
  | `configure()` | Authentication and setup | Core |
77
77
  | `setConfig()` | Runtime configuration | Core |
78
78
  | `getToken()` | Get current access token | Core |
79
- | `auth()` | Re-authenticate | Core |
79
+ | `auth()` | OIDC token grant/login endpoint wrapper | Core |
80
80
  | `stop()` | Stop token refresh timer | Core |
81
81
  | `realms` | Realm management | realmsHandler |
82
82
  | `users` | User management | usersHandler |
@@ -15,7 +15,9 @@ This package exposes a single runtime (`index.js`) that initializes one Keycloak
15
15
  - Keeps active session/token management in place.
16
16
 
17
17
  3. `auth(credentials)`
18
- - Direct token endpoint call for explicit auth flows.
18
+ - Direct token endpoint call for explicit OIDC login/token flows.
19
+ - Intended for application-level third-party authentication use-cases.
20
+ - Does not mutate the internal admin client session established by `configure()`.
19
21
 
20
22
  4. `stop()`
21
23
  - Stops refresh timer for clean process termination.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keycloak-api-manager",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "Enhanced Node.js wrapper for Keycloak Admin REST API. Professional alternative to @keycloak/keycloak-admin-client with advanced features, bug fixes, automatic token refresh, Organizations API support, fine-grained permissions, and comprehensive resource management. Battle-tested with 113+ integration tests.",
5
5
  "main": "index.js",
6
6
  "scripts": {