diff --git a/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java b/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java index b5c6b10d9..54c82f12b 100644 --- a/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java +++ b/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java @@ -179,7 +179,9 @@ public class ArduinoServiceImpl implements ArduinoService { @Produces("application/zip") public Response downloadSketch(@QueryParam("deviceName") String deviceName) { try { - ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName); + String username = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain(); + ZipArchive zipFile = createDownloadFile(username, deviceName); Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile())); response.status(Response.Status.OK); response.type("application/zip"); @@ -221,7 +223,8 @@ public class ArduinoServiceImpl implements ArduinoService { throw new DeviceManagementException(msg); } String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminUserName(); + .getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain();; if (apiApplicationKey == null) { APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {ArduinoConstants.DEVICE_TYPE}; diff --git a/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java b/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java index c0dec9973..f87439123 100644 --- a/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java +++ b/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java @@ -141,7 +141,9 @@ public class RaspberryPiServiceImpl implements RaspberryPiService { @Produces("application/zip") public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) { try { - ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType); + String username = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain(); + ZipArchive zipFile = createDownloadFile(username, deviceName, sketchType); Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile())); response.status(Response.Status.OK); response.type("application/zip"); @@ -206,7 +208,8 @@ public class RaspberryPiServiceImpl implements RaspberryPiService { } if (apiApplicationKey == null) { String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminUserName(); + .getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {RaspberrypiConstants.DEVICE_TYPE}; apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index b283bcb3d..fbebe12f9 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -172,7 +172,9 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) { try { - ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType); + String user = APIUtil.getAuthenticatedUser() + "@" + PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getTenantDomain(); + ZipArchive zipFile = createDownloadFile(user, deviceName, sketchType); Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile())); response.status(Response.Status.OK); response.type("application/zip"); @@ -242,7 +244,8 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { if (apiApplicationKey == null) { String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() - .getAdminUserName(); + .getAdminUserName() + "@" + PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getTenantDomain(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE}; apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( 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 b710a4aa5..dbfc79c9c 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 @@ -142,8 +142,16 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { registrationProfile.setGrantType(MQTTEventAdapterConstants.GRANT_TYPE); registrationProfile.setOwner(username); registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE); - registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX - + mqttBrokerConnectionConfiguration.getAdapterName() + "_" + tenantId); + if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) { + registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + + mqttBrokerConnectionConfiguration.getAdapterName() + + "_" + tenantId); + registrationProfile.setIsSaasApp(false); + } else { + registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + + mqttBrokerConnectionConfiguration.getAdapterName()); + registrationProfile.setIsSaasApp(true); + } String jsonString = registrationProfile.toJSON(); StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON); postMethod.setEntity(requestEntity); 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 d531aa038..fddc8e534 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 @@ -12,6 +12,7 @@ public class RegistrationProfile { private String owner; private String grantType; private String applicationType; + private boolean isSaasApp; private static final String TAG = RegistrationProfile.class.getSimpleName(); @@ -63,11 +64,19 @@ public class RegistrationProfile { this.applicationType = applicationType; } - public String toJSON() { + public boolean isSaasApp() { + return isSaasApp; + } + + public void setIsSaasApp(boolean isSaasApp) { + this.isSaasApp = isSaasApp; + } + + public String toJSON() { String jsonString = "{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " + "\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType + - "\", \"saasApp\" :true }\n"; + "\", \"saasApp\" :" + isSaasApp + " }\n"; return jsonString; } } \ No newline at end of file diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/pom.xml b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/pom.xml index 8c29a6cae..8842867d9 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/pom.xml +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/pom.xml @@ -33,6 +33,10 @@ http://wso2.org + + commons-codec.wso2 + commons-codec + org.wso2.carbon.analytics-common org.wso2.carbon.event.output.adapter.core @@ -61,6 +65,10 @@ com.googlecode.json-simple.wso2 json-simple + + org.wso2.carbon.devicemgt + org.wso2.carbon.identity.jwt.client.extension + @@ -121,7 +129,9 @@ org.apache.commons.codec.binary, org.apache.http.client.entity, org.apache.http.message, - org.apache.commons.ssl + org.apache.commons.ssl, + org.wso2.carbon.identity.jwt.client.extension.*, + org.wso2.carbon.user.api diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java index a87fddf58..fbc9c015d 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java @@ -122,7 +122,7 @@ public class MQTTEventAdapter implements OutputEventAdapter { String clientId = eventAdapterConfiguration.getStaticProperties().get( MQTTEventAdapterConstants.ADAPTER_CONF_CLIENTID); qos = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_QOS); - mqttAdapterPublisher = new MQTTAdapterPublisher(mqttBrokerConnectionConfiguration, clientId); + mqttAdapterPublisher = new MQTTAdapterPublisher(mqttBrokerConnectionConfiguration, clientId, tenantId); } @Override diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapterFactory.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapterFactory.java index 9275ec20d..1c8cd43de 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapterFactory.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapterFactory.java @@ -56,19 +56,20 @@ public class MQTTEventAdapterFactory extends OutputEventAdapterFactory { //Broker Username Property userName = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME); userName.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME)); - userName.setRequired(true); + userName.setRequired(false); userName.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME_HINT)); //Broker Password Property password = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD); password.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD)); - password.setRequired(true); + password.setRequired(false); password.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD_HINT)); //Broker Connection Scopes Property scopes = new Property(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES); scopes.setDisplayName(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES)); - scopes.setRequired(true); + scopes.setRequired(false); + scopes.setDefaultValue("default"); scopes.setHint(resourceBundle.getString(MQTTEventAdapterConstants.ADAPTER_CONF_SCOPES_HINT)); // set clientId diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/MQTTEventAdapterServiceComponent.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/MQTTEventAdapterServiceComponent.java index 80784a4ce..d7ff9d9ed 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/MQTTEventAdapterServiceComponent.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/MQTTEventAdapterServiceComponent.java @@ -22,10 +22,15 @@ import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.output.adapter.mqtt.MQTTEventAdapterFactory; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterFactory; - +import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; /** * @scr.component component.name="output.Mqtt.AdapterService.component" immediate="true" + * @scr.reference name="jwt.client.service" interface="org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService" + * cardinality="1..1" + * policy="dynamic" + * bind="setJWTClientManagerService" + * unbind="unsetJWTClientManagerService" */ public class MQTTEventAdapterServiceComponent { @@ -48,4 +53,12 @@ public class MQTTEventAdapterServiceComponent { } } + protected void setJWTClientManagerService(JWTClientManagerService jwtClientManagerService) { + OutputAdapterServiceDataHolder.setJwtClientManagerService(jwtClientManagerService); + } + + protected void unsetJWTClientManagerService(JWTClientManagerService jwtClientManagerService) { + OutputAdapterServiceDataHolder.setJwtClientManagerService(null); + } + } diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/OutputAdapterServiceDataHolder.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/OutputAdapterServiceDataHolder.java new file mode 100644 index 000000000..9cda29113 --- /dev/null +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/internal/OutputAdapterServiceDataHolder.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.wso2.carbon.device.mgt.output.adapter.mqtt.internal; + +import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; + +/** + * common place to hold some OSGI service references. + */ +public final class OutputAdapterServiceDataHolder { + + private static JWTClientManagerService jwtClientManagerService; + + public static JWTClientManagerService getJwtClientManagerService() { + return jwtClientManagerService; + } + + public static void setJwtClientManagerService( + JWTClientManagerService jwtClientManagerService) { + OutputAdapterServiceDataHolder.jwtClientManagerService = jwtClientManagerService; + } +} diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTAdapterPublisher.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTAdapterPublisher.java index 79188e840..38f078ff7 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTAdapterPublisher.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTAdapterPublisher.java @@ -21,14 +21,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.ssl.Base64; import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; -import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; -import org.apache.http.message.BasicNameValuePair; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; @@ -38,9 +35,14 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.output.adapter.mqtt.internal.OutputAdapterServiceDataHolder; import org.wso2.carbon.event.output.adapter.core.exception.ConnectionUnavailableException; import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterRuntimeException; +import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; +import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; +import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; +import org.wso2.carbon.user.api.UserStoreException; import java.io.IOException; import java.net.MalformedURLException; @@ -48,8 +50,6 @@ import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.List; /** * MQTT publisher related configuration initialization and publishing capabilties are implemented here. @@ -60,8 +60,11 @@ public class MQTTAdapterPublisher { private MqttClient mqttClient; private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration; String clientId; + int tenantId; - public MQTTAdapterPublisher(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String clientId) { + public MQTTAdapterPublisher(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String clientId + , int tenantId) { + this.tenantId = tenantId; if (clientId == null || clientId.trim().isEmpty()) { this.clientId = MqttClient.generateClientId(); } @@ -146,7 +149,6 @@ public class MQTTAdapterPublisher { String password = this.mqttBrokerConnectionConfiguration.getPassword(); String dcrUrlString = this.mqttBrokerConnectionConfiguration.getDcrUrl(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (dcrUrlString != null && !dcrUrlString.isEmpty()) { try { URL dcrUrl = new URL(dcrUrlString); @@ -157,8 +159,16 @@ public class MQTTAdapterPublisher { registrationProfile.setGrantType(MQTTEventAdapterConstants.GRANT_TYPE); registrationProfile.setOwner(username); registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE); - registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX - + mqttBrokerConnectionConfiguration.getAdapterName() + "_" + tenantId); + if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) { + registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + + mqttBrokerConnectionConfiguration.getAdapterName() + + "_" + tenantId); + registrationProfile.setIsSaasApp(false); + } else { + registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX + + mqttBrokerConnectionConfiguration.getAdapterName()); + registrationProfile.setIsSaasApp(true); + } String jsonString = registrationProfile.toJSON(); StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON); postMethod.setEntity(requestEntity); @@ -186,40 +196,34 @@ public class MQTTAdapterPublisher { throw new OutputEventAdapterRuntimeException("Invalid dcrUrl : " + dcrUrlString); } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | IOException e) { throw new OutputEventAdapterRuntimeException("Failed to create an https connection.", e); + } catch (JWTClientException | UserStoreException e) { + log.error("Failed to create an oauth token with jwt grant type.", e); } } throw new OutputEventAdapterRuntimeException("Invalid configuration for mqtt publisher"); } private String getToken(String clientId, String clientSecret) - throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, ParseException { - URL tokenEndpoint = new URL(mqttBrokerConnectionConfiguration.getTokenUrl()); - HttpClient httpClient = MQTTUtil.getHttpClient(tokenEndpoint.getProtocol()); - HttpPost postMethod = new HttpPost(tokenEndpoint.toString()); - - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.GRANT_TYPE_PARAM_NAME, - MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE)); - nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_USERNAME, - mqttBrokerConnectionConfiguration.getUsername())); - nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_PASSWORD, - mqttBrokerConnectionConfiguration.getPassword())); - String scopes = mqttBrokerConnectionConfiguration.getScopes(); - if (scopes != null && !scopes.isEmpty()) { - nameValuePairs.add(new BasicNameValuePair(MQTTEventAdapterConstants.PASSWORD_GRANT_TYPE_SCOPES, scopes)); - } + throws UserStoreException, JWTClientException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); + try { + String scopes = mqttBrokerConnectionConfiguration.getScopes(); + String username = mqttBrokerConnectionConfiguration.getUsername(); + if (mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) { + username = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUserRealm().getRealmConfiguration().getAdminUserName() + "@" + PrivilegedCarbonContext + .getThreadLocalCarbonContext().getTenantDomain(true); + } - postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs)); - postMethod.addHeader("Authorization", "Basic " + getBase64Encode(clientId, clientSecret)); - postMethod.addHeader("Content-Type", "application/x-www-form-urlencoded"); - HttpResponse httpResponse = httpClient.execute(postMethod); - String response = MQTTUtil.getResponseString(httpResponse); - if (log.isDebugEnabled()) { - log.debug(response); + JWTClientManagerService jwtClientManagerService = + OutputAdapterServiceDataHolder.getJwtClientManagerService(); + AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( + clientId, clientSecret, username, scopes); + return accessTokenInfo.getAccessToken(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); } - JSONParser jsonParser = new JSONParser(); - JSONObject jsonObject = (JSONObject) jsonParser.parse(response); - return (String) jsonObject.get(MQTTEventAdapterConstants.ACCESS_TOKEN_GRANT_TYPE_PARAM_NAME); } private String getBase64Encode(String key, String value) { diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java index b88d9d18b..2bbd394ab 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java @@ -33,6 +33,8 @@ public class MQTTBrokerConnectionConfiguration { private String tokenUrl; private boolean cleanSession = true; private int keepAlive; + private boolean globalCredentailSet; + public String getTokenUrl() { return tokenUrl; } @@ -69,11 +71,20 @@ public class MQTTBrokerConnectionConfiguration { return adapterName; } + public boolean isGlobalCredentailSet() { + return globalCredentailSet; + } + public MQTTBrokerConnectionConfiguration(OutputEventAdapterConfiguration eventAdapterConfiguration, Map globalProperties) { adapterName = eventAdapterConfiguration.getName(); this.username = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME); this.password = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD); + if ((username == null || username.isEmpty()) && (password == null || password.isEmpty())) { + username = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_USERNAME); + password = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_PASSWORD); + globalCredentailSet = true; + } String url = eventAdapterConfiguration .getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_CONF_URL); if (url == null || url.isEmpty()) { url = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_URL); diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTEventAdapterConstants.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTEventAdapterConstants.java index 732af141c..1f2bbdd38 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTEventAdapterConstants.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTEventAdapterConstants.java @@ -58,7 +58,7 @@ public final class MQTTEventAdapterConstants { public static final String DEFAULT_CALLBACK = ""; public static final String DEFAULT_PASSWORD = ""; - public static final String GRANT_TYPE = "password"; + public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer"; public static final String TOKEN_SCOPE = "production"; public static final String APPLICATION_NAME_PREFIX = "OutputAdapter_"; public static final String CLIENT_ID = "clientId"; diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/RegistrationProfile.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/RegistrationProfile.java index 0a4d93125..ef9249bd6 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/RegistrationProfile.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/RegistrationProfile.java @@ -12,6 +12,7 @@ public class RegistrationProfile { private String owner; private String grantType; private String applicationType; + private boolean isSaasApp; private static final String TAG = RegistrationProfile.class.getSimpleName(); @@ -63,11 +64,19 @@ public class RegistrationProfile { this.applicationType = applicationType; } - public String toJSON() { + public boolean isSaasApp() { + return isSaasApp; + } + + public void setIsSaasApp(boolean isSaasApp) { + this.isSaasApp = isSaasApp; + } + + public String toJSON() { String jsonString = "{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " + "\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType + - "\", \"saasApp\" :false }\n"; + "\", \"saasApp\" : " + isSaasApp + " }\n"; return jsonString; } } \ No newline at end of file diff --git a/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/pom.xml b/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/pom.xml index 8aed76cc9..ace48d60c 100644 --- a/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/pom.xml +++ b/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/pom.xml @@ -110,6 +110,28 @@ + + unpack-analytics + package + + unpack + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.analytics + ${project.version} + zip + true + + ${project.build.directory}/maven-shared-archive-resources/carbonapps + + **/* + + + + unpack-geo package diff --git a/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/src/main/resources/devicetypes/android_sense.xml b/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/src/main/resources/devicetypes/android_sense.xml index 33cb2a7d2..764aa311a 100644 --- a/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/src/main/resources/devicetypes/android_sense.xml +++ b/features/device-types-feature/androidsense-plugin-feature/org.wso2.carbon.device.mgt.iot.androidsense.feature/src/main/resources/devicetypes/android_sense.xml @@ -57,10 +57,7 @@ androidsense.mqtt.adapter - admin - admin 0 - true diff --git a/features/device-types-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml b/features/device-types-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml index f18237123..87fb81ed0 100644 --- a/features/device-types-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml +++ b/features/device-types-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml @@ -113,6 +113,28 @@ + + unpack-analytics + package + + unpack + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.analytics + ${project.version} + zip + true + + ${project.build.directory}/maven-shared-archive-resources/carbonapps + + **/* + + + + copy-jaxrs-war package diff --git a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml index aab3de96e..02541412d 100644 --- a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml +++ b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml @@ -133,6 +133,28 @@ + + unpack-analytics + package + + unpack + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.analytics + ${project.version} + zip + true + + ${project.build.directory}/maven-shared-archive-resources/carbonapps + + **/* + + + + diff --git a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/devicetypes/raspberrypi.xml b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/devicetypes/raspberrypi.xml index 60bbfa162..85db147c6 100644 --- a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/devicetypes/raspberrypi.xml +++ b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/devicetypes/raspberrypi.xml @@ -39,10 +39,7 @@ raspberrypi.mqtt.adapter - admin - admin 0 - true diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/pom.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/pom.xml index 2a6d5259b..0459e1eb1 100644 --- a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/pom.xml +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/pom.xml @@ -162,6 +162,28 @@ + + unpack-analytics + package + + unpack + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.analytics + ${project.version} + zip + true + + ${project.build.directory}/maven-shared-archive-resources/carbonapps + + **/* + + + + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual_firealarm.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual_firealarm.xml deleted file mode 100644 index b4a9a64a9..000000000 --- a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual_firealarm.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - MQTT - - virtualfirealarm.mqtt.adapter - admin - admin - 0 - - true - - - - - diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/devicetypes/virtual_firealarm.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/devicetypes/virtual_firealarm.xml index e88e4f03d..1fcfec4c6 100644 --- a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/devicetypes/virtual_firealarm.xml +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/devicetypes/virtual_firealarm.xml @@ -31,7 +31,7 @@ - false + true @@ -39,10 +39,7 @@ virtualfirealarm.mqtt.adapter - admin - admin 0 - true diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf index c09bb3b0e..9ebe160d9 100644 --- a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf @@ -20,7 +20,6 @@ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../featur org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../resources/device-types/);\ org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../deployment/server/devicetypes/);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm_${feature.version}/devicetypes/,target:${installFolder}/../../deployment/server/devicetypes/,overwrite:true);\ -org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../conf/etc/device-mgt-plugins/);\ org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../deployment/server/carbonapps/);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm_${feature.version}/carbonapps/,target:${installFolder}/../../resources/devicetypes/virtual_firealarm/,overwrite:true);\