2023-11-29 15:12:56 +01:00
|
|
|
package eu.eudat.service.keycloak;
|
|
|
|
|
2023-12-08 10:48:32 +01:00
|
|
|
import gr.cite.commons.web.keycloak.api.configuration.KeycloakClientConfiguration;
|
2023-11-29 15:12:56 +01:00
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2024-04-01 10:16:19 +02:00
|
|
|
import java.util.*;
|
2023-11-29 15:12:56 +01:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class KeycloakServiceImpl implements KeycloakService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(KeycloakServiceImpl.class));
|
2023-12-08 10:48:32 +01:00
|
|
|
private final MyKeycloakAdminRestApi api;
|
2023-11-29 15:12:56 +01:00
|
|
|
private final KeycloakResourcesConfiguration configuration;
|
2023-12-08 10:48:32 +01:00
|
|
|
private final KeycloakClientConfiguration clientConfiguration;
|
2023-11-29 15:12:56 +01:00
|
|
|
|
|
|
|
@Autowired
|
2023-12-08 10:48:32 +01:00
|
|
|
public KeycloakServiceImpl(MyKeycloakAdminRestApi api, KeycloakResourcesConfiguration configuration, KeycloakClientConfiguration clientConfiguration) {
|
2023-11-29 15:12:56 +01:00
|
|
|
this.api = api;
|
|
|
|
this.configuration = configuration;
|
2023-12-08 10:48:32 +01:00
|
|
|
this.clientConfiguration = clientConfiguration;
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void addUserToGroup(@NotNull String subjectId, String groupId) {
|
|
|
|
api.users().addUserToGroup(subjectId, groupId);
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void removeUserFromGroup(@NotNull String subjectId, String groupId) {
|
|
|
|
api.users().removeUserFromGroup(subjectId, groupId);
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void addUserToGlobalRoleGroup(String subjectId, String role) {
|
|
|
|
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().get(role);
|
|
|
|
if (properties != null) addUserToGroup(subjectId, properties.getGroupId());
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void removeUserGlobalRoleGroup(@NotNull String subjectId, String role) {
|
|
|
|
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().get(role);
|
2023-12-08 12:27:04 +01:00
|
|
|
if (properties != null)
|
|
|
|
removeUserFromGroup(subjectId, properties.getGroupId());
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
2024-04-16 12:17:58 +02:00
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public void assignClientRoleToUser(UUID subjectId, String clientId, KeycloakRole role) {
|
|
|
|
// if (clientId == null)
|
|
|
|
// clientId = clientConfiguration.getProperties().getClientId();
|
|
|
|
// UserRepresentation user = api.users().findUserById(subjectId.toString());
|
|
|
|
// user.getClientRoles().computeIfAbsent(clientId, k -> new ArrayList<>());
|
|
|
|
// Set<String> clientRoles = new HashSet<>(Set.copyOf(user.getClientRoles().get(clientId)));
|
|
|
|
// clientRoles.add(role.name());
|
|
|
|
// user.getClientRoles().get(clientId).clear();
|
|
|
|
// user.getClientRoles().get(clientId).addAll(clientRoles);
|
|
|
|
// api.users().updateUser(subjectId.toString(), user);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public void removeClientRoleFromUser(UUID subjectId, String clientId, KeycloakRole role) {
|
|
|
|
// if (clientId == null) clientId = clientConfiguration.getProperties().getClientId();
|
|
|
|
// UserRepresentation user = api.users().findUserById(subjectId.toString());
|
|
|
|
// user.getClientRoles().computeIfAbsent(clientId, k -> new ArrayList<>());
|
|
|
|
// Set<String> clientRoles = new HashSet<>(Set.copyOf(user.getClientRoles().get(clientId)));
|
|
|
|
// clientRoles.remove(role.name());
|
|
|
|
// user.getClientRoles().get(clientId).clear();
|
|
|
|
// user.getClientRoles().get(clientId).addAll(clientRoles);
|
|
|
|
// api.users().updateUser(subjectId.toString(), user);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public List<GroupRepresentation> getUserGroups(UUID subjectId) {
|
|
|
|
// return api.users().getGroups(subjectId.toString());
|
|
|
|
// }
|
2023-11-29 15:12:56 +01:00
|
|
|
|
2023-12-08 10:48:32 +01:00
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void addUserToTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) {
|
|
|
|
GroupRepresentation group = api.groups().findGroupByPath(getTenantAuthorityParentPath(tenantRole) + "/" + configuration.getTenantGroupName(tenantCode));
|
|
|
|
addUserToGroup(subjectId, group.getId());
|
2023-12-08 10:48:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-16 12:17:58 +02:00
|
|
|
public void removeUserTenantRoleGroup(String subjectId, String tenantCode, String tenantRole) {
|
|
|
|
GroupRepresentation group = api.groups().findGroupByPath(getTenantAuthorityParentPath(tenantRole) + "/" + configuration.getTenantGroupName(tenantCode));
|
|
|
|
removeUserFromGroup(subjectId, group.getId());
|
2023-12-08 10:48:32 +01:00
|
|
|
}
|
2024-04-16 12:17:58 +02:00
|
|
|
|
|
|
|
private String getTenantAuthorityParentPath(String tenantRole) {
|
|
|
|
GroupRepresentation parent = api.groups().findGroupById(configuration.getProperties().getTenantAuthorities().get(tenantRole).getParent());
|
|
|
|
return parent.getPath();
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|
|
|
|
|
2024-04-16 12:17:58 +02:00
|
|
|
@Override
|
|
|
|
public void createTenantGroups(String tenantCode) {
|
|
|
|
for (Map.Entry<String,KeycloakTenantAuthorityProperties> entry :configuration.getProperties().getTenantAuthorities().entrySet()){
|
|
|
|
GroupRepresentation group = new GroupRepresentation();
|
|
|
|
group.setName(configuration.getTenantGroupName(tenantCode));
|
|
|
|
HashMap<String, List<String>> user_attributes = new HashMap<>();
|
|
|
|
user_attributes.put(this.configuration.getProperties().getTenantRoleAttributeName(), List.of(configuration.getTenantRoleAttributeValue(tenantCode, entry.getKey())));
|
|
|
|
group.setAttributes(user_attributes);
|
|
|
|
api.groups().addGroupWithParent(group, configuration.getProperties().getTenantAuthorities().get(entry.getKey()).getParent());
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 15:12:56 +01:00
|
|
|
}
|