Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the list of users from keycloak by using httppost/rest-api

I am new to keycloak any help will be appreciable. I'm able to get the list of user details by using the keycloak api, I want to know how we can get it by using http-post.

Keycloak kc = Keycloak.getInstance("url-path", "realm", "username", "password", "admin-cli");

By using this keycloak object I am able to get the list of users. But when I want to get the list of users by using http-post or rest api call by using the end point url which is given by keycloak I am not able to get the access token based on token I am getting only the particular user , I want list of users by using rest api's.

like image 453
raana Avatar asked Jan 23 '18 04:01

raana


People also ask

How do I get a list of users from a Keycloak?

Keycloak kc = Keycloak. getInstance("url-path", "realm", "username", "password", "admin-cli"); By using this keycloak object I am able to get the list of users.

What is Keycloak REST API?

Keycloak comes with a fully functional Admin REST API with all features provided by the Admin Console. To invoke the API you need to obtain an access token with the appropriate permissions. The required permissions are described in Server Administration.


2 Answers

According to the Offical Keycloak Admin REST API Docs (Scroll down a bit to the Get users Returns a list of users, filtered according to query parameters section), you will find that there are 2 query parameters available: first and max.

If you set first as 10 and max as 15, then first 10 users will be skipped, and you will receive the next 15 i.e. users 11 to 25 in response.

Use the following request:

GET https://$KEYCLOAK_HOST/auth/admin/$REALM/users?first=10&max=15

P.S. - Don't forget the Authorization Header.

like image 166
retr0 Avatar answered Oct 04 '22 21:10

retr0


Below are the steps to achieve this by key-cloak rest-api first create the url

String fetchAllUsers = URL+"/admin/realms/"+realmName+"/users?";

Now if you have millions of users you don't want to do fetch all user in one go so keycloak support rest-api with pagination,in your rest post query you can ask tenantName,queryString,Limit,PageNumber

fetchAllUsers(String tenantName, List<String> queryString, String limit, String pageNumber) 

now these thing will help to create a query which will send only that many record which will not impact performance of keycloak as well as web-api.

In queryString user can pass the filter criteria like if he want to search by name,lastname,email-id or user want result in ascending or descending order.That you have to parse and create a query String so it will resemble to the rest query of keycloak. After doing that you can concatenate the query String with fetchAllUsers .

Your query will be like this

https://<IP ADDRESS>/<PORT>/admin/realms/<REALM NAME>/users?q=username=<USER NAME>ASC&email=<EMAIL ID>&limit=<RECORD LIMIT>&page=<PAGE NO>

Please ignore typo if you found anything.

like image 42
Subodh Joshi Avatar answered Oct 04 '22 21:10

Subodh Joshi