keycloak-api-manager 5.0.0 → 5.0.2

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.
@@ -0,0 +1,1563 @@
1
+ # Users API
2
+
3
+ Complete user management including CRUD operations, credentials, roles, groups, sessions, and federated identities.
4
+
5
+ **Namespace:** `KeycloakManager.users`
6
+
7
+ ## Table of Contents
8
+
9
+ ### User CRUD
10
+ - [create()](#create) - Create user
11
+ - [find()](#find) - Search users
12
+ - [findOne()](#findone) - Get user by ID
13
+ - [count()](#count) - Count users
14
+ - [update()](#update) - Update user
15
+ - [del()](#del) - Delete user
16
+
17
+ ### Credentials & Password
18
+ - [resetPassword()](#resetpassword) - Reset/set user password
19
+ - [getCredentials()](#getcredentials) - List user credentials
20
+ - [deleteCredential()](#deletecredential) - Delete specific credential
21
+ - [getUserStorageCredentialTypes()](#getuserstoragecredentialtypes) - Get credential types from user storage
22
+ - [updateCredentialLabel()](#updatecredentiallabel) - Update credential label
23
+
24
+ ### Profile & Metadata
25
+ - [getProfile()](#getprofile) - Get user profile schema
26
+
27
+ ### Group Membership
28
+ - [addToGroup()](#addtogroup) - Add user to group
29
+ - [delFromGroup()](#delfromgroup) - Remove user from group
30
+ - [listGroups()](#listgroups) - List user's groups
31
+ - [countGroups()](#countgroups) - Count user's groups
32
+
33
+ ### Realm Role Mappings
34
+ - [addRealmRoleMappings()](#addrealmrolemappings) - Assign realm roles
35
+ - [delRealmRoleMappings()](#delrealmrolemappings) - Remove realm roles
36
+ - [listRealmRoleMappings()](#listrealmrolemappings) - List assigned realm roles
37
+ - [listAvailableRealmRoleMappings()](#listavailablerealmrolemappings) - List assignable realm roles
38
+ - [listCompositeRealmRoleMappings()](#listcompositerealmrolemappings) - List effective realm roles
39
+ - [listRoleMappings()](#listrolemappings) - List all role mappings (realm + client)
40
+
41
+ ### Client Role Mappings
42
+ - [addClientRoleMappings()](#addclientrolemappings) - Assign client roles
43
+ - [delClientRoleMappings()](#delclientrolemappings) - Remove client roles
44
+ - [listClientRoleMappings()](#listclientrolemappings) - List assigned client roles
45
+ - [listAvailableClientRoleMappings()](#listavailableclientrolemappings) - List assignable client roles
46
+ - [listCompositeClientRoleMappings()](#listcompositeclientrolemappings) - List effective client roles
47
+
48
+ ### Sessions & Logout
49
+ - [listSessions()](#listsessions) - List active user sessions
50
+ - [listOfflineSessions()](#listofflinesessions) - List offline sessions
51
+ - [logout()](#logout) - Logout user (all sessions)
52
+
53
+ ### Consents
54
+ - [listConsents()](#listconsents) - List user consents
55
+ - [revokeConsent()](#revokeconsent) - Revoke specific consent
56
+
57
+ ### Impersonation
58
+ - [impersonation()](#impersonation) - Impersonate user
59
+
60
+ ### Federated Identities
61
+ - [listFederatedIdentities()](#listfederatedidentities) - List linked identities
62
+ - [addToFederatedIdentity()](#addtofederatedidentity) - Link federated identity
63
+ - [delFromFederatedIdentity()](#delfromfederatedidentity) - Unlink federated identity
64
+
65
+ ---
66
+
67
+ ## create()
68
+
69
+ Create a new user in the current realm.
70
+
71
+ **Syntax:**
72
+ ```javascript
73
+ const result = await KeycloakManager.users.create(userRepresentation)
74
+ ```
75
+
76
+ ### Parameters
77
+
78
+ #### userRepresentation (Object) ⚠️ Required
79
+
80
+ | Property | Type | Required | Description |
81
+ |----------|------|----------|-------------|
82
+ | `username` | string | ⚠️ Yes | Unique username |
83
+ | `email` | string | 📋 Optional | User email address |
84
+ | `firstName` | string | 📋 Optional | First name |
85
+ | `lastName` | string | 📋 Optional | Last name |
86
+ | `enabled` | boolean | 📋 Optional | Enable/disable account (default: true) |
87
+ | `emailVerified` | boolean | 📋 Optional | Mark email as verified (default: false) |
88
+ | `credentials` | array | 📋 Optional | Initial credentials (password, etc.) |
89
+ | `requiredActions` | array | 📋 Optional | Required actions (`['VERIFY_EMAIL', 'UPDATE_PASSWORD']`) |
90
+ | `attributes` | object | 📋 Optional | Custom user attributes |
91
+ | `groups` | array | 📋 Optional | Group IDs or paths |
92
+ | `realmRoles` | array | 📋 Optional | Realm role names |
93
+ | `clientRoles` | object | 📋 Optional | Client roles map `{ clientId: ['role1', 'role2'] }` |
94
+
95
+ ### Returns
96
+
97
+ **Promise\<Object\>** - Created user representation with `id`
98
+
99
+ ### Examples
100
+
101
+ #### Basic User
102
+
103
+ ```javascript
104
+ const user = await KeycloakManager.users.create({
105
+ username: 'john.doe',
106
+ email: 'john.doe@example.com',
107
+ firstName: 'John',
108
+ lastName: 'Doe',
109
+ enabled: true
110
+ });
111
+
112
+ console.log('Created user:', user.id);
113
+ ```
114
+
115
+ #### With Password and Required Actions
116
+
117
+ ```javascript
118
+ const user = await KeycloakManager.users.create({
119
+ username: 'jane.smith',
120
+ email: 'jane@example.com',
121
+ firstName: 'Jane',
122
+ lastName: 'Smith',
123
+ enabled: true,
124
+ emailVerified: false,
125
+ credentials: [{
126
+ type: 'password',
127
+ value: 'temporaryPassword123',
128
+ temporary: true // Force password change on first login
129
+ }],
130
+ requiredActions: ['VERIFY_EMAIL', 'UPDATE_PASSWORD']
131
+ });
132
+
133
+ console.log('User created with temporary password');
134
+ ```
135
+
136
+ #### With Attributes
137
+
138
+ ```javascript
139
+ const user = await KeycloakManager.users.create({
140
+ username: 'employee123',
141
+ email: 'employee@company.com',
142
+ firstName: 'Employee',
143
+ lastName: 'Name',
144
+ enabled: true,
145
+ attributes: {
146
+ department: ['Engineering'],
147
+ employeeId: ['EMP-12345'],
148
+ location: ['San Francisco']
149
+ }
150
+ });
151
+ ```
152
+
153
+ ---
154
+
155
+ ## find()
156
+
157
+ Search for users with optional filtering and pagination. Supports searching by built-in attributes and custom attributes (Keycloak 15+).
158
+
159
+ **Syntax:**
160
+ ```javascript
161
+ const users = await KeycloakManager.users.find(filter)
162
+ ```
163
+
164
+ ### Parameters
165
+
166
+ #### filter (Object) 📋 Optional
167
+
168
+ | Property | Type | Required | Description |
169
+ |----------|------|----------|-------------|
170
+ | `username` | string | 📋 Optional | Filter by username (partial match) |
171
+ | `email` | string | 📋 Optional | Filter by email (partial match) |
172
+ | `firstName` | string | 📋 Optional | Filter by first name |
173
+ | `lastName` | string | 📋 Optional | Filter by last name |
174
+ | `search` | string | 📋 Optional | Search across username/email/name |
175
+ | `exact` | boolean | 📋 Optional | Exact match instead of partial (default: false) |
176
+ | `enabled` | boolean | 📋 Optional | Filter by enabled status |
177
+ | `q` | string | 📋 Optional | Query by custom attributes (Keycloak 15+): `'attrName:value'` |
178
+ | `max` | number | 📋 Optional | Maximum results (pagination) |
179
+ | `first` | number | 📋 Optional | First result index (pagination) |
180
+
181
+ ### Returns
182
+
183
+ **Promise\<Array\>** - Array of user representations
184
+
185
+ ### Examples
186
+
187
+ #### List All Users (Limited)
188
+
189
+ ```javascript
190
+ const users = await KeycloakManager.users.find({ max: 100 });
191
+ console.log(`Found ${users.length} users`);
192
+
193
+ users.forEach(user => {
194
+ console.log(`- ${user.username} (${user.email})`);
195
+ });
196
+ ```
197
+
198
+ #### Search by Email
199
+
200
+ ```javascript
201
+ const users = await KeycloakManager.users.find({
202
+ email: 'example.com',
203
+ max: 50
204
+ });
205
+
206
+ console.log(`Users with @example.com email: ${users.length}`);
207
+ ```
208
+
209
+ #### Exact Username Match
210
+
211
+ ```javascript
212
+ const users = await KeycloakManager.users.find({
213
+ username: 'john.doe',
214
+ exact: true
215
+ });
216
+
217
+ if (users.length > 0) {
218
+ console.log('User found:', users[0].id);
219
+ }
220
+ ```
221
+
222
+ #### Search by Custom Attribute (Keycloak 15+)
223
+
224
+ ```javascript
225
+ const users = await KeycloakManager.users.find({
226
+ q: 'department:Engineering',
227
+ max: 100
228
+ });
229
+
230
+ console.log(`Engineering department users: ${users.length}`);
231
+ ```
232
+
233
+ #### Pagination
234
+
235
+ ```javascript
236
+ // Get first page (0-99)
237
+ const page1 = await KeycloakManager.users.find({ first: 0, max: 100 });
238
+
239
+ // Get second page (100-199)
240
+ const page2 = await KeycloakManager.users.find({ first: 100, max: 100 });
241
+
242
+ console.log(`Page 1: ${page1.length} users`);
243
+ console.log(`Page 2: ${page2.length} users`);
244
+ ```
245
+
246
+ ---
247
+
248
+ ## findOne()
249
+
250
+ Get a specific user by their unique ID.
251
+
252
+ **Syntax:**
253
+ ```javascript
254
+ const user = await KeycloakManager.users.findOne(filter)
255
+ ```
256
+
257
+ ### Parameters
258
+
259
+ #### filter (Object) ⚠️ Required
260
+
261
+ | Property | Type | Required | Description |
262
+ |----------|------|----------|-------------|
263
+ | `id` | string | ⚠️ Yes | User UUID |
264
+
265
+ ### Returns
266
+
267
+ **Promise\<Object\>** - User representation
268
+
269
+ ### Examples
270
+
271
+ ```javascript
272
+ const user = await KeycloakManager.users.findOne({
273
+ id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
274
+ });
275
+
276
+ console.log('Username:', user.username);
277
+ console.log('Email:', user.email);
278
+ console.log('Enabled:', user.enabled);
279
+ console.log('Attributes:', user.attributes);
280
+ ```
281
+
282
+ ---
283
+
284
+ ## count()
285
+
286
+ Count users matching specified criteria.
287
+
288
+ **Syntax:**
289
+ ```javascript
290
+ const total = await KeycloakManager.users.count(filter)
291
+ ```
292
+
293
+ ### Parameters
294
+
295
+ #### filter (Object) 📋 Optional
296
+
297
+ Same as `find()` filter parameters.
298
+
299
+ ### Returns
300
+
301
+ **Promise\<number\>** - Total count
302
+
303
+ ### Examples
304
+
305
+ ```javascript
306
+ // Total users in realm
307
+ const total = await KeycloakManager.users.count();
308
+ console.log(`Total users: ${total}`);
309
+
310
+ // Count enabled users
311
+ const enabled = await KeycloakManager.users.count({ enabled: true });
312
+ console.log(`Enabled users: ${enabled}`);
313
+
314
+ // Count by email domain
315
+ const companyUsers = await KeycloakManager.users.count({
316
+ email: '@company.com'
317
+ });
318
+ console.log(`Company users: ${companyUsers}`);
319
+ ```
320
+
321
+ ---
322
+
323
+ ## update()
324
+
325
+ Update user details.
326
+
327
+ **Syntax:**
328
+ ```javascript
329
+ await KeycloakManager.users.update(searchParams, userRepresentation)
330
+ ```
331
+
332
+ ### Parameters
333
+
334
+ #### searchParams (Object) ⚠️ Required
335
+
336
+ | Property | Type | Required | Description |
337
+ |----------|------|----------|-------------|
338
+ | `id` | string | ⚠️ Yes | User UUID |
339
+
340
+ #### userRepresentation (Object) ⚠️ Required
341
+
342
+ Object with fields to update (same structure as `create()`).
343
+
344
+ ### Returns
345
+
346
+ **Promise\<void\>**
347
+
348
+ ### Examples
349
+
350
+ #### Update Basic Fields
351
+
352
+ ```javascript
353
+ await KeycloakManager.users.update(
354
+ { id: userId },
355
+ {
356
+ firstName: 'John',
357
+ lastName: 'Smith',
358
+ email: 'john.smith@newdomain.com'
359
+ }
360
+ );
361
+
362
+ console.log('User updated');
363
+ ```
364
+
365
+ #### Enable/Disable User
366
+
367
+ ```javascript
368
+ await KeycloakManager.users.update(
369
+ { id: userId },
370
+ { enabled: false }
371
+ );
372
+
373
+ console.log('User disabled');
374
+ ```
375
+
376
+ #### Update Attributes
377
+
378
+ ```javascript
379
+ await KeycloakManager.users.update(
380
+ { id: userId },
381
+ {
382
+ attributes: {
383
+ department: ['Sales'],
384
+ status: ['Active']
385
+ }
386
+ }
387
+ );
388
+ ```
389
+
390
+ ---
391
+
392
+ ## del()
393
+
394
+ Delete a user permanently.
395
+
396
+ **Syntax:**
397
+ ```javascript
398
+ await KeycloakManager.users.del(filter)
399
+ ```
400
+
401
+ ### Parameters
402
+
403
+ #### filter (Object) ⚠️ Required
404
+
405
+ | Property | Type | Required | Description |
406
+ |----------|------|----------|-------------|
407
+ | `id` | string | ⚠️ Yes | User UUID |
408
+
409
+ ### Returns
410
+
411
+ **Promise\<void\>**
412
+
413
+ ### Examples
414
+
415
+ ```javascript
416
+ await KeycloakManager.users.del({
417
+ id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
418
+ });
419
+
420
+ console.log('User deleted');
421
+ ```
422
+
423
+ ---
424
+
425
+ ## resetPassword()
426
+
427
+ Set or reset a user's password.
428
+
429
+ **Syntax:**
430
+ ```javascript
431
+ await KeycloakManager.users.resetPassword(newCredentialsParameters)
432
+ ```
433
+
434
+ ### Parameters
435
+
436
+ #### newCredentialsParameters (Object) ⚠️ Required
437
+
438
+ | Property | Type | Required | Description |
439
+ |----------|------|----------|-------------|
440
+ | `id` | string | ⚠️ Yes | User UUID |
441
+ | `credential` | object | ⚠️ Yes | Credential object |
442
+ | `credential.type` | string | ⚠️ Yes | Credential type (`'password'`) |
443
+ | `credential.value` | string | ⚠️ Yes | New password value |
444
+ | `credential.temporary` | boolean | 📋 Optional | Force change on next login (default: false) |
445
+
446
+ ### Returns
447
+
448
+ **Promise\<void\>**
449
+
450
+ ### Examples
451
+
452
+ #### Set Permanent Password
453
+
454
+ ```javascript
455
+ await KeycloakManager.users.resetPassword({
456
+ id: userId,
457
+ credential: {
458
+ type: 'password',
459
+ value: 'newSecurePassword123!',
460
+ temporary: false
461
+ }
462
+ });
463
+
464
+ console.log('Password updated');
465
+ ```
466
+
467
+ #### Set Temporary Password
468
+
469
+ ```javascript
470
+ await KeycloakManager.users.resetPassword({
471
+ id: userId,
472
+ credential: {
473
+ type: 'password',
474
+ value: 'tempPassword123',
475
+ temporary: true // User must change on next login
476
+ }
477
+ });
478
+
479
+ console.log('Temporary password set');
480
+ ```
481
+
482
+ ---
483
+
484
+ ## getCredentials()
485
+
486
+ List all credentials for a user (passwords, OTP, etc.).
487
+
488
+ **Syntax:**
489
+ ```javascript
490
+ const credentials = await KeycloakManager.users.getCredentials(filter)
491
+ ```
492
+
493
+ ### Parameters
494
+
495
+ #### filter (Object) ⚠️ Required
496
+
497
+ | Property | Type | Required | Description |
498
+ |----------|------|----------|-------------|
499
+ | `id` | string | ⚠️ Yes | User UUID |
500
+
501
+ ### Returns
502
+
503
+ **Promise\<Array\>** - Array of credential representations
504
+
505
+ ### Examples
506
+
507
+ ```javascript
508
+ const credentials = await KeycloakManager.users.getCredentials({
509
+ id: userId
510
+ });
511
+
512
+ console.log(`User has ${credentials.length} credentials`);
513
+ credentials.forEach(cred => {
514
+ console.log(`- ${cred.type}: ${cred.userLabel || 'unnamed'}`);
515
+ });
516
+ ```
517
+
518
+ ---
519
+
520
+ ## deleteCredential()
521
+
522
+ Delete a specific credential.
523
+
524
+ **Syntax:**
525
+ ```javascript
526
+ await KeycloakManager.users.deleteCredential(accountInfo)
527
+ ```
528
+
529
+ ### Parameters
530
+
531
+ #### accountInfo (Object) ⚠️ Required
532
+
533
+ | Property | Type | Required | Description |
534
+ |----------|------|----------|-------------|
535
+ | `id` | string | ⚠️ Yes | User UUID |
536
+ | `credentialId` | string | ⚠️ Yes | Credential ID |
537
+
538
+ ### Returns
539
+
540
+ **Promise\<void\>**
541
+
542
+ ### Examples
543
+
544
+ ```javascript
545
+ // Get credentials first
546
+ const credentials = await KeycloakManager.users.getCredentials({ id: userId });
547
+
548
+ // Delete specific OTP credential
549
+ const otpCred = credentials.find(c => c.type === 'otp');
550
+ if (otpCred) {
551
+ await KeycloakManager.users.deleteCredential({
552
+ id: userId,
553
+ credentialId: otpCred.id
554
+ });
555
+ console.log('OTP credential deleted');
556
+ }
557
+ ```
558
+
559
+ ---
560
+
561
+ ## getUserStorageCredentialTypes()
562
+
563
+ Get available credential types from user storage provider.
564
+
565
+ **Syntax:**
566
+ ```javascript
567
+ const types = await KeycloakManager.users.getUserStorageCredentialTypes(filter)
568
+ ```
569
+
570
+ ### Parameters
571
+
572
+ #### filter (Object) ⚠️ Required
573
+
574
+ | Property | Type | Required | Description |
575
+ |----------|------|----------|-------------|
576
+ | `id` | string | ⚠️ Yes | User UUID |
577
+
578
+ ### Returns
579
+
580
+ **Promise\<Array\>** - Array of credential type strings
581
+
582
+ ### Examples
583
+
584
+ ```javascript
585
+ const types = await KeycloakManager.users.getUserStorageCredentialTypes({
586
+ id: userId
587
+ });
588
+
589
+ console.log('Available credential types:', types);
590
+ // Example output: ['password', 'otp']
591
+ ```
592
+
593
+ ---
594
+
595
+ ## updateCredentialLabel()
596
+
597
+ Update the label of a credential.
598
+
599
+ **Syntax:**
600
+ ```javascript
601
+ await KeycloakManager.users.updateCredentialLabel(filter, label)
602
+ ```
603
+
604
+ ### Parameters
605
+
606
+ #### filter (Object) ⚠️ Required
607
+
608
+ | Property | Type | Required | Description |
609
+ |----------|------|----------|-------------|
610
+ | `id` | string | ⚠️ Yes | User UUID |
611
+ | `credentialId` | string | ⚠️ Yes | Credential ID |
612
+
613
+ #### label (string) ⚠️ Required
614
+
615
+ New label for the credential.
616
+
617
+ ### Returns
618
+
619
+ **Promise\<void\>**
620
+
621
+ ### Examples
622
+
623
+ ```javascript
624
+ const credentials = await KeycloakManager.users.getCredentials({ id: userId });
625
+ const passwordCred = credentials.find(c => c.type === 'password');
626
+
627
+ await KeycloakManager.users.updateCredentialLabel(
628
+ { id: userId, credentialId: passwordCred.id },
629
+ 'My Primary Password'
630
+ );
631
+
632
+ console.log('Credential label updated');
633
+ ```
634
+
635
+ ---
636
+
637
+ ## getProfile()
638
+
639
+ Get the user profile schema for the realm.
640
+
641
+ **Syntax:**
642
+ ```javascript
643
+ const profile = await KeycloakManager.users.getProfile()
644
+ ```
645
+
646
+ ### Parameters
647
+
648
+ None
649
+
650
+ ### Returns
651
+
652
+ **Promise\<Object\>** - User profile configuration
653
+
654
+ ### Examples
655
+
656
+ ```javascript
657
+ const profile = await KeycloakManager.users.getProfile();
658
+
659
+ console.log('User profile attributes:');
660
+ profile.attributes.forEach(attr => {
661
+ console.log(`- ${attr.name} (${attr.validators ? 'validated' : 'free-form'})`);
662
+ });
663
+ ```
664
+
665
+ ---
666
+
667
+ ## addToGroup()
668
+
669
+ Add user to a group.
670
+
671
+ **Syntax:**
672
+ ```javascript
673
+ await KeycloakManager.users.addToGroup(parameters)
674
+ ```
675
+
676
+ ### Parameters
677
+
678
+ #### parameters (Object) ⚠️ Required
679
+
680
+ | Property | Type | Required | Description |
681
+ |----------|------|----------|-------------|
682
+ | `id` | string | ⚠️ Yes | User UUID |
683
+ | `groupId` | string | ⚠️ Yes | Group UUID |
684
+
685
+ ### Returns
686
+
687
+ **Promise\<void\>**
688
+
689
+ ### Examples
690
+
691
+ ```javascript
692
+ // Find group
693
+ const groups = await KeycloakManager.groups.find({ search: 'Administrators' });
694
+ const groupId = groups[0].id;
695
+
696
+ // Add user to group
697
+ await KeycloakManager.users.addToGroup({
698
+ id: userId,
699
+ groupId: groupId
700
+ });
701
+
702
+ console.log('User added to group');
703
+ ```
704
+
705
+ ---
706
+
707
+ ## delFromGroup()
708
+
709
+ Remove user from a group.
710
+
711
+ **Syntax:**
712
+ ```javascript
713
+ await KeycloakManager.users.delFromGroup(parameters)
714
+ ```
715
+
716
+ ### Parameters
717
+
718
+ #### parameters (Object) ⚠️ Required
719
+
720
+ | Property | Type | Required | Description |
721
+ |----------|------|----------|-------------|
722
+ | `id` | string | ⚠️ Yes | User UUID |
723
+ | `groupId` | string | ⚠️ Yes | Group UUID |
724
+
725
+ ### Returns
726
+
727
+ **Promise\<void\>**
728
+
729
+ ### Examples
730
+
731
+ ```javascript
732
+ await KeycloakManager.users.delFromGroup({
733
+ id: userId,
734
+ groupId: groupId
735
+ });
736
+
737
+ console.log('User removed from group');
738
+ ```
739
+
740
+ ---
741
+
742
+ ## listGroups()
743
+
744
+ List all groups a user belongs to.
745
+
746
+ **Syntax:**
747
+ ```javascript
748
+ const groups = await KeycloakManager.users.listGroups(filter)
749
+ ```
750
+
751
+ ### Parameters
752
+
753
+ #### filter (Object) ⚠️ Required
754
+
755
+ | Property | Type | Required | Description |
756
+ |----------|------|----------|-------------|
757
+ | `id` | string | ⚠️ Yes | User UUID |
758
+ | `briefRepresentation` | boolean | 📋 Optional | Return minimal data (default: true) |
759
+ | `first` | number | 📋 Optional | Pagination first index |
760
+ | `max` | number | 📋 Optional | Maximum results |
761
+
762
+ ### Returns
763
+
764
+ **Promise\<Array\>** - Array of group representations
765
+
766
+ ### Examples
767
+
768
+ ```javascript
769
+ const groups = await KeycloakManager.users.listGroups({
770
+ id: userId,
771
+ max: 100
772
+ });
773
+
774
+ console.log(`User belongs to ${groups.length} groups:`);
775
+ groups.forEach(group => {
776
+ console.log(`- ${group.name} (${group.path})`);
777
+ });
778
+ ```
779
+
780
+ ---
781
+
782
+ ## countGroups()
783
+
784
+ Count groups a user belongs to.
785
+
786
+ **Syntax:**
787
+ ```javascript
788
+ const count = await KeycloakManager.users.countGroups(filter)
789
+ ```
790
+
791
+ ### Parameters
792
+
793
+ #### filter (Object) ⚠️ Required
794
+
795
+ | Property | Type | Required | Description |
796
+ |----------|------|----------|-------------|
797
+ | `id` | string | ⚠️ Yes | User UUID |
798
+ | `search` | string | 📋 Optional | Filter by group name |
799
+
800
+ ### Returns
801
+
802
+ **Promise\<number\>** - Count
803
+
804
+ ### Examples
805
+
806
+ ```javascript
807
+ const total = await KeycloakManager.users.countGroups({ id: userId });
808
+ console.log(`User is in ${total} groups`);
809
+ ```
810
+
811
+ ---
812
+
813
+ ## addRealmRoleMappings()
814
+
815
+ Assign realm roles to a user.
816
+
817
+ **Syntax:**
818
+ ```javascript
819
+ await KeycloakManager.users.addRealmRoleMappings(roleMapping)
820
+ ```
821
+
822
+ ### Parameters
823
+
824
+ #### roleMapping (Object) ⚠️ Required
825
+
826
+ | Property | Type | Required | Description |
827
+ |----------|------|----------|-------------|
828
+ | `id` | string | ⚠️ Yes | User UUID |
829
+ | `roles` | array | ⚠️ Yes | Array of role objects with `id` and `name` |
830
+
831
+ ### Returns
832
+
833
+ **Promise\<void\>**
834
+
835
+ ### Examples
836
+
837
+ ```javascript
838
+ // Find roles
839
+ const roles = await KeycloakManager.roles.find();
840
+ const adminRole = roles.find(r => r.name === 'admin');
841
+ const userRole = roles.find(r => r.name === 'user');
842
+
843
+ // Assign roles
844
+ await KeycloakManager.users.addRealmRoleMappings({
845
+ id: userId,
846
+ roles: [
847
+ { id: adminRole.id, name: adminRole.name },
848
+ { id: userRole.id, name: userRole.name }
849
+ ]
850
+ });
851
+
852
+ console.log('Realm roles assigned');
853
+ ```
854
+
855
+ ---
856
+
857
+ ## delRealmRoleMappings()
858
+
859
+ Remove realm roles from a user.
860
+
861
+ **Syntax:**
862
+ ```javascript
863
+ await KeycloakManager.users.delRealmRoleMappings(roleMapping)
864
+ ```
865
+
866
+ ### Parameters
867
+
868
+ Same as `addRealmRoleMappings()`.
869
+
870
+ ### Returns
871
+
872
+ **Promise\<void\>**
873
+
874
+ ### Examples
875
+
876
+ ```javascript
877
+ const roles = await KeycloakManager.roles.find();
878
+ const adminRole = roles.find(r => r.name === 'admin');
879
+
880
+ await KeycloakManager.users.delRealmRoleMappings({
881
+ id: userId,
882
+ roles: [{ id: adminRole.id, name: adminRole.name }]
883
+ });
884
+
885
+ console.log('Admin role removed');
886
+ ```
887
+
888
+ ---
889
+
890
+ ## listRealmRoleMappings()
891
+
892
+ List realm roles directly assigned to a user (not including composite roles).
893
+
894
+ **Syntax:**
895
+ ```javascript
896
+ const roles = await KeycloakManager.users.listRealmRoleMappings(filter)
897
+ ```
898
+
899
+ ### Parameters
900
+
901
+ #### filter (Object) ⚠️ Required
902
+
903
+ | Property | Type | Required | Description |
904
+ |----------|------|----------|-------------|
905
+ | `id` | string | ⚠️ Yes | User UUID |
906
+
907
+ ### Returns
908
+
909
+ **Promise\<Array\>** - Array of role representations
910
+
911
+ ### Examples
912
+
913
+ ```javascript
914
+ const roles = await KeycloakManager.users.listRealmRoleMappings({
915
+ id: userId
916
+ });
917
+
918
+ console.log(`User has ${roles.length} assigned realm roles:`);
919
+ roles.forEach(role => {
920
+ console.log(`- ${role.name}`);
921
+ });
922
+ ```
923
+
924
+ ---
925
+
926
+ ## listAvailableRealmRoleMappings()
927
+
928
+ List realm roles available for assignment to a user.
929
+
930
+ **Syntax:**
931
+ ```javascript
932
+ const roles = await KeycloakManager.users.listAvailableRealmRoleMappings(filter)
933
+ ```
934
+
935
+ ### Parameters
936
+
937
+ #### filter (Object) ⚠️ Required
938
+
939
+ | Property | Type | Required | Description |
940
+ |----------|------|----------|-------------|
941
+ | `id` | string | ⚠️ Yes | User UUID |
942
+
943
+ ### Returns
944
+
945
+ **Promise\<Array\>** - Array of assignable role representations
946
+
947
+ ### Examples
948
+
949
+ ```javascript
950
+ const available = await KeycloakManager.users.listAvailableRealmRoleMappings({
951
+ id: userId
952
+ });
953
+
954
+ console.log('Roles available for assignment:');
955
+ available.forEach(role => {
956
+ console.log(`- ${role.name}`);
957
+ });
958
+ ```
959
+
960
+ ---
961
+
962
+ ## listCompositeRealmRoleMappings()
963
+
964
+ List effective realm roles (including roles from composite roles).
965
+
966
+ **Syntax:**
967
+ ```javascript
968
+ const roles = await KeycloakManager.users.listCompositeRealmRoleMappings(filter)
969
+ ```
970
+
971
+ ### Parameters
972
+
973
+ #### filter (Object) ⚠️ Required
974
+
975
+ | Property | Type | Required | Description |
976
+ |----------|------|----------|-------------|
977
+ | `id` | string | ⚠️ Yes | User UUID |
978
+
979
+ ### Returns
980
+
981
+ **Promise\<Array\>** - Array of effective role representations
982
+
983
+ ### Examples
984
+
985
+ ```javascript
986
+ const effectiveRoles = await KeycloakManager.users.listCompositeRealmRoleMappings({
987
+ id: userId
988
+ });
989
+
990
+ console.log(`User has ${effectiveRoles.length} effective realm roles (including composite)`);
991
+ ```
992
+
993
+ ---
994
+
995
+ ## listRoleMappings()
996
+
997
+ List all role mappings (both realm and client roles).
998
+
999
+ **Syntax:**
1000
+ ```javascript
1001
+ const mappings = await KeycloakManager.users.listRoleMappings(filter)
1002
+ ```
1003
+
1004
+ ### Parameters
1005
+
1006
+ #### filter (Object) ⚠️ Required
1007
+
1008
+ | Property | Type | Required | Description |
1009
+ |----------|------|----------|-------------|
1010
+ | `id` | string | ⚠️ Yes | User UUID |
1011
+
1012
+ ### Returns
1013
+
1014
+ **Promise\<Object\>** - Object with `realmMappings` and `clientMappings`
1015
+
1016
+ ### Examples
1017
+
1018
+ ```javascript
1019
+ const mappings = await KeycloakManager.users.listRoleMappings({
1020
+ id: userId
1021
+ });
1022
+
1023
+ console.log('Realm roles:', mappings.realmMappings?.map(r => r.name));
1024
+ console.log('Client mappings:', Object.keys(mappings.clientMappings || {}));
1025
+ ```
1026
+
1027
+ ---
1028
+
1029
+ ## addClientRoleMappings()
1030
+
1031
+ Assign client roles to a user.
1032
+
1033
+ **Syntax:**
1034
+ ```javascript
1035
+ await KeycloakManager.users.addClientRoleMappings(filter)
1036
+ ```
1037
+
1038
+ ### Parameters
1039
+
1040
+ #### filter (Object) ⚠️ Required
1041
+
1042
+ | Property | Type | Required | Description |
1043
+ |----------|------|----------|-------------|
1044
+ | `id` | string | ⚠️ Yes | User UUID |
1045
+ | `clientUniqueId` | string | ⚠️ Yes | Client UUID (not clientId) |
1046
+ | `roles` | array | ⚠️ Yes | Array of role objects |
1047
+
1048
+ ### Returns
1049
+
1050
+ **Promise\<void\>**
1051
+
1052
+ ### Examples
1053
+
1054
+ ```javascript
1055
+ // Find client
1056
+ const clients = await KeycloakManager.clients.find({ clientId: 'my-app' });
1057
+ const clientUniqueId = clients[0].id;
1058
+
1059
+ // Get client roles
1060
+ const clientRoles = await KeycloakManager.clients.listRoles({
1061
+ id: clientUniqueId
1062
+ });
1063
+
1064
+ const adminRole = clientRoles.find(r => r.name === 'app-admin');
1065
+
1066
+ // Assign client role
1067
+ await KeycloakManager.users.addClientRoleMappings({
1068
+ id: userId,
1069
+ clientUniqueId: clientUniqueId,
1070
+ roles: [{ id: adminRole.id, name: adminRole.name }]
1071
+ });
1072
+
1073
+ console.log('Client role assigned');
1074
+ ```
1075
+
1076
+ ---
1077
+
1078
+ ## delClientRoleMappings()
1079
+
1080
+ Remove client roles from a user.
1081
+
1082
+ **Syntax:**
1083
+ ```javascript
1084
+ await KeycloakManager.users.delClientRoleMappings(filter)
1085
+ ```
1086
+
1087
+ ### Parameters
1088
+
1089
+ Same as `addClientRoleMappings()`.
1090
+
1091
+ ### Returns
1092
+
1093
+ **Promise\<void\>**
1094
+
1095
+ ---
1096
+
1097
+ ## listClientRoleMappings()
1098
+
1099
+ List client roles directly assigned to a user.
1100
+
1101
+ **Syntax:**
1102
+ ```javascript
1103
+ const roles = await KeycloakManager.users.listClientRoleMappings(filter)
1104
+ ```
1105
+
1106
+ ### Parameters
1107
+
1108
+ #### filter (Object) ⚠️ Required
1109
+
1110
+ | Property | Type | Required | Description |
1111
+ |----------|------|----------|-------------|
1112
+ | `id` | string | ⚠️ Yes | User UUID |
1113
+ | `clientUniqueId` | string | ⚠️ Yes | Client UUID |
1114
+
1115
+ ### Returns
1116
+
1117
+ **Promise\<Array\>** - Array of role representations
1118
+
1119
+ ---
1120
+
1121
+ ## listAvailableClientRoleMappings()
1122
+
1123
+ List client roles available for assignment.
1124
+
1125
+ **Syntax:**
1126
+ ```javascript
1127
+ const roles = await KeycloakManager.users.listAvailableClientRoleMappings(filter)
1128
+ ```
1129
+
1130
+ ### Parameters
1131
+
1132
+ Same as `listClientRoleMappings()`.
1133
+
1134
+ ### Returns
1135
+
1136
+ **Promise\<Array\>** - Array of assignable role representations
1137
+
1138
+ ---
1139
+
1140
+ ## listCompositeClientRoleMappings()
1141
+
1142
+ List effective client roles (including composite).
1143
+
1144
+ **Syntax:**
1145
+ ```javascript
1146
+ const roles = await KeycloakManager.users.listCompositeClientRoleMappings(filter)
1147
+ ```
1148
+
1149
+ ### Parameters
1150
+
1151
+ Same as `listClientRoleMappings()`.
1152
+
1153
+ ### Returns
1154
+
1155
+ **Promise\<Array\>** - Array of effective role representations
1156
+
1157
+ ---
1158
+
1159
+ ## listSessions()
1160
+
1161
+ List active user sessions.
1162
+
1163
+ **Syntax:**
1164
+ ```javascript
1165
+ const sessions = await KeycloakManager.users.listSessions(filter)
1166
+ ```
1167
+
1168
+ ### Parameters
1169
+
1170
+ #### filter (Object) ⚠️ Required
1171
+
1172
+ | Property | Type | Required | Description |
1173
+ |----------|------|----------|-------------|
1174
+ | `id` | string | ⚠️ Yes | User UUID |
1175
+
1176
+ ### Returns
1177
+
1178
+ **Promise\<Array\>** - Array of session representations
1179
+
1180
+ ### Examples
1181
+
1182
+ ```javascript
1183
+ const sessions = await KeycloakManager.users.listSessions({
1184
+ id: userId
1185
+ });
1186
+
1187
+ console.log(`User has ${sessions.length} active sessions`);
1188
+ sessions.forEach(session => {
1189
+ console.log(`- Session ID: ${session.id}`);
1190
+ console.log(` IP: ${session.ipAddress}`);
1191
+ console.log(` Started: ${new Date(session.start)}`);
1192
+ });
1193
+ ```
1194
+
1195
+ ---
1196
+
1197
+ ## listOfflineSessions()
1198
+
1199
+ List offline sessions for a client.
1200
+
1201
+ **Syntax:**
1202
+ ```javascript
1203
+ const sessions = await KeycloakManager.users.listOfflineSessions(filter)
1204
+ ```
1205
+
1206
+ ### Parameters
1207
+
1208
+ #### filter (Object) ⚠️ Required
1209
+
1210
+ | Property | Type | Required | Description |
1211
+ |----------|------|----------|-------------|
1212
+ | `id` | string | ⚠️ Yes | User UUID |
1213
+ | `clientUniqueId` | string | ⚠️ Yes | Client UUID |
1214
+
1215
+ ### Returns
1216
+
1217
+ **Promise\<Array\>** - Array of offline session representations
1218
+
1219
+ ---
1220
+
1221
+ ## logout()
1222
+
1223
+ Logout user from all sessions.
1224
+
1225
+ **Syntax:**
1226
+ ```javascript
1227
+ await KeycloakManager.users.logout(filter)
1228
+ ```
1229
+
1230
+ ### Parameters
1231
+
1232
+ #### filter (Object) ⚠️ Required
1233
+
1234
+ | Property | Type | Required | Description |
1235
+ |----------|------|----------|-------------|
1236
+ | `id` | string | ⚠️ Yes | User UUID |
1237
+
1238
+ ### Returns
1239
+
1240
+ **Promise\<void\>**
1241
+
1242
+ ### Examples
1243
+
1244
+ ```javascript
1245
+ await KeycloakManager.users.logout({ id: userId });
1246
+ console.log('User logged out from all sessions');
1247
+ ```
1248
+
1249
+ ---
1250
+
1251
+ ## listConsents()
1252
+
1253
+ List user consents for clients.
1254
+
1255
+ **Syntax:**
1256
+ ```javascript
1257
+ const consents = await KeycloakManager.users.listConsents(filter)
1258
+ ```
1259
+
1260
+ ### Parameters
1261
+
1262
+ #### filter (Object) ⚠️ Required
1263
+
1264
+ | Property | Type | Required | Description |
1265
+ |----------|------|----------|-------------|
1266
+ | `id` | string | ⚠️ Yes | User UUID |
1267
+
1268
+ ### Returns
1269
+
1270
+ **Promise\<Array\>** - Array of consent representations
1271
+
1272
+ ### Examples
1273
+
1274
+ ```javascript
1275
+ const consents = await KeycloakManager.users.listConsents({
1276
+ id: userId
1277
+ });
1278
+
1279
+ console.log(`User has granted consent to ${consents.length} clients`);
1280
+ consents.forEach(consent => {
1281
+ console.log(`- ${consent.clientId}: ${consent.grantedClientScopes?.join(', ')}`);
1282
+ });
1283
+ ```
1284
+
1285
+ ---
1286
+
1287
+ ## revokeConsent()
1288
+
1289
+ Revoke user consent for a specific client.
1290
+
1291
+ **Syntax:**
1292
+ ```javascript
1293
+ await KeycloakManager.users.revokeConsent(filter)
1294
+ ```
1295
+
1296
+ ### Parameters
1297
+
1298
+ #### filter (Object) ⚠️ Required
1299
+
1300
+ | Property | Type | Required | Description |
1301
+ |----------|------|----------|-------------|
1302
+ | `id` | string | ⚠️ Yes | User UUID |
1303
+ | `clientId` | string | ⚠️ Yes | Client ID (not UUID) |
1304
+
1305
+ ### Returns
1306
+
1307
+ **Promise\<void\>**
1308
+
1309
+ ### Examples
1310
+
1311
+ ```javascript
1312
+ await KeycloakManager.users.revokeConsent({
1313
+ id: userId,
1314
+ clientId: 'my-app-client'
1315
+ });
1316
+
1317
+ console.log('Consent revoked');
1318
+ ```
1319
+
1320
+ ---
1321
+
1322
+ ## impersonation()
1323
+
1324
+ Impersonate a user (requires impersonation permission).
1325
+
1326
+ **Syntax:**
1327
+ ```javascript
1328
+ const result = await KeycloakManager.users.impersonation(filter)
1329
+ ```
1330
+
1331
+ ### Parameters
1332
+
1333
+ #### filter (Object) ⚠️ Required
1334
+
1335
+ | Property | Type | Required | Description |
1336
+ |----------|------|----------|-------------|
1337
+ | `id` | string | ⚠️ Yes | User UUID |
1338
+
1339
+ ### Returns
1340
+
1341
+ **Promise\<Object\>** - Impersonation result with redirect URL
1342
+
1343
+ ### Examples
1344
+
1345
+ ```javascript
1346
+ const result = await KeycloakManager.users.impersonation({
1347
+ id: userId
1348
+ });
1349
+
1350
+ console.log('Impersonation redirect:', result.redirect);
1351
+ ```
1352
+
1353
+ ---
1354
+
1355
+ ## listFederatedIdentities()
1356
+
1357
+ List federated identities linked to a user.
1358
+
1359
+ **Syntax:**
1360
+ ```javascript
1361
+ const identities = await KeycloakManager.users.listFederatedIdentities(filter)
1362
+ ```
1363
+
1364
+ ### Parameters
1365
+
1366
+ #### filter (Object) ⚠️ Required
1367
+
1368
+ | Property | Type | Required | Description |
1369
+ |----------|------|----------|-------------|
1370
+ | `id` | string | ⚠️ Yes | User UUID |
1371
+
1372
+ ### Returns
1373
+
1374
+ **Promise\<Array\>** - Array of federated identity representations
1375
+
1376
+ ### Examples
1377
+
1378
+ ```javascript
1379
+ const identities = await KeycloakManager.users.listFederatedIdentities({
1380
+ id: userId
1381
+ });
1382
+
1383
+ console.log(`User has ${identities.length} linked identities`);
1384
+ identities.forEach(identity => {
1385
+ console.log(`- ${identity.identityProvider}: ${identity.userName}`);
1386
+ });
1387
+ ```
1388
+
1389
+ ---
1390
+
1391
+ ## addToFederatedIdentity()
1392
+
1393
+ Link a federated identity to a user.
1394
+
1395
+ **Syntax:**
1396
+ ```javascript
1397
+ await KeycloakManager.users.addToFederatedIdentity(options)
1398
+ ```
1399
+
1400
+ ### Parameters
1401
+
1402
+ #### options (Object) ⚠️ Required
1403
+
1404
+ | Property | Type | Required | Description |
1405
+ |----------|------|----------|-------------|
1406
+ | `id` | string | ⚠️ Yes | User UUID |
1407
+ | `federatedIdentityId` | string | ⚠️ Yes | Identity provider alias |
1408
+ | `federatedIdentity` | object | ⚠️ Yes | Identity representation |
1409
+
1410
+ ### Returns
1411
+
1412
+ **Promise\<void\>**
1413
+
1414
+ ### Examples
1415
+
1416
+ ```javascript
1417
+ await KeycloakManager.users.addToFederatedIdentity({
1418
+ id: userId,
1419
+ federatedIdentityId: 'google',
1420
+ federatedIdentity: {
1421
+ identityProvider: 'google',
1422
+ userId: 'google-user-id-123',
1423
+ userName: 'user@gmail.com'
1424
+ }
1425
+ });
1426
+
1427
+ console.log('Federated identity linked');
1428
+ ```
1429
+
1430
+ ---
1431
+
1432
+ ## delFromFederatedIdentity()
1433
+
1434
+ Unlink a federated identity from a user.
1435
+
1436
+ **Syntax:**
1437
+ ```javascript
1438
+ await KeycloakManager.users.delFromFederatedIdentity(options)
1439
+ ```
1440
+
1441
+ ### Parameters
1442
+
1443
+ #### options (Object) ⚠️ Required
1444
+
1445
+ | Property | Type | Required | Description |
1446
+ |----------|------|----------|-------------|
1447
+ | `id` | string | ⚠️ Yes | User UUID |
1448
+ | `federatedIdentityId` | string | ⚠️ Yes | Identity provider alias |
1449
+
1450
+ ### Returns
1451
+
1452
+ **Promise\<void\>**
1453
+
1454
+ ### Examples
1455
+
1456
+ ```javascript
1457
+ await KeycloakManager.users.delFromFederatedIdentity({
1458
+ id: userId,
1459
+ federatedIdentityId: 'google'
1460
+ });
1461
+
1462
+ console.log('Google identity unlinked');
1463
+ ```
1464
+
1465
+ ---
1466
+
1467
+ ## Complete Workflow Example
1468
+
1469
+ ```javascript
1470
+ const KeycloakManager = require('keycloak-api-manager');
1471
+
1472
+ async function userManagementWorkflow() {
1473
+ await KeycloakManager.configure({
1474
+ baseUrl: 'https://keycloak.example.com',
1475
+ realmName: 'master',
1476
+ username: 'admin',
1477
+ password: 'admin',
1478
+ grantType: 'password',
1479
+ clientId: 'admin-cli'
1480
+ });
1481
+
1482
+ KeycloakManager.setConfig({ realmName: 'my-app' });
1483
+
1484
+ try {
1485
+ // 1. Create user
1486
+ const user = await KeycloakManager.users.create({
1487
+ username: 'john.doe',
1488
+ email: 'john@example.com',
1489
+ firstName: 'John',
1490
+ lastName: 'Doe',
1491
+ enabled: true,
1492
+ credentials: [{
1493
+ type: 'password',
1494
+ value: 'tempPassword123',
1495
+ temporary: true
1496
+ }],
1497
+ requiredActions: ['VERIFY_EMAIL']
1498
+ });
1499
+ console.log('✓ User created:', user.id);
1500
+
1501
+ // 2. Find group and add user
1502
+ const groups = await KeycloakManager.groups.find({ search: 'Users' });
1503
+ if (groups.length > 0) {
1504
+ await KeycloakManager.users.addToGroup({
1505
+ id: user.id,
1506
+ groupId: groups[0].id
1507
+ });
1508
+ console.log('✓ Added to group');
1509
+ }
1510
+
1511
+ // 3. Assign realm role
1512
+ const roles = await KeycloakManager.roles.find();
1513
+ const userRole = roles.find(r => r.name === 'user');
1514
+ if (userRole) {
1515
+ await KeycloakManager.users.addRealmRoleMappings({
1516
+ id: user.id,
1517
+ roles: [{ id: userRole.id, name: userRole.name }]
1518
+ });
1519
+ console.log('✓ Role assigned');
1520
+ }
1521
+
1522
+ // 4. List user's effective roles
1523
+ const effectiveRoles = await KeycloakManager.users.listCompositeRealmRoleMappings({
1524
+ id: user.id
1525
+ });
1526
+ console.log(`✓ User has ${effectiveRoles.length} effective roles`);
1527
+
1528
+ // 5. Update user
1529
+ await KeycloakManager.users.update(
1530
+ { id: user.id },
1531
+ {
1532
+ attributes: {
1533
+ department: ['Engineering'],
1534
+ employeeId: ['EMP-789']
1535
+ }
1536
+ }
1537
+ );
1538
+ console.log('✓ User updated');
1539
+
1540
+ // 6. List sessions
1541
+ const sessions = await KeycloakManager.users.listSessions({
1542
+ id: user.id
1543
+ });
1544
+ console.log(`✓ User has ${sessions.length} active sessions`);
1545
+
1546
+ } catch (error) {
1547
+ console.error('✗ Error:', error.message);
1548
+ } finally {
1549
+ KeycloakManager.stop();
1550
+ }
1551
+ }
1552
+
1553
+ userManagementWorkflow();
1554
+ ```
1555
+
1556
+ ---
1557
+
1558
+ ## See Also
1559
+
1560
+ - [API Reference](../api-reference.md) - Complete API index
1561
+ - [Groups API](groups.md) - Group management
1562
+ - [Roles API](roles.md) - Role management
1563
+ - [Configuration](configuration.md) - Authentication setup