feat: created a better account creation flow
All checks were successful
Format / formatting (push) Successful in 6s
Build / build (push) Successful in 5m20s
CI / build (push) Successful in 11s

This commit is contained in:
Pierre Tellier
2025-04-09 17:39:43 +02:00
parent 385c5cd8d0
commit 66be0baca6
14 changed files with 377 additions and 88 deletions

View File

@ -6,12 +6,17 @@ import enseirb.myinpulse.exception.UserNotFoundException;
import enseirb.myinpulse.model.RoleRepresentation;
import enseirb.myinpulse.model.UserRepresentation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.client.RestClient;
import java.util.List;
import javax.management.relation.RoleNotFoundException;
public class KeycloakApi {
protected static final Logger logger = LogManager.getLogger();
static final String keycloakUrl;
static final String realmName;
@ -29,44 +34,48 @@ public class KeycloakApi {
realmName = System.getenv("VITE_KEYCLOAK_REALM");
}
static String toBearer(String b) {
return "Bearer " + b;
}
/**
* 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
* @param token authorization header used by the client to authenticate to keycloak
*/
public static RoleRepresentation getRoleRepresentationByName(String roleName, String bearer)
public static RoleRepresentation getRoleRepresentationByName(String roleName, String token)
throws RoleNotFoundException {
RoleRepresentation[] response =
RoleRepresentation response =
RestClient.builder()
.baseUrl(keycloakUrl)
.defaultHeader("Authorization", bearer)
.defaultHeader("Authorization", toBearer(token))
.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];
.body(RoleRepresentation.class);
/*
{"id":"7a845f2e-c832-4465-8cd8-894d72bc13f1","name":"MyINPulse-entrepreneur","description":"Role for entrepreneur","composite":false,"clientRole":false,"containerId":"0d6f691b-e328-471a-b89e-c30bd7e5b6b0","attributes":{}}
*/
// TODO: check what happens when role does not exist
return response;
}
/**
* Use keycloak API to to retreive a userID via his name or email.
*
* @param username username or mail of the user
* @param bearer bearer of the user, allowing access to database
* @param token bearer of the user, allowing access to database
* @return the userid, as a String
* @throws UserNotFoundException
*/
public static String getUserIdByName(String username, String bearer)
public static String getUserIdByName(String username, String token)
throws UserNotFoundException {
UserRepresentation[] response =
RestClient.builder()
.baseUrl(keycloakUrl)
.defaultHeader("Authorization", bearer)
.defaultHeader("Authorization", toBearer(token))
.build()
.get()
.uri(
@ -91,27 +100,26 @@ public class KeycloakApi {
*
* @param username
* @param roleName
* @param bearer
* @param token
* @throws RoleNotFoundException
* @throws UserNotFoundException
*/
public static void setRoleToUser(String username, String roleName, String bearer)
public static void setRoleToUser(String username, String roleName, String token)
throws RoleNotFoundException, UserNotFoundException {
RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, bearer);
String userId = getUserIdByName(username, bearer);
RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, token);
String userId = getUserIdByName(username, token);
List<RoleRepresentation> rolesToAdd = List.of(roleRepresentation);
logger.debug("Adding role {} to user {}", roleRepresentation.id, userId);
RestClient.builder()
.baseUrl(keycloakUrl)
.defaultHeader("Authorization", bearer)
.defaultHeader("Authorization", toBearer(token))
.build()
.post()
.uri(
"/admin/realms/${realmName}/users/${userId}/role-mappings/realm",
realmName,
userId)
.body(roleRepresentation)
.uri("/admin/realms/" + realmName + "/users/" + userId + "/role-mappings/realm")
.body(rolesToAdd)
.contentType(APPLICATION_JSON)
.retrieve();
.retrieve()
.toBodilessEntity();
}
/**