This commit is contained in:
parent
249d00177c
commit
6235fe7e68
@ -1,77 +0,0 @@
|
|||||||
package enseirb.myinpulse.utils;
|
|
||||||
|
|
||||||
import enseirb.myinpulse.exceptions.UserNotFoundException;
|
|
||||||
import org.springframework.web.client.RestClient;
|
|
||||||
|
|
||||||
import javax.management.relation.RoleNotFoundException;
|
|
||||||
|
|
||||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
|
||||||
|
|
||||||
public class KeycloakApi {
|
|
||||||
|
|
||||||
static final String keycloakUrl = "http://localhost:7080";
|
|
||||||
static final String realmName = "test";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Uses Keycloak API to retrieve a role representation of a role by its name
|
|
||||||
* @param roleName name of the role
|
|
||||||
* @param bearer authorization header used by the client to authenticate to keycloak
|
|
||||||
*/
|
|
||||||
static public RoleRepresentation getRoleRepresentationByName(String roleName, String bearer) throws RoleNotFoundException {
|
|
||||||
RoleRepresentation[] response = RestClient.builder().baseUrl(keycloakUrl)
|
|
||||||
.defaultHeader("Authorization", bearer)
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/admin/realms/{realmName}/roles/{roleName}", realmName, roleName)
|
|
||||||
.retrieve()
|
|
||||||
.body(RoleRepresentation[].class);
|
|
||||||
|
|
||||||
if (response == null || response.length == 0) {
|
|
||||||
throw new RoleNotFoundException("Role not found");
|
|
||||||
}
|
|
||||||
return response[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
static public String getUserIdByName(String username, String bearer) throws UserNotFoundException {
|
|
||||||
UserRepresentation[] response = RestClient.builder().baseUrl(keycloakUrl)
|
|
||||||
.defaultHeader("Authorization", bearer)
|
|
||||||
.build()
|
|
||||||
.get()
|
|
||||||
.uri("/admin/realms/{realmName}/users?username={username}", realmName, username)
|
|
||||||
.retrieve()
|
|
||||||
.body(UserRepresentation[].class);
|
|
||||||
|
|
||||||
if (response == null || response.length == 0) {
|
|
||||||
throw new UserNotFoundException("User not found");
|
|
||||||
}
|
|
||||||
return response[0].id;
|
|
||||||
}
|
|
||||||
|
|
||||||
static public void setRoleToUser(String username, String roleName, String bearer) throws RoleNotFoundException, UserNotFoundException {
|
|
||||||
RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, bearer);
|
|
||||||
String userId = getUserIdByName(username, bearer);
|
|
||||||
|
|
||||||
|
|
||||||
RestClient.builder().baseUrl(keycloakUrl)
|
|
||||||
.defaultHeader("Authorization", bearer)
|
|
||||||
.build()
|
|
||||||
.post()
|
|
||||||
.uri("/admin/realms/${realmName}/users/${userId}/role-mappings/realm", realmName, userId)
|
|
||||||
.body(roleRepresentation)
|
|
||||||
.contentType(APPLICATION_JSON)
|
|
||||||
.retrieve();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class RoleRepresentation {
|
|
||||||
public String id;
|
|
||||||
public String name;
|
|
||||||
public String description;
|
|
||||||
}
|
|
||||||
|
|
||||||
class UserRepresentation {
|
|
||||||
public String id;
|
|
||||||
public String name;
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package enseirb.myinpulse.utils.keycloak.datatypes;
|
||||||
|
|
||||||
|
public class RoleRepresentation {
|
||||||
|
public String id;
|
||||||
|
public String name;
|
||||||
|
public String description;
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package enseirb.myinpulse.utils.keycloak.datatypes;
|
||||||
|
|
||||||
|
public class UserRepresentation {
|
||||||
|
public String id;
|
||||||
|
public String name;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user