Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Bug fixes and improvements for ClientEventProtocol and role management

Rich Turnerccc200a75218
diff --git a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
index f58492b5c..31413932a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
@@ -64,7 +64,7 @@ public class ClientEventProtocol extends AbstractProtocol<ClientEventAgent, Agen
protocolClientEventService.addExchangeInterceptor(this::onMessageIntercept);
- String clientId = CLIENT_ID_PREFIX + agent.getId();
+ String clientId = CLIENT_ID_PREFIX + agent.getId().substring(12);
String clientSecret = agent.getClientSecret().orElse(UUID.randomUUID().toString());
ClientRole[] roles = agent.getClientRoles().orElse(null);
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
index e2a452199..a55246fa4 100644
--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
@@ -335,10 +335,10 @@ public class ClientEventService implements ProtocolClientEventService {
// Check if exists already
ClientRepresentation clientRepresentation = keycloakIdentityProvider.getClient(clientCredentials.getRealm(), clientCredentials.getClientId());
+ boolean isNew = clientRepresentation == null;
if (clientRepresentation == null) {
clientRepresentation = new ClientRepresentation();
- clientRepresentation.setId(UUID.nameUUIDFromBytes(clientCredentials.getClientId().getBytes()).toString());
clientRepresentation.setStandardFlowEnabled(false);
clientRepresentation.setImplicitFlowEnabled(false);
clientRepresentation.setDirectAccessGrantsEnabled(false);
@@ -349,11 +349,15 @@ public class ClientEventService implements ProtocolClientEventService {
clientRepresentation.setSecret(clientCredentials.getSecret());
LOG.info("Creating/updating client event service client credentials: " + clientCredentials);
- keycloakIdentityProvider.createClient(clientCredentials.getRealm(), clientRepresentation);
+ if (isNew) {
+ keycloakIdentityProvider.createClient(clientCredentials.getRealm(), clientRepresentation);
+ } else {
+ keycloakIdentityProvider.updateClient(clientCredentials.getRealm(), clientRepresentation);
+ }
User serviceUser = keycloakIdentityProvider.getClientServiceUser(clientCredentials.getRealm(), clientCredentials.getClientId());
if (clientCredentials.getRoles() != null && clientCredentials.getRoles().length > 0) {
- keycloakIdentityProvider.updateUserRoles(clientCredentials.getRealm(), serviceUser.getId(), KEYCLOAK_CLIENT_ID, Arrays.stream(clientCredentials.getRoles()).map(ClientRole::getValue).toArray(String[]::new));
+ keycloakIdentityProvider.updateUserRoles(clientCredentials.getRealm(), serviceUser.getId(), clientCredentials.getClientId(), Arrays.stream(clientCredentials.getRoles()).filter(Objects::nonNull).map(ClientRole::getValue).toArray(String[]::new));
}
}
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
index 5322cef5e..f1d7a39f9 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -395,6 +395,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
List<RoleRepresentation> addRoles = roles == null ? Collections.emptyList() : Arrays.stream(roles)
.filter(cr -> clientMappedRoles.stream().noneMatch(r -> r.getName().equals(cr)))
.map(cr -> availableRoles.stream().filter(r -> r.getName().equals(cr)).findFirst().orElse(null))
+ .filter(Objects::nonNull)
.collect(Collectors.toList());
// Remove obsolete roles
diff --git a/model/src/main/java/org/openremote/model/security/ClientRole.java b/model/src/main/java/org/openremote/model/security/ClientRole.java
index 1629438ab..e4583da8c 100644
--- a/model/src/main/java/org/openremote/model/security/ClientRole.java
+++ b/model/src/main/java/org/openremote/model/security/ClientRole.java
@@ -19,7 +19,6 @@
*/
package org.openremote.model.security;
-import com.fasterxml.jackson.annotation.JsonValue;
import org.keycloak.representations.idm.RoleRepresentation;
import org.openremote.model.Constants;
@@ -99,7 +98,6 @@ public enum ClientRole {
this.composites = null;
}
- @JsonValue
public String getValue() {
return value;
}
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index 8c4ca07f4..e3741e386 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -45,6 +45,7 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.stream.IntStream;
/**
* Utilities for working with values and JSON
@@ -198,9 +199,31 @@ public class Values {
if (coerce) {
try {
+ if (value instanceof List && type.isArray()) {
+ Class<?> innerType = type.getComponentType();
+ List list = ((List) value);
+
+ if (list.isEmpty()) {
+ return Optional.of((T)Array.newInstance(type.getComponentType(), 0));
+ }
+
+ Object[] arr = (Object[])Array.newInstance(type.getComponentType(), list.size());
+ IntStream.range(0, list.size()).forEach(i -> {
+ Object o = list.get(i);
+ if (o != null && !innerType.isAssignableFrom(o.getClass())) {
+ o = getValue(o, innerType, true).orElse(null);
+ }
+ arr[i] = o;
+ });
+ return Optional.of((T)arr);
+ }
+
return Optional.of(JSON.convertValue(value, type));
} catch (Exception e) {
if (value instanceof String) {
+
+ if (!((String) value).startsWith("\"")) {}
+
// Try and parse the value
return parse((String)value, type);
}