From e0f6e9579d5856609dd9c062c4d1289cf5e35d3a Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 2 Feb 2017 03:17:19 +0530 Subject: [PATCH] fixed multi tenant issues after testing --- .../pom.xml | 4 +++ .../input/adapter/mqtt/MQTTEventAdapter.java | 1 - .../mqtt/util/MQTTAdapterListener.java | 29 ++++++++++++------- .../mqtt/util/MQTTEventAdapterConstants.java | 2 +- .../mqtt/util/RegistrationProfile.java | 2 +- pom.xml | 7 ++++- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/pom.xml b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/pom.xml index 30e7afbbd..634fc4647 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/pom.xml +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/pom.xml @@ -32,6 +32,10 @@ http://wso2.org + + commons-codec.wso2 + commons-codec + org.wso2.carbon.analytics-common org.wso2.carbon.event.input.adapter.core diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java index 767153902..f6b80dc79 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java @@ -42,7 +42,6 @@ public class MQTTEventAdapter implements InputEventAdapter { private MQTTAdapterListener mqttAdapterListener; private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration; - public MQTTEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration, Map globalProperties) { this.eventAdapterConfiguration = eventAdapterConfiguration; diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java index 802295862..b710a4aa5 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -267,18 +267,25 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { private String getToken(String clientId, String clientSecret) throws UserStoreException, JWTClientException { - String scopes = mqttBrokerConnectionConfiguration.getBrokerScopes(); - String username = mqttBrokerConnectionConfiguration.getUsername(); - if (mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) { - username = PrivilegedCarbonContext.getThreadLocalCarbonContext() - .getUserRealm().getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext - .getThreadLocalCarbonContext().getTenantDomain(true); - } + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); + try { + String scopes = mqttBrokerConnectionConfiguration.getBrokerScopes(); + String username = mqttBrokerConnectionConfiguration.getUsername(); + if (mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) { + username = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUserRealm().getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain(true); + } - JWTClientManagerService jwtClientManagerService = InputAdapterServiceDataHolder.getJwtClientManagerService(); - AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( - clientId, clientSecret, username, scopes); - return accessTokenInfo.getAccessToken(); + JWTClientManagerService jwtClientManagerService = + InputAdapterServiceDataHolder.getJwtClientManagerService(); + AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( + clientId, clientSecret, username, scopes); + return accessTokenInfo.getAccessToken(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } } private String getBase64Encode(String key, String value) { diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTEventAdapterConstants.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTEventAdapterConstants.java index 657ac99eb..dc0e45413 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTEventAdapterConstants.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTEventAdapterConstants.java @@ -53,7 +53,7 @@ public class MQTTEventAdapterConstants { public static final String EMPTY_STRING = ""; public static final String GRANT_TYPE_PARAM_NAME = "grant_type"; - public static final String GRANT_TYPE = "password refresh_token"; + public static final String GRANT_TYPE = "password refresh_token urn:ietf:params:oauth:grant-type:jwt-bearer"; public static final String TOKEN_SCOPE = "production"; public static final String APPLICATION_NAME_PREFIX = "InputAdapter_"; public static final String CLIENT_ID = "clientId"; diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/RegistrationProfile.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/RegistrationProfile.java index f5445f170..d531aa038 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/RegistrationProfile.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/RegistrationProfile.java @@ -67,7 +67,7 @@ public class RegistrationProfile { String jsonString = "{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " + "\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType + - "\", \"saasApp\" :false }\n"; + "\", \"saasApp\" :true }\n"; return jsonString; } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 27e371288..04c05eca3 100644 --- a/pom.xml +++ b/pom.xml @@ -793,6 +793,11 @@ + + commons-codec.wso2 + commons-codec + ${commons-codec.version.wso2} + commons-codec commons-codec @@ -1179,7 +1184,7 @@ 3.1.0.wso2v2 - + 1.4.0.wso2v1 1.7 6.0