From 2032f760704e821e2b8ea2b450c30a7fd7ada341 Mon Sep 17 00:00:00 2001 From: pasindu Date: Sun, 7 Jan 2024 15:12:06 +0530 Subject: [PATCH] Fix dynamic client registration bugs --- .../api/service/impl/VirtualFireAlarmServiceImpl.java | 4 +++- .../input/adapter/mqtt/util/MQTTAdapterListener.java | 6 +++++- .../output/adapter/mqtt/util/MQTTAdapterPublisher.java | 7 ++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/components/device-types/virtual-fire-alarm-plugin/io.entgra.device.mgt.plugins.virtualfirealarm.api/src/main/java/io/entgra/device/mgt/plugins/virtualfirealarm/api/service/impl/VirtualFireAlarmServiceImpl.java b/components/device-types/virtual-fire-alarm-plugin/io.entgra.device.mgt.plugins.virtualfirealarm.api/src/main/java/io/entgra/device/mgt/plugins/virtualfirealarm/api/service/impl/VirtualFireAlarmServiceImpl.java index 5482bd119..890ab165c 100644 --- a/components/device-types/virtual-fire-alarm-plugin/io.entgra.device.mgt.plugins.virtualfirealarm.api/src/main/java/io/entgra/device/mgt/plugins/virtualfirealarm/api/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/device-types/virtual-fire-alarm-plugin/io.entgra.device.mgt.plugins.virtualfirealarm.api/src/main/java/io/entgra/device/mgt/plugins/virtualfirealarm/api/service/impl/VirtualFireAlarmServiceImpl.java @@ -221,6 +221,7 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { .getTenantDomain(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE}; + ArrayList supportedGrantTypes = new ArrayList<>(); try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantAdminDomainName); @@ -230,7 +231,8 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, - VirtualFireAlarmConstants.APIM_APPLICATION_TOKEN_VALIDITY_PERIOD, null); + VirtualFireAlarmConstants.APIM_APPLICATION_TOKEN_VALIDITY_PERIOD, null, null, + supportedGrantTypes, null, false); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/extensions/cdmf-transport-adapters/input/io.entgra.device.mgt.plugins.input.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/extensions/cdmf-transport-adapters/input/io.entgra.device.mgt.plugins.input.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/input/adapter/mqtt/util/MQTTAdapterListener.java index a4012f4ff..3d0f2ae8e 100644 --- a/components/extensions/cdmf-transport-adapters/input/io.entgra.device.mgt.plugins.input.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/extensions/cdmf-transport-adapters/input/io.entgra.device.mgt.plugins.input.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -43,7 +43,9 @@ import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClie import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; public class MQTTAdapterListener implements MqttCallback, Runnable { @@ -129,6 +131,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { String password = this.mqttBrokerConnectionConfiguration.getPassword(); String dcrUrlString = this.mqttBrokerConnectionConfiguration.getDcrUrl(); String scopes = this.mqttBrokerConnectionConfiguration.getBrokerScopes(); + List supportedGrantTypes = new ArrayList<>(); //getJWT Client Parameters. if (dcrUrlString != null && !dcrUrlString.isEmpty()) { try { @@ -136,7 +139,8 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { String applicationName = MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + mqttBrokerConnectionConfiguration.getAdapterName(); DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username, - "client_credentials", null, new String[]{"device_management"}, false, Integer.MAX_VALUE, password); + "client_credentials", null, new String[]{"device_management"}, false, Integer.MAX_VALUE, password, + supportedGrantTypes, dcrUrlString); String accessToken = getToken(dcrResponse.getClientId(), dcrResponse.getClientSecret()); connectionOptions.setUserName(accessToken.substring(0, 18)); connectionOptions.setPassword(accessToken.substring(19).toCharArray()); diff --git a/components/extensions/cdmf-transport-adapters/output/io.entgra.device.mgt.plugins.output.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/output/adapter/mqtt/util/MQTTAdapterPublisher.java b/components/extensions/cdmf-transport-adapters/output/io.entgra.device.mgt.plugins.output.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/output/adapter/mqtt/util/MQTTAdapterPublisher.java index 7bb32c010..f23e85302 100644 --- a/components/extensions/cdmf-transport-adapters/output/io.entgra.device.mgt.plugins.output.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/output/adapter/mqtt/util/MQTTAdapterPublisher.java +++ b/components/extensions/cdmf-transport-adapters/output/io.entgra.device.mgt.plugins.output.adapter.mqtt/src/main/java/io/entgra/device/mgt/plugins/output/adapter/mqtt/util/MQTTAdapterPublisher.java @@ -41,6 +41,9 @@ import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterExc import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterRuntimeException; import org.wso2.carbon.user.api.UserStoreException; +import java.util.ArrayList; +import java.util.List; + /** * MQTT publisher related configuration initialization and publishing capabilties are implemented here. */ @@ -142,6 +145,7 @@ public class MQTTAdapterPublisher { String username = this.mqttBrokerConnectionConfiguration.getUsername(); String password = this.mqttBrokerConnectionConfiguration.getPassword(); String dcrUrlString = this.mqttBrokerConnectionConfiguration.getDcrUrl(); + List supportedGrantTypes = new ArrayList<>(); if (dcrUrlString != null && !dcrUrlString.isEmpty()) { PrivilegedCarbonContext.startTenantFlow(); @@ -151,7 +155,8 @@ public class MQTTAdapterPublisher { String applicationName = MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + mqttBrokerConnectionConfiguration.getAdapterName(); DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username, - "client_credentials", null, new String[]{"device_management"}, false, Integer.MAX_VALUE, password); + "client_credentials", null, new String[]{"device_management"}, false, Integer.MAX_VALUE, password, + supportedGrantTypes, dcrUrlString); return getToken(dcrResponse.getClientId(), dcrResponse.getClientSecret()); // connectionOptions.setUserName(accessToken.substring(0, 18)); // connectionOptions.setPassword(accessToken.substring(19).toCharArray());