Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign role to user Keycloak Admin Client

I'm having a problem trying to assign role to user via keycloak admin client. It always returns bad request. First I created a new realm in keycloak with new client. I created couple of users and assigned all roles from realm-management client to one of the users. I used credentials of that user in KeycloakBuilder.

Now admin client can create users, but assigning role to user returns 400 Bad Request. Following is the code-snippet

class KeycloakKonfig {
    var keycloak: Keycloak = KeycloakBuilder.builder()
        .serverUrl("http://localhost:8080/auth")
        .username("username")
        .password("password")
        .realm("realm")
        .clientId("clientId")
        .clientSecret("clientSecret")

        .resteasyClient(ResteasyClientBuilder().connectionPoolSize(20).register(CustomJacksonProvider()).build())

        .build()
}

method body in controller

val savedRoleRepresentation = KeycloakKonfig().keycloak.realm("realm").roles().get("role").toRepresentation()
KeycloakKonfig().keycloak.realm("realm").users().get("userId").roles().realmLevel().add(Arrays.asList(savedRoleRepresentation))

Thanks

like image 764
Ikromjon Mirzayev Avatar asked Oct 16 '22 08:10

Ikromjon Mirzayev


1 Answers

The problem was the keycloak version. I had Keycloak 4.5.0 which does not allow creating roles and assigning it to user via KeycloakBuilder. I switched to Keycloak 4.8.3 and everything works perfect.

like image 181
Ikromjon Mirzayev Avatar answered Oct 20 '22 16:10

Ikromjon Mirzayev