100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fixed race condition with MQTT clients which immediately do an subscribe after receiving Connect ACK. The EventInterceptHandler doesn't try to get an access token(which took too much time) but leaves it to KeycloakAuthorizatorPolicy to try for the access token.
Michael Rademaker5ea0d91118de
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.javaindex d6323152f..5ff81665a 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java@@ -89,11 +89,7 @@ public class EventInterceptHandler extends AbstractInterceptHandler {@Overridepublic void onConnect(InterceptConnectMessage interceptConnectMessage) {-MqttConnection connection = new MqttConnection(interceptConnectMessage.getClientID(), interceptConnectMessage.getUsername(), interceptConnectMessage.getPassword());- String suppliedClientSecret = new String(interceptConnectMessage.getPassword(), StandardCharsets.UTF_8);- connection.accessToken = identityProvider.getExternalKeycloak().getAccessToken(connection.realm, new ClientCredentialsAuthForm(connection.username, suppliedClientSecret)).getToken();-mqttConnectionMap.put(connection.clientId, connection);Map<String, Object> headers = prepareHeaders(connection);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.javaindex 1a179a20a..41f5a7dfd 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java@@ -5,7 +5,6 @@ import io.moquette.broker.subscriptions.Token;import io.moquette.broker.subscriptions.Topic;import org.keycloak.adapters.rotation.AdapterTokenVerifier;import org.keycloak.common.VerificationException;-import org.keycloak.exceptions.TokenNotActiveException;import org.keycloak.representations.AccessToken;import org.openremote.container.security.AuthContext;import org.openremote.container.security.ClientCredentialsAuthForm;@@ -26,7 +25,8 @@ import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;-import static org.openremote.manager.mqtt.MqttBrokerService.*;+import static org.openremote.manager.mqtt.MqttBrokerService.ASSETS_TOPIC;+import static org.openremote.manager.mqtt.MqttBrokerService.ASSET_ATTRIBUTE_VALUE_TOPIC;import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {@@ -98,21 +98,16 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {}}- AccessToken accessToken = null;+ AccessToken accessToken;try {accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));} catch (VerificationException e) {- if (e instanceof TokenNotActiveException) {- String suppliedClientSecret = new String(connection.password, StandardCharsets.UTF_8);- connection.accessToken = identityProvider.getExternalKeycloak().getAccessToken(connection.realm, new ClientCredentialsAuthForm(connection.username, suppliedClientSecret)).getToken();- try {- accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));- } catch (VerificationException verificationException) {- LOG.log(Level.INFO, "Couldn't verify token", verificationException);- return false;- }- } else {- LOG.log(Level.INFO, "Couldn't verify token", e);+ String suppliedClientSecret = new String(connection.password, StandardCharsets.UTF_8);+ connection.accessToken = identityProvider.getExternalKeycloak().getAccessToken(connection.realm, new ClientCredentialsAuthForm(connection.username, suppliedClientSecret)).getToken();+ try {+ accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));+ } catch (VerificationException verificationException) {+ LOG.log(Level.INFO, "Couldn't verify token", verificationException);return false;}}