Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get users by custom attributes in keycloak?

Tags:

keycloak

I know that there is admin APIs to get the list of users which returns the user representation array.

GET /admin/realms/{realm}/groups/{id}/members

returns

https://www.keycloak.org/docs-api/2.5/rest-api/index.html#_userrepresentation

but is there a way to get users by custom attribute ?

like image 398
Milan Savaliya Avatar asked Feb 13 '19 10:02

Milan Savaliya


People also ask

What are user attributes in Keycloak?

Keycloak is a third-party authorization server that manages users of our web or mobile applications. It offers some default attributes, such as first name, last name, and email to be stored for any given user.

How do I add a user attribute to a Keycloak?

Add custom attribute Before we start, you must be logged in as an admin in keycloak. In the left menu bar click on Users , choose a user, in our case (johndoe) and the click Attributes tab. Enter a key and value and click the Add button on the right side. Now click the Save button.

How do I add a custom claim to a Keycloak?

Adding in request a custom header containing our custom claims json - base64 encoded. Implement a protocol mapper, which configuration of the name of the custom header and type (Json) The mapper read the custom header value, decode the value and the json value to the access token.


4 Answers

This is enabled out of the box from Keycloak version 15.1.0

Using GET /{realm}/users API, parameter q is introduced: A query to search for custom attributes, in the format 'key1:value2 key2:value2'

curl 'http://{{keycloak_url}}/auth/admin/realms/{{realm}}/users?q=phone:123456789'

You can also combine several attributes within this parameter using space ' ' delimiter

curl 'http://{{keycloak_url}}/auth/admin/realms/{{realm}}/users?q=phone:123456789 country:USA'

Docs: https://www.keycloak.org/docs-api/15.1/rest-api/index.html#_users_resource

like image 53
Darko Avatar answered Oct 18 '22 07:10

Darko


This is not possible by default, but Keycloak offers the possibility to extend its functionalities via a system of Service Provider Interfaces which is very easy to implement.

Here is an example of new route that allows to search by custom attributes :

public class SearchByAttributeResourceProvider implements RealmResourceProvider {
    private KeycloakSession session;

    public SearchByAttributeResourceProvider(KeycloakSession session) {
        this.session = session;
    }

    @Override
    public Object getResource() {
        return this;
    }

    @GET
    @Path("search-by-stuff/{stuffValue}")
    @Produces({MediaType.APPLICATION_JSON})
    public List<UserRepresentation> getUsersByStuff(@PathParam("stuffValue") String stuffValue) {
        return session
                .users()
                .searchForUserByUserAttribute("stuff", stuffValue, session.getContext().getRealm())
                                .stream()  
                                .map(userModel -> ModelToRepresentation.toRepresentation(session, session.getContext().getRealm(), userModel))
                                .collect(toList());
    } 

    @Override
    public void close() {

    }
}

You'll find more details here : https://www.keycloak.org/docs/latest/server_development/index.html#_extensions_rest

like image 32
Lucas Declercq Avatar answered Oct 18 '22 07:10

Lucas Declercq


With latest version of keycloak (18.01), we have api in


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    List<UserRepresentation> searchByAttributes(@QueryParam("q") String searchQuery);

The query param is of format 'key:value' . Using this we can get list of all users by custom attributes

like image 2
Anil Bhat Avatar answered Oct 18 '22 07:10

Anil Bhat


Current Keycloak API version is 4.8 and there is API: Get users Returns a list of users, filtered according to query parameters

GET /{realm}/users

See doc: https://www.keycloak.org/docs-api/4.8/rest-api/index.html#_users_resource

Only this "search" is available from the API. If you need search by user attributes, then you need to implement it in your own code.

like image 1
Jan Garaj Avatar answered Oct 18 '22 07:10

Jan Garaj