Monday, 11 January 2016

Resetting password of a keycloak user using Rest Service

Not all the time, the users will want to reset the password the in keycloak admin console. Once the keycloak authentication is implemented in an application, there will be scenarios, where password reset has to be done from the third party application. 

Such scenarios can be handled either by keycloak java api or REST services exposed by keycloak.

In this post, let’s see how the password reset of a user in keycloak can be performed by using REST services.
From the code below, keycloak security context can be fetched from the http request.
KeycloakSecurityContext session = (KeycloakSecurityContext)httpreq.getAttribute(KeycloakSecurityContext.class.getName());
String req = “http://localhost:8081/auth/admin/realms/realm_name/users/userId/reset-password";
String jsonBody = "{\"type\":\"password\",\"value\":\"password\",\"temporary\":\"false\"}";
ClientRequest clientRequest = new ClientRequest(req);
clientRequest.body("application/json", jsonBody);
clientRequest.accept("application/json");
clientRequest.header("Authorization", "Bearer " + session.getTokenString());
ClientResponse clientresponse = clientRequest.put(String.class);
String resp = (String) clientresponse.getEntity();

Realm_name          -            Realm name in which the user is created
userId                       -            User id
jsonBody 
security           -           Type of security
value               -           Password to be set for the user
Temporary      -           false (makes sure that user need not change the password after logging into the application)
http://localhost:8081/auth - Keycloak admin console.

No comments:

Post a Comment