keycloak-api-manager 5.0.2 → 5.0.4

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
@@ -65,9 +65,14 @@ In Keycloak 26.x, management-permissions APIs used by group/user fine-grained te
65
65
  - `configure(credentials)`
66
66
  - `setConfig(overrides)`
67
67
  - `getToken()`
68
+ - `login(credentials)`
68
69
  - `auth(credentials)`
69
70
  - `stop()`
70
71
 
72
+ `login(credentials)` is the preferred OIDC token endpoint helper for login/token grant flows (user/client).
73
+
74
+ `auth(credentials)` is kept as backward-compatible alias and does not replace the internal admin session configured by `configure()`.
75
+
71
76
  Configured handler namespaces:
72
77
 
73
78
  - `realms`
@@ -7,6 +7,7 @@ Core API methods for initializing and managing the Keycloak Admin Client connect
7
7
  - [configure()](#configure)
8
8
  - [setConfig()](#setconfig)
9
9
  - [getToken()](#gettoken)
10
+ - [login()](#login)
10
11
  - [auth()](#auth)
11
12
  - [stop()](#stop)
12
13
 
@@ -180,7 +181,7 @@ KeycloakManager.setConfig({
180
181
 
181
182
  ### Notes
182
183
 
183
- - `setConfig()` does NOT re-authenticate. It only updates the context.
184
+ - `setConfig()` does NOT perform token/login calls. It only updates the runtime context.
184
185
  - Changing `realmName` affects all subsequent API calls until changed again.
185
186
  - The access token remains valid across realm switches (as long as the authenticated user/service account has permissions).
186
187
 
@@ -236,29 +237,64 @@ const response = await axios.get('https://keycloak.example.com/admin/realms/mast
236
237
 
237
238
  ## auth()
238
239
 
239
- Re-authenticate with new or updated credentials. Use this to switch users or refresh authentication manually.
240
+ Backward-compatible alias of `login()`.
241
+
242
+ Use `login()` in new code for clearer intent.
240
243
 
241
244
  **Syntax:**
242
245
  ```javascript
243
246
  await KeycloakManager.auth(credentials)
244
247
  ```
245
248
 
249
+ ### Returns
250
+
251
+ **Promise\<Object\>** - Same response as `login()`
252
+
253
+ ---
254
+
255
+ ## login()
256
+
257
+ Request tokens from Keycloak via the OIDC token endpoint.
258
+
259
+ This method is intended for application-level login/token flows (for users, service clients, or third-party integrations) using this package as a wrapper.
260
+
261
+ It does **not** reconfigure or replace the internal admin session created by `configure()`.
262
+
263
+ **Syntax:**
264
+ ```javascript
265
+ await KeycloakManager.login(credentials)
266
+ ```
267
+
246
268
  ### Parameters
247
269
 
248
270
  #### credentials (Object) ⚠️ Required
249
271
 
250
- Same structure as `configure()` credentials parameter.
272
+ Form parameters sent to `/protocol/openid-connect/token`.
273
+
274
+ Common fields:
275
+
276
+ | Property | Type | Required | Description |
277
+ |----------|------|----------|-------------|
278
+ | `grant_type` | string | ⚠️ Yes | OAuth2 grant type (`password`, `client_credentials`, `refresh_token`, `authorization_code`) |
279
+ | `username` | string | 📋 Conditional | Required for `password` grant |
280
+ | `password` | string | 📋 Conditional | Required for `password` grant |
281
+ | `client_id` | string | 📋 Optional | If omitted, runtime `clientId` from `configure()` is used |
282
+ | `client_secret` | string | 📋 Optional | If omitted, runtime `clientSecret` from `configure()` is used |
283
+ | `refresh_token` | string | 📋 Conditional | Required for `refresh_token` grant |
284
+ | `scope` | string | 📋 Optional | OAuth scopes (e.g. `openid profile email offline_access`) |
285
+ | `code` | string | 📋 Conditional | Required for `authorization_code` grant |
286
+ | `redirect_uri` | string | 📋 Conditional | Required for `authorization_code` grant |
251
287
 
252
288
  ### Returns
253
289
 
254
- **Promise\<void\>** - Resolves when re-authentication succeeds
290
+ **Promise\<Object\>** - Token payload returned by Keycloak (e.g. `access_token`, `refresh_token`, `expires_in`, `token_type`)
255
291
 
256
292
  ### Examples
257
293
 
258
- #### Switch User
294
+ #### Resource Owner Password Login
259
295
 
260
296
  ```javascript
261
- // Initial authentication as admin
297
+ // Admin setup for wrapper/handlers
262
298
  await KeycloakManager.configure({
263
299
  baseUrl: 'https://keycloak.example.com',
264
300
  realmName: 'master',
@@ -268,45 +304,46 @@ await KeycloakManager.configure({
268
304
  clientId: 'admin-cli'
269
305
  });
270
306
 
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'
307
+ // Application login/token request for an end-user
308
+ const tokenResponse = await KeycloakManager.login({
309
+ grant_type: 'password',
310
+ username: 'end-user',
311
+ password: 'user-password',
312
+ scope: 'openid profile email'
279
313
  });
314
+
315
+ console.log(tokenResponse.access_token);
280
316
  ```
281
317
 
282
- #### Refresh Expired Session
318
+ #### Client Credentials Login
283
319
 
284
320
  ```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
- }
321
+ const tokenResponse = await KeycloakManager.login({
322
+ grant_type: 'client_credentials',
323
+ client_id: 'my-public-api',
324
+ client_secret: process.env.API_CLIENT_SECRET
325
+ });
326
+
327
+ console.log(tokenResponse.access_token);
328
+ ```
329
+
330
+ #### Refresh Token Flow
331
+
332
+ ```javascript
333
+ const refreshed = await KeycloakManager.login({
334
+ grant_type: 'refresh_token',
335
+ refresh_token: oldRefreshToken
336
+ });
337
+
338
+ console.log(refreshed.access_token);
304
339
  ```
305
340
 
306
341
  ### Notes
307
342
 
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.
343
+ - `login()` posts to `${baseUrl}/realms/${realmName}/protocol/openid-connect/token`.
344
+ - `login()` returns raw token endpoint payload and throws on non-2xx responses.
345
+ - `login()`/`auth()` do not change handler wiring, runtime config, or the internal admin refresh timer.
346
+ - Runtime `clientId`/`clientSecret` are appended automatically if configured and not overridden in request payload.
310
347
 
311
348
  ---
312
349
 
@@ -76,7 +76,8 @@ 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
+ | `login()` | Preferred OIDC token grant/login endpoint wrapper | Core |
80
+ | `auth()` | Backward-compatible alias of `login()` | Core |
80
81
  | `stop()` | Stop token refresh timer | Core |
81
82
  | `realms` | Realm management | realmsHandler |
82
83
  | `users` | User management | usersHandler |
@@ -14,8 +14,11 @@ This package exposes a single runtime (`index.js`) that initializes one Keycloak
14
14
  - Updates realm/baseUrl/request options at runtime.
15
15
  - Keeps active session/token management in place.
16
16
 
17
- 3. `auth(credentials)`
18
- - Direct token endpoint call for explicit auth flows.
17
+ 3. `login(credentials)`
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()`.
21
+ - `auth(credentials)` remains available as backward-compatible alias.
19
22
 
20
23
  4. `stop()`
21
24
  - Stops refresh timer for clean process termination.
package/index.js CHANGED
@@ -138,7 +138,7 @@ exports.stop = function stop() {
138
138
  clearRefreshTimer();
139
139
  };
140
140
 
141
- exports.auth = async function auth(credentials = {}) {
141
+ async function requestOidcToken(credentials = {}) {
142
142
  assertConfigured();
143
143
 
144
144
  const body = new URLSearchParams();
@@ -175,4 +175,12 @@ exports.auth = async function auth(credentials = {}) {
175
175
  }
176
176
 
177
177
  return payload;
178
+ }
179
+
180
+ exports.auth = async function auth(credentials = {}) {
181
+ return requestOidcToken(credentials);
182
+ };
183
+
184
+ exports.login = async function login(credentials = {}) {
185
+ return requestOidcToken(credentials);
178
186
  };
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.4",
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": {