keycloak-api-manager 1.0.0 → 2.0.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/README.md CHANGED
@@ -10,6 +10,8 @@ API calls, and error management internally. Designed for developers who want ful
10
10
  without manually invoking REST endpoints, keycloak-api-manager enables fast, reliable,
11
11
  and type-safe operations across multiple realms and environments.
12
12
  it is a wrapper based on '@keycloak/keycloak-admin-client'
13
+
14
+
13
15
  ---
14
16
  ## 📦 Features
15
17
 
@@ -66,10 +68,8 @@ yarn add keycloak-api-manager
66
68
  ---
67
69
 
68
70
  ## 🛠️ Get Keycloak Configuration
69
-
70
71
  Copy or Download from keycloak admin page your client configuration `keycloak.json` by visiting
71
72
  the Keycloak Admin Console → clients (left sidebar) → choose your client → Installation → Format Option → Keycloak OIDC JSON → Download
72
-
73
73
  ```json
74
74
  {
75
75
  "realm": "your-realm",
@@ -82,19 +82,57 @@ the Keycloak Admin Console → clients (left sidebar) → choose your client →
82
82
  "confidential-port": 0
83
83
  }
84
84
  ```
85
-
86
85
  ---
87
86
 
88
87
  ## 📄 Usage Example
89
88
 
90
89
  ```js
91
90
  const express = require('express');
92
- const keycloackAdapter = require('keycloak-api-manager');
91
+ // Import the Keycloak API Manager
92
+ const KeycloakManager = require('keycloak-api-manager');
93
93
 
94
94
  const app = express();
95
95
 
96
96
 
97
- // Configure and Initialize Keycloak adapter
97
+ // Configure and Initialize Keycloak manager api
98
+ // Initialize the manager with Keycloak credentials
99
+ const keycloak = new KeycloakManager({
100
+ baseUrl: 'http://localhost:8080', // Keycloak base URL
101
+ realm: 'master', // Realm where the admin user belongs
102
+ clientId: 'admin-cli', // Keycloak admin client
103
+ username: 'admin', // Admin username
104
+ password: 'admin', // Admin password
105
+ });
106
+
107
+ // Authenticate and obtain access token
108
+ await keycloak.authenticate();
109
+
110
+ // Create a new user in the target realm
111
+ const newUser = await keycloak.createUser({
112
+ realm: 'myrealm',
113
+ username: 'john.doe',
114
+ email: 'john.doe@example.com',
115
+ firstName: 'John',
116
+ lastName: 'Doe',
117
+ enabled: true,
118
+ });
119
+
120
+ // Assign a realm role to the user
121
+ await keycloak.assignRealmRole({
122
+ realm: 'myrealm',
123
+ userId: newUser.id,
124
+ roleName: 'user',
125
+ });
126
+
127
+ console.log(`✅ User ${newUser.username} created and assigned to role 'user'`);
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
98
136
  await keycloackAdapter.configure(app,{
99
137
  "realm": "Realm-Project",
100
138
  "auth-server-url": "https://YourKeycloakUrl:30040/",
@@ -1864,21 +1902,6 @@ console.log(ressult);
1864
1902
  ```
1865
1903
 
1866
1904
 
1867
- ##### `function getCredentials(filter)`
1868
- getCredentials() method retrieves the list of credentials (e.g., passwords, OTPs, WebAuthn, etc.)
1869
- currently associated with a given user in a specific realm.
1870
- This is useful for auditing, checking what types of credentials a user has set up,
1871
- or managing credentials such as password reset, WebAuthn deletion, etc.
1872
- @parameters:
1873
- - getCredentials: is a JSON object that accepts filter parameters
1874
- - id: [Required] the user ID to update
1875
- - realm [Optional] the realm name (defaults to current realm)
1876
- ```js
1877
- // get credentials info for user whose id is 'user-id'
1878
- const ressult = await keycloakAdapter.kcAdminClient.users.getCredentials({id: 'user-id'});
1879
- console.log(ressult);
1880
- ```
1881
-
1882
1905
  ##### `function deleteCredential(accountInfo)`
1883
1906
  deleteCredential method allows you to delete a specific credential (e.g., password, OTP, WebAuthn, etc.) from a user.
1884
1907
  This is useful when you want to invalidate or remove a credential, forcing the user to reconfigure or reset it.
@@ -1899,7 +1922,7 @@ It is a method that retrieves the user profile dictionary information.
1899
1922
  This includes basic user details such as username, email, first name, last name,
1900
1923
  and other attributes associated with the user profile in the Keycloak realm.
1901
1924
  ```js
1902
- // create a role name called my-role
1925
+ // Get user profile
1903
1926
  const userProfile = await keycloakAdapter.kcAdminClient.users.getProfile();
1904
1927
  console.log('User profile dicionary:', userProfile);
1905
1928
  ```
@@ -3649,8 +3672,7 @@ meaning it can define resources, scopes, permissions, and policies for fine-grai
3649
3672
 
3650
3673
  // get resource Server
3651
3674
  const resourceServer = await keycloakAdapter.kcAdminClient.clients.getResourceServer({
3652
- id: 'internal-client-id',
3653
- resourceId: '12345-abcde',
3675
+ id: 'internal-client-id'
3654
3676
  });
3655
3677
 
3656
3678
  console.log('Resource Server:', resourceServer);
@@ -3910,8 +3932,7 @@ Resources represent protected entities (such as APIs, files, or services) that c
3910
3932
 
3911
3933
  // List resources
3912
3934
  const resources= await keycloakAdapter.kcAdminClient.clients.listResources({
3913
- id: 'internal-client-id',
3914
- resourceId: 'resource-id'
3935
+ id: 'internal-client-id'
3915
3936
  });
3916
3937
 
3917
3938
  console.log("Resources:", resources);
@@ -5488,7 +5509,7 @@ console.log("Mapper updated successfully!");
5488
5509
 
5489
5510
 
5490
5511
 
5491
- ##### `function identityProviders.importFromUrl(filter,mapperRepresentation)`
5512
+ ##### `function identityProviders.importFromUrl(filter)`
5492
5513
  The method lets you import an Identity Provider configuration directly from a metadata URL (e.g., OIDC discovery document or SAML metadata XML).
5493
5514
  This saves you from manually entering configuration details, since Keycloak can auto-fill them from the provided URL.
5494
5515
  @parameters:
@@ -5559,6 +5580,15 @@ Groups help organize users and assign permissions in a scalable way
5559
5580
  #### `entity groups functions`
5560
5581
  ##### `function create(groupRappresentation)`
5561
5582
  Create a new group in the current realme
5583
+ @parameters:
5584
+ - groupRepresentation:An object representing the new state of the group. You can update properties such as:
5585
+ - name: [optional] New name of the group
5586
+ - attributes: [optional] Custom attributes up field
5587
+ - path: [optional] full path of the group
5588
+ - subGroups: [optional] List of child groups (can also be updated separately)
5589
+ - description: [optional] the new group Description
5590
+ - {other [optional] group descriprion fields}
5591
+
5562
5592
  ```js
5563
5593
  // create a group called my-group
5564
5594
  keycloakAdapter.kcAdminClient.groups.create({name: "my-group"});
@@ -5908,12 +5938,21 @@ Create a new role
5908
5938
  // create a role name called my-role
5909
5939
  keycloakAdapter.kcAdminClient.roles.create({name:'my-role'});
5910
5940
  ```
5911
- ##### `function createComposite(params: { roleId: string }, payload: RoleRepresentation[]`
5912
- Create a new composite role
5913
- Composite roles in Keycloak are roles that combine other roles, allowing you to group multiple permissions
5914
- into a single, higher-level role. A composite role can include roles from the same realm as well
5915
- as roles from different clients. When you assign a composite role to a user,
5916
- they automatically inherit all the roles it contains.
5941
+ ##### `function createComposite(filters,[roles])`
5942
+ Create a new composite role. Composite roles in Keycloak are roles that combine other roles,
5943
+ allowing you to group multiple permissions into a single, higher-level role.
5944
+ A composite role can include roles from the same realm as well
5945
+ as roles from different clients.
5946
+ When you assign a composite role to a user, they automatically inherit all the roles it contains.
5947
+ @parameters:
5948
+ - filters: parameter provided as a JSON object that accepts the following parameters:
5949
+ - roleId: [required] The id of the role to which composite roles will be added.
5950
+
5951
+ - roles: (Array<RoleRepresentation>) [required] A list of roles to be added as composites. Each RoleRepresentation typically includes:
5952
+ - id: [required] The role’s unique ID.
5953
+ - name: [required] The role’s name.
5954
+ - containerId: [optional] The realm or client that owns the role.
5955
+ - clientRole: [optional] Whether the role belongs to a client.
5917
5956
 
5918
5957
 
5919
5958
  ```js
@@ -5924,69 +5963,106 @@ const readerRole = await client.roles.findOneByName({ name: 'reader' });
5924
5963
  await client.roles.createComposite({ roleId: adminRole.id }, [readerRole]);
5925
5964
  ```
5926
5965
 
5927
- ##### `function find()`
5966
+ ##### `function find(filters)`
5928
5967
  get all realm roles and return a JSON
5968
+ - filters: parameter provided as a JSON object that accepts the following parameters:
5969
+ - name (string, optional): Search string to filter roles by name
5970
+ - realm (string, optional: if set globally in the client): The realm from which to retrieve roles.
5971
+ - first (number, optional): Index of the first result to return (used for pagination).
5972
+ - max (number, optional): Maximum number of results to return.
5929
5973
  ```js
5930
5974
  keycloakAdapter.kcAdminClient.roles.find();
5931
5975
  ```
5932
- ##### `function findOneByName(filter)`
5933
- get a role by name
5976
+ ##### `function findOneByName(filters)`
5977
+ Get a role by name
5978
+ @parameters:
5979
+ - filters: parameter provided as a JSON object that accepts the following parameters:
5980
+ - name (string, required) — The exact name of the role to retrieve.
5981
+ - realm (string, optional if set globally) — The realm where the role is defined.
5934
5982
  ```js
5935
5983
  // get information about 'my-role' role
5936
5984
  keycloakAdapter.kcAdminClient.roles.findOneByName({ name: 'my-role' });
5937
5985
  ```
5938
5986
 
5939
- ##### `function findOneById(filter)`
5940
- get a role by its Id
5987
+ ##### `function findOneById(filters)`
5988
+ Get a role by its Id
5989
+ @parameters:
5990
+ - filters: parameter provided as a JSON object that accepts the following parameters:
5991
+ - Id (string, required) — The Id of the role to retrieve.
5992
+ - realm (string, optional if set globally) — The realm where the role is defined.
5941
5993
  ```js
5942
5994
  // get information about 'my-role-id' role
5943
5995
  keycloakAdapter.kcAdminClient.roles.findOneById({ id: 'my-role-id' });
5944
5996
  ```
5945
5997
 
5946
- ##### `function updateByName(filter,role_dictionary)`
5947
- update a role by its name
5998
+ ##### `function updateByName(filters,role_dictionary)`
5999
+ Update a role by its name
6000
+ @parameters:
6001
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6002
+ - name (string, required) — The exact name of the role to retrieve.
6003
+ - realm (string, optional if set globally) — The realm where the role is defined.
6004
+ - role_dictionary: A JSON object representing a role dictionary as defined in Keycloak
5948
6005
  ```js
5949
6006
  // update 'my-role' role with a new description
5950
6007
  keycloakAdapter.kcAdminClient.roles.updateByName({ name: 'my-role' }, {description:"new Description"});
5951
6008
  ```
5952
6009
 
5953
- ##### `function updateById(filter,role_dictionary)`
5954
- update a role by its id
6010
+ ##### `function updateById(filters,role_dictionary)`
6011
+ Update a role by its Id
6012
+ @parameters:
6013
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6014
+ - name (string, required) — The exact name of the role to retrieve.
6015
+ - realm (string, optional if set globally) — The realm where the role is defined.
6016
+ - role_dictionary: A JSON object representing a role dictionary as defined in Keycloak
5955
6017
  ```js
5956
6018
  // update role by id 'my-role-id' with a new description
5957
6019
  keycloakAdapter.kcAdminClient.roles.updateById({ id: 'my-role-id' }, {description:"new Description"});
5958
6020
  ```
5959
6021
 
5960
- ##### `function delByName(filter)`
5961
- delete a role by its name
6022
+ ##### `function delByName(filters)`
6023
+ Delete a role by its name
6024
+ @parameters:
6025
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6026
+ - name (string, required) — The exact name of the role to retrieve.
6027
+ - realm (string, optional if set globally) — The realm where the role is defined.
5962
6028
  ```js
5963
6029
  // delete role 'my-role'
5964
6030
  keycloakAdapter.kcAdminClient.roles.delByName({ name: 'my-role' });
5965
6031
  ```
5966
6032
 
5967
- ##### `function findUsersWithRole(filter)`
5968
- Find all users associated with a specific role.
6033
+ ##### `function findUsersWithRole(filters)`
6034
+ Find all users associated with a specific role
6035
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6036
+ - name: (string, optional) — The exact name of the role to retrieve.
6037
+ - id: (string, optional) — The Id of the role to retrieve.
6038
+ - realm: (string, optional if set globally) — The realm where the role is defined.
5969
6039
  ```js
5970
6040
  // Find all users associated with role named 'my-role'
5971
6041
  keycloakAdapter.kcAdminClient.roles.findUsersWithRole({ name: 'my-role' });
5972
6042
  ```
5973
6043
 
5974
- ##### `function getCompositeRoles({id:roleid})`
5975
- Find all composite roles associated with a specific id.
6044
+ ##### `function getCompositeRoles(filters)`
6045
+ Find all composite roles associated with a specific role.
6046
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6047
+ - name: (string, optional) — The exact name of the role to retrieve.
6048
+ - id: (string, optional) — The Id of the role to retrieve.
5976
6049
  ```js
5977
6050
  // Find all composite role named 'my-role' and id 'my-role-id'
5978
6051
  keycloakAdapter.kcAdminClient.roles.getCompositeRoles({ id: 'my-role-id' });
5979
6052
  ```
5980
6053
 
5981
6054
  ##### `function getCompositeRolesForRealm({roleId:roleid})`
5982
- The getCompositeRolesForRealm function is used to
5983
- retrieve all realm-level roles that are associated with a given composite role.
5984
- When a role is defined as composite, it can include other roles either from the same
6055
+ The getCompositeRolesForRealm function is used to retrieve all realm-level roles that are
6056
+ associated with a given composite role.
6057
+ When a role is defined as composite, it can include other roles either from the same
5985
6058
  realm or from different clients. This specific method returns only the realm-level roles
5986
- that have been added to the composite role. It requires the roleId of the target role as a
6059
+ that have been added to the composite role. It requires the roleId of the target role as a
5987
6060
  parameter and returns an array of RoleRepresentation objects. If the role is not composite
5988
- or has no associated realm roles, the result will be an empty array. This method is useful
6061
+ or has no associated realm roles, the result will be an empty array. This method is useful
5989
6062
  for understanding and managing hierarchical role structures within a realm in Keycloak.
6063
+ @parameters:
6064
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6065
+ - roleId: (string, required) — The Id of the role to retrieve.
5990
6066
  ```js
5991
6067
  const compositeRoles = await keycloakAdapter.kcAdminClient.roles.getCompositeRolesForRealm({ roleId: 'role-id' });
5992
6068
  console.log('admin composite roles:', compositeRoles.map(r => r.name));
@@ -5994,14 +6070,18 @@ console.log('admin composite roles:', compositeRoles.map(r => r.name));
5994
6070
  ```
5995
6071
 
5996
6072
  ##### `function getCompositeRolesForClient({roleId:'roleid', clientId:'clientId'})`
5997
- The getCompositeRolesForClient function is used to retrieve
5998
- all client-level roles that are associated with a given composite role.
6073
+ The getCompositeRolesForClient function is used to retrieve all client-level roles that are
6074
+ associated with a given composite role.
5999
6075
  Composite roles in Keycloak can include roles from different clients,
6000
6076
  and this method specifically returns the roles belonging to a specified client that
6001
- are part of the composite role. It requires the roleId of the composite role
6077
+ are part of the composite role. It requires the roleId of the composite role
6002
6078
  and the clientId of the client whose roles you want to retrieve. The function returns an array of
6003
- RoleRepresentation objects representing the client roles included in the composite.
6079
+ RoleRepresentation objects representing the client roles included in the composite.
6004
6080
  This helps manage and inspect client-specific role hierarchies within the composite role structure in Keycloak.
6081
+ @parameters:
6082
+ - filters: parameter provided as a JSON object that accepts the following parameters:
6083
+ - roleId: (string, required) — The Id of the role to retrieve
6084
+ - clientId: (string, required) — The Id of the client to search for composite roles
6005
6085
  ```js
6006
6086
  const compositeRoles = await keycloakAdapter.kcAdminClient.roles.getCompositeRolesForClient({
6007
6087
  roleId: 'compositeRole-Id',
@@ -6020,7 +6100,7 @@ Components in Keycloak are modular and pluggable, and this API lets you create,
6020
6100
 
6021
6101
  #### `entity components functions`
6022
6102
 
6023
- ##### `function create(comoponentReppresentation)`
6103
+ ##### `function create(componentReppresentation)`
6024
6104
  The method creates a new component in a Keycloak realm.
6025
6105
  Components are modular providers in Keycloak, such as user federation providers (LDAP, Kerberos), authenticators, identity providers, or other pluggable extensions.
6026
6106
 
@@ -6030,7 +6110,12 @@ Components are modular providers in Keycloak, such as user federation providers
6030
6110
  - providerId: [required] The provider ID (e.g., "ldap", "kerberos", "totp").
6031
6111
  - providerType: [required] The type/class of the provider (e.g., "org.keycloak.storage.UserStorageProvider").
6032
6112
  - parentId: [optional] The ID of the parent component (if hierarchical).
6033
- - config: [optional] A map of configuration options, where each property is an array of strings (Keycloak convention).
6113
+ - config: [optional] A map of configuration options, where each property is an array of strings (Keycloak convention). Example:
6114
+ - enabled: ["true"],
6115
+ - connectionUrl: ["ldap://ldap.example.com"],
6116
+ - bindDn: ["cn=admin,dc=example,dc=com"],
6117
+ - bindCredential: ["secret"],
6118
+ - usersDn: ["ou=users,dc=example,dc=com"]
6034
6119
  ```js
6035
6120
  // create a component called my-ldap
6036
6121
  const newComponent= await keycloakAdapter.kcAdminClient.components.create({
@@ -6051,19 +6136,25 @@ console.log("Created component:", newComponent);
6051
6136
  ```
6052
6137
 
6053
6138
 
6054
- ##### `function update(comoponentReppresentation)`
6139
+ ##### `function update(componentReppresentation)`
6055
6140
  The method updates an existing component in a Keycloak realm.
6056
- Components represent pluggable extensions such as user federation providers (LDAP, Kerberos), protocol mappers, authenticator factories, or other custom integrations.
6141
+ Components represent pluggable extensions such as user federation providers (LDAP, Kerberos),
6142
+ protocol mappers, authenticator factories, or other custom integrations.
6057
6143
 
6058
6144
  @parameters:
6059
6145
  - filter: parameter provided as a JSON object that accepts the following filter:
6060
- - id: [required] The unique ID of the component to update.
6061
- - comoponentReppresentation: An object representing the component to update.
6062
- - name: [required] A human-readable name for the component.
6063
- - providerId: [required] The provider ID (e.g., "ldap", "kerberos", "totp").
6064
- - providerType: [required] The type/class of the provider (e.g., "org.keycloak.storage.UserStorageProvider").
6065
- - parentId: [optional] The ID of the parent component (if hierarchical).
6066
- - config: [optional] A map of configuration options, where each property is an array of strings (Keycloak convention).
6146
+ - id: [required] The unique ID of the component to update.
6147
+ - componentRepresentation: An object representing the component to update.
6148
+ - name: [required] A human-readable name for the component.
6149
+ - providerId: [required] The provider ID (e.g., "ldap", "kerberos", "totp").
6150
+ - providerType: [required] The type/class of the provider (e.g., "org.keycloak.storage.UserStorageProvider").
6151
+ - parentId: [optional] The ID of the parent component (if hierarchical).
6152
+ - config: [optional] A map of configuration options, where each property is an array of strings (Keycloak convention). Example:
6153
+ - enabled: ["true"],
6154
+ - connectionUrl: ["ldap://ldap.example.com"],
6155
+ - bindDn: ["cn=admin,dc=example,dc=com"],
6156
+ - bindCredential: ["secret"],
6157
+ - usersDn: ["ou=users,dc=example,dc=com"]
6067
6158
  ```js
6068
6159
  // update a component
6069
6160
  await keycloakAdapter.kcAdminClient.components.update(