From 549186eda2450b2f323f67a9d349906a710971ee Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Mon, 27 Jan 2020 21:33:47 +0530 Subject: [PATCH] Add API to get enrollment QR code payload --- .../api/DeviceTypeConfigurationAPI.java | 40 +++ .../api/impl/DeviceManagementAPIImpl.java | 62 ++--- .../impl/DeviceTypeConfigurationAPIImpl.java | 35 +++ .../api/impl/EventReceiverAPIImpl.java | 22 +- .../android/common/AndroidConstants.java | 26 ++ .../AndroidDeviceMgtPluginException.java | 3 +- .../pom.xml | 1 + .../impl/DeviceTypeCommonServiceImpl.java | 238 +++++++++++++++--- .../AndroidDeviceManagementDataHolder.java | 11 + .../AndroidPluginServiceComponent.java | 7 + .../android/core/util/AndroidAPIUtils.java | 13 + 11 files changed, 375 insertions(+), 83 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/DeviceTypeConfigurationAPI.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/DeviceTypeConfigurationAPI.java index e3cce4634..537444a90 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/DeviceTypeConfigurationAPI.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/DeviceTypeConfigurationAPI.java @@ -246,4 +246,44 @@ public interface DeviceTypeConfigurationAPI { "Example: Mon, 05 Jan 2014 15:10:00 +0200.") @HeaderParam("If-Modified-Since") String ifModifiedSince); + @GET + @Path("/enrollment-qr-config/{ownershipType}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting the Enrollment QR config for device type Android", + notes = "Use this REST API to retrieve the Enrollment QR Config that is used for the Android device " + + "enrollment.", + tags = "Android Configuration Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = AndroidConstants.SCOPE, value = "perm:android:enroll") + }) + } + ) + @ApiResponses(value = { + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request."), + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched Android Enrollment QR configuration.", + response = String.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "Content type of the body") + }), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the Android Enrollment QR" + + " configuration.") + }) + Response getQRConfig( + @ApiParam( + name = "ownershipType", + value = "The ownership type that device has enrolled.", + required = true) + @PathParam("ownershipType") String ownershipType); } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java index 68d94fa64..479641ff3 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java @@ -142,18 +142,18 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { String msg = "Device identifier is invalid. Device identifier " + id; log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (DeviceManagementException e) { - String msg = "Error occurred while getting pending operations of the device."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) - .setMessage(msg).build()).build(); } catch (AndroidDeviceMgtPluginException e) { String errorMessage = "Error occured while executing get pending operations"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) .setMessage(errorMessage).build()).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting pending operations of the device."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()).build(); } } @@ -198,19 +198,19 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { } return Response.status(Integer.parseInt(message.getResponseCode())) .entity(message.getResponseMessage()).build(); - } catch (DeviceManagementException e) { - String msg = "Error occurred while enrolling the android, which carries the id '" + - androidDevice.getDeviceIdentifier() + "'"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) - .setMessage(msg).build()).build(); } catch (AndroidDeviceMgtPluginException e) { String errorMessage = "Error occured while enrolling device"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) .setMessage(errorMessage).build()).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while enrolling the android, which carries the id '" + androidDevice + .getDeviceIdentifier() + "'"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()).build(); } } @@ -251,31 +251,31 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { "carries the id '" + id + "' has not been updated"); return Response.status(Response.Status.NOT_MODIFIED).entity(responseMessage).build(); } - } catch (DeviceManagementException e) { - String msg = "Error occurred while modifying enrollment of the Android device that carries the id '" + - id + "'"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) - .setMessage(msg).build()).build(); - } catch (BadRequestException e){ + } catch (BadRequestException e) { String errorMessage = "The payload of the android device enrollment is incorrect."; log.error(errorMessage, e); - return Response.status(Response.Status.BAD_REQUEST).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_BAD_REQUEST) + return Response.status(Response.Status.BAD_REQUEST) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_BAD_REQUEST) .setMessage(errorMessage).build()).build(); } catch (NotFoundException e) { String errorMessage = "The device to be modified doesn't exist."; log.error(errorMessage, e); - return Response.status(Response.Status.NOT_FOUND).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND) + return Response.status(Response.Status.NOT_FOUND) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND) .setMessage(errorMessage).build()).build(); } catch (AndroidDeviceMgtPluginException e) { String errorMessage = "Error occured while modifying enrollment"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) .setMessage(errorMessage).build()).build(); + } catch (DeviceManagementException e) { + String msg = + "Error occurred while modifying enrollment of the Android device that carries the id '" + id + "'"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()).build(); } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceTypeConfigurationAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceTypeConfigurationAPIImpl.java index 86cf58963..c22dfa5c8 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceTypeConfigurationAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceTypeConfigurationAPIImpl.java @@ -39,7 +39,9 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException; import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.spi.DeviceTypeCommonService; import org.wso2.carbon.device.mgt.mobile.android.api.DeviceTypeConfigurationAPI; import org.wso2.carbon.device.mgt.mobile.android.common.bean.AndroidPlatformConfiguration; import org.wso2.carbon.device.mgt.mobile.android.common.bean.ErrorResponse; @@ -54,9 +56,11 @@ import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.Map; @Path("/configuration") @Produces(MediaType.APPLICATION_JSON) @@ -125,4 +129,35 @@ public class DeviceTypeConfigurationAPIImpl implements DeviceTypeConfigurationAP .setMessage(msg).build()).build(); } } + + @GET + @Path("/enrollment-qr-config/{ownershipType}") + @Produces(MediaType.APPLICATION_JSON) + public Response getQRConfig( + @PathParam("ownershipType") String ownershipType) { + try { + DeviceTypeCommonService deviceTypeCommonService = AndroidAPIUtils.getDeviceTypeCommonService(); + Map enrollmentQRConfig = deviceTypeCommonService.getEnrollmentQRCode(ownershipType); + if (enrollmentQRConfig.isEmpty()) { + String msg = "Couldn't find Enrollment QR code config for Android. Please contact administrator."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(enrollmentQRConfig).build(); + } catch (BadRequestException e) { + String msg = "Bad Request, trying to get Enrollment QR code for invalid device ownership type " + + ownershipType; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while retrieving the license configured for Android device enrolment"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (InvalidConfigurationException e) { + String msg = "Platform configuration is not configured properly to generate QR code for the Android " + + "enrollment. Device ownership mode is " + ownershipType; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java index fa60ca476..645249fcd 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/EventReceiverAPIImpl.java @@ -54,23 +54,23 @@ public class EventReceiverAPIImpl implements EventReceiverAPI { AndroidService androidService = AndroidAPIUtils.getAndroidService(); Message message = androidService.publishEvents(eventBeanWrapper); return Response.status(Integer.parseInt(message.getResponseCode())).entity(message.getResponseMessage()).build(); - } catch (DeviceManagementException e) { - String errorMessage = "Error occurred while checking Operation Analytics is Enabled."; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) - .setMessage(errorMessage).build()).build(); } catch (UnexpectedServerErrorException e) { String errorMessage = "Error occurred while getting the Data publisher Service instance."; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) .setMessage(errorMessage).build()).build(); - } catch (AndroidDeviceMgtPluginException e){ + } catch (AndroidDeviceMgtPluginException e) { String errorMessage = "Error occurred while publishing events."; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(errorMessage).build()).build(); + } catch (DeviceManagementException e) { + String errorMessage = "Error occurred while checking Operation Analytics is Enabled."; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) .setMessage(errorMessage).build()).build(); } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java index d96c8d0e5..be48dbe62 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java @@ -52,6 +52,8 @@ package org.wso2.carbon.device.mgt.mobile.android.common; +import com.sun.org.apache.bcel.internal.generic.ATHROW; + /** * Defines constants used in Android-REST API bundle. */ @@ -227,4 +229,28 @@ public final class AndroidConstants { } + public final class PlatformConfigs { + private PlatformConfigs() { throw new AssertionError(); } + + public final class KioskConfigs { + private KioskConfigs() { throw new AssertionError(); } + + public static final String ADMIN_COMPONENT_NAME = "android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME"; + public static final String WIFI_SSID = "android.app.extra.PROVISIONING_WIFI_SSID"; + public static final String WIFI_PASSWORD = "android.app.extra.PROVISIONING_WIFI_PASSWORD"; + public static final String WIFI_SECURITY = "android.app.extra.PROVISIONING_WIFI_SECURITY_TYPE"; + public static final String SKIP_ENCRYPTION = "android.app.extra.PROVISIONING_SKIP_ENCRYPTION"; + public static final String CHECKSUM = "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM"; + public static final String DOWNLOAD_URL = "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION"; + public static final String ANDROID_EXTRA = "android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE"; + } + + public final class DefaultConfigs { + private DefaultConfigs() { throw new AssertionError(); } + + public static final String ACCESS_TOKEN = "android.app.extra.token"; + public static final String DEFAULT_OWNERSHIP = "android.app.extra.defaultOwner"; + public static final String SERVER_IP = "android.app.extra.serverIp"; + } + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/AndroidDeviceMgtPluginException.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/AndroidDeviceMgtPluginException.java index 117c42905..d73a1e8cc 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/AndroidDeviceMgtPluginException.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/AndroidDeviceMgtPluginException.java @@ -17,8 +17,9 @@ */ package org.wso2.carbon.device.mgt.mobile.android.common.exception; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; -public class AndroidDeviceMgtPluginException extends Exception{ +public class AndroidDeviceMgtPluginException extends DeviceManagementException { private static final long serialVersionUID = -2297311387874900305L; private String errorMessage; diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml index 8d07d4532..e4f4e22e6 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/pom.xml @@ -85,6 +85,7 @@ org.wso2.carbon.device.application.mgt.common.*, org.wso2.carbon.user.core.*, org.wso2.carbon.user.api.*, + org.wso2.carbon.identity.jwt.client.extension.*, org.apache.commons.httpclient, org.apache.http, org.apache.http.client.methods, diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/DeviceTypeCommonServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/DeviceTypeCommonServiceImpl.java index 24aac46fd..03726cbf6 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/DeviceTypeCommonServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/DeviceTypeCommonServiceImpl.java @@ -17,59 +17,217 @@ package org.wso2.carbon.device.mgt.mobile.android.core.impl; -import org.wso2.carbon.base.ServerConfiguration; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; +import org.wso2.carbon.device.mgt.common.ApplicationRegistrationException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.common.MDMAppConstants; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeCommonService; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.config.keymanager.KeyManagerConfigurations; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.mobile.android.common.AndroidConstants; +import org.wso2.carbon.device.mgt.mobile.android.common.exception.BadRequestException; +import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidService; import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidAPIUtils; +import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; +import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.HashMap; import java.util.Map; public class DeviceTypeCommonServiceImpl implements DeviceTypeCommonService { + private static final Log log = LogFactory.getLog(DeviceTypeCommonServiceImpl.class); + @Override - public Map getEnrollmentQRCode() throws DeviceManagementException { - return null; + public Map getEnrollmentQRCode(String ownershipType) throws DeviceManagementException { + AndroidService androidService = AndroidAPIUtils.getAndroidService(); + PlatformConfiguration platformConfiguration = androidService.getPlatformConfig(); + + Map qrEnrollmentPayload = new HashMap<>(); + Map defaultQREnrollmentPayload = new HashMap<>(); + + if (Arrays.stream(EnrolmentInfo.OwnerShip.values()) + .noneMatch(ownerShip -> ownerShip.toString().equalsIgnoreCase(ownershipType))) { + String msg = "Request to get QR enrollment code for invalid device ownership type " + ownershipType; + log.error(msg); + throw new BadRequestException(msg); + } + + String accessToken = getAccessTokenToEnroll(); + if (StringUtils.isBlank(accessToken)) { + String msg = "Couldn't get a access token for user " + PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getUsername(); + log.error(msg); + throw new DeviceManagementException(msg); + } + + for (ConfigurationEntry configEntry : platformConfiguration.getConfiguration()) { + if (AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP.equals(configEntry.getName())) { + defaultQREnrollmentPayload + .put(AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP, configEntry.getValue()); + } + if (AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP.equals(configEntry.getName())) { + defaultQREnrollmentPayload + .put(AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP, configEntry.getValue()); + } + + if (EnrolmentInfo.OwnerShip.COSU.toString().equalsIgnoreCase(ownershipType) || EnrolmentInfo.OwnerShip.COPE + .toString().equalsIgnoreCase(ownershipType)) { + if (AndroidConstants.PlatformConfigs.KioskConfigs.ADMIN_COMPONENT_NAME.equals(configEntry.getName())) { + qrEnrollmentPayload.put(AndroidConstants.PlatformConfigs.KioskConfigs.ADMIN_COMPONENT_NAME, + configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SSID.equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SSID, configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_PASSWORD.equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_PASSWORD, configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SECURITY.equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SECURITY, configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.CHECKSUM.equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.CHECKSUM, configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.DOWNLOAD_URL.equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.DOWNLOAD_URL, configEntry.getValue()); + } else if (AndroidConstants.PlatformConfigs.KioskConfigs.SKIP_ENCRYPTION + .equals(configEntry.getName())) { + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.SKIP_ENCRYPTION, configEntry.getValue()); + } + } + + } + + defaultQREnrollmentPayload.put(AndroidConstants.PlatformConfigs.DefaultConfigs.ACCESS_TOKEN, accessToken); + qrEnrollmentPayload + .put(AndroidConstants.PlatformConfigs.KioskConfigs.ANDROID_EXTRA, defaultQREnrollmentPayload); + validateQREnrollmentPayload(qrEnrollmentPayload, ownershipType); + return qrEnrollmentPayload; } -// private void getPlatformConfig() { -// PlatformConfiguration platformConfiguration; -// List configs; -// try { -// platformConfiguration = AndroidAPIUtils.getDeviceManagementService(). -// getConfiguration(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); -// if (platformConfiguration != null) { -// configs = platformConfiguration.getConfiguration(); -// } else { -// platformConfiguration = new PlatformConfiguration(); -// configs = new ArrayList<>(); -// } -// ConfigurationEntry entry = new ConfigurationEntry(); -// License license = AndroidAPIUtils.getDeviceManagementService().getLicense( -// DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID, MDMAppConstants.AndroidConstants. -// TenantConfigProperties.LANGUAGE_US); -// -// if (configs != null) { -// ConfigurationEntry versionEntry = new ConfigurationEntry(); -// versionEntry.setContentType(AndroidConstants.TenantConfigProperties.CONTENT_TYPE_TEXT); -// versionEntry.setName(AndroidConstants.TenantConfigProperties.SERVER_VERSION); -// versionEntry.setValue(ServerConfiguration.getInstance().getFirstProperty("Version")); -// configs.add(versionEntry); -// if (license != null) { -// entry.setContentType(AndroidConstants.TenantConfigProperties.CONTENT_TYPE_TEXT); -// entry.setName(AndroidConstants.TenantConfigProperties.LICENSE_KEY); -// entry.setValue(license.getText()); -// configs.add(entry); -// } -// platformConfiguration.setConfiguration(configs); -// } -// } + /** + * To get Access token for device enroll scope. + * + * @return Access token + * @throws DeviceManagementException if error occurred when trying to get access token for device enroll scope. + */ + private String getAccessTokenToEnroll() throws DeviceManagementException { + DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance() + .getDeviceManagementConfig(); + KeyManagerConfigurations kmConfig = deviceManagementConfig.getKeyManagerConfigurations(); + try { + AppRegistrationCredentials credentials = DeviceManagerUtil.getApplicationRegistrationCredentials( + System.getProperty( + org.wso2.carbon.device.mgt.core.DeviceManagementConstants.ConfigurationManagement.IOT_GATEWAY_HOST), + System.getProperty( + org.wso2.carbon.device.mgt.core.DeviceManagementConstants.ConfigurationManagement.IOT_GATEWAY_HTTPS_PORT), + kmConfig.getAdminUsername(), kmConfig.getAdminPassword()); + + AccessTokenInfo accessTokenForAdmin = DeviceManagerUtil + .getAccessTokenForDeviceOwner("perm:device:enroll", credentials.getClient_id(), + credentials.getClient_secret(), + PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); + return accessTokenForAdmin.getAccessToken(); + } catch (ApplicationRegistrationException e) { + String msg = "Error occurred while registering Application to get access token to create enrollment QR code " + + "payload."; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (JWTClientException e) { + String msg = "JWT Error occurred while registering Application to get access token to create enrollment " + + "QR code payload."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + } + + /** + * Validate the QR enrollment payload + * + * @param payload Configs use to generate Enrollment QR code + * @param ownershipType Device enrollment type + */ + private void validateQREnrollmentPayload(Map payload, + String ownershipType) { + boolean isInvalidConfig = false; + String invalidPlatformConfigs = ""; + + if (payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.ANDROID_EXTRA) == null) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.ANDROID_EXTRA + ", "; + isInvalidConfig = true; + } else { + Map defaultPayload = (Map) payload + .get(AndroidConstants.PlatformConfigs.KioskConfigs.ANDROID_EXTRA); + if (StringUtils.isBlank( + (String) defaultPayload.get(AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP + ", "; + isInvalidConfig = true; + } + if (StringUtils + .isBlank((String) defaultPayload.get(AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP + ", "; + isInvalidConfig = true; + } + if (StringUtils.isBlank( + (String) defaultPayload.get(AndroidConstants.PlatformConfigs.DefaultConfigs.ACCESS_TOKEN))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.DefaultConfigs.ACCESS_TOKEN + ", "; + isInvalidConfig = true; + } + } + + if (EnrolmentInfo.OwnerShip.COSU.toString().equalsIgnoreCase(ownershipType) || EnrolmentInfo.OwnerShip.COPE + .toString().equalsIgnoreCase(ownershipType)) { + if (StringUtils.isBlank( + (String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.ADMIN_COMPONENT_NAME))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.ADMIN_COMPONENT_NAME + ", "; + isInvalidConfig = true; + } + if (StringUtils.isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SSID))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SSID + ", "; + isInvalidConfig = true; + } + if (StringUtils + .isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_PASSWORD))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_PASSWORD + ", "; + isInvalidConfig = true; + } + if (StringUtils + .isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SECURITY))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.WIFI_SECURITY + ", "; + isInvalidConfig = true; + } + if (StringUtils.isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.CHECKSUM))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.CHECKSUM + ", "; + isInvalidConfig = true; + } + if (StringUtils.isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.DOWNLOAD_URL))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.DOWNLOAD_URL + ", "; + isInvalidConfig = true; + } + if (StringUtils + .isBlank((String) payload.get(AndroidConstants.PlatformConfigs.KioskConfigs.SKIP_ENCRYPTION))) { + invalidPlatformConfigs += AndroidConstants.PlatformConfigs.KioskConfigs.SKIP_ENCRYPTION + ", "; + isInvalidConfig = true; + } + } + + if (isInvalidConfig) { + String msg = "Android Platform Configuration is not configured properly. Platform configs [ " + + invalidPlatformConfigs + " ]"; + log.error(msg); + throw new InvalidConfigurationException(msg); + } + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidDeviceManagementDataHolder.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidDeviceManagementDataHolder.java index 9f4740313..42466d158 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidDeviceManagementDataHolder.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidDeviceManagementDataHolder.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.android.core.internal; +import org.wso2.carbon.device.mgt.common.spi.DeviceTypeCommonService; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidGoogleEnterpriseService; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -31,6 +32,8 @@ public class AndroidDeviceManagementDataHolder { private AndroidGoogleEnterpriseService androidDeviceManagementService; private AndroidService androidService; + private DeviceTypeCommonService deviceTypeCommonService; + private static AndroidDeviceManagementDataHolder thisInstance = new AndroidDeviceManagementDataHolder(); private AndroidDeviceManagementDataHolder() { @@ -64,4 +67,12 @@ public class AndroidDeviceManagementDataHolder { public void setAndroidService(AndroidService androidService) { this.androidService = androidService; } + + public DeviceTypeCommonService getDeviceTypeCommonService() { + return deviceTypeCommonService; + } + + public void setDeviceTypeCommonService(DeviceTypeCommonService deviceTypeCommonService) { + this.deviceTypeCommonService = deviceTypeCommonService; + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidPluginServiceComponent.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidPluginServiceComponent.java index 0465449d2..69294d958 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidPluginServiceComponent.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/internal/AndroidPluginServiceComponent.java @@ -22,11 +22,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.common.spi.DeviceTypeCommonService; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidGoogleEnterpriseService; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidService; import org.wso2.carbon.device.mgt.mobile.android.core.dao.AbstractMobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.android.core.impl.AndroidGoogleEnterpriseServiceImpl; import org.wso2.carbon.device.mgt.mobile.android.core.impl.AndroidServiceImpl; +import org.wso2.carbon.device.mgt.mobile.android.core.impl.DeviceTypeCommonServiceImpl; import org.wso2.carbon.device.mgt.mobile.android.core.util.MobileDeviceManagementUtil; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -69,6 +71,11 @@ public class AndroidPluginServiceComponent { androidService); bundleContext.registerService(AndroidService.class.getName(), androidService, null); + DeviceTypeCommonService deviceTypeCommonService = new DeviceTypeCommonServiceImpl(); + AndroidDeviceManagementDataHolder.getInstance().setDeviceTypeCommonService( + deviceTypeCommonService); + bundleContext.registerService(DeviceTypeCommonService.class.getName(), deviceTypeCommonService, null); + AndroidGoogleEnterpriseService androidPluginService = new AndroidGoogleEnterpriseServiceImpl(); AndroidDeviceManagementDataHolder.getInstance().setAndroidDeviceManagementService( androidPluginService); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/AndroidAPIUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/AndroidAPIUtils.java index dae74b5f5..ad5f0bc64 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/AndroidAPIUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/AndroidAPIUtils.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.spi.DeviceTypeCommonService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -101,6 +102,18 @@ public class AndroidAPIUtils { return deviceManagementProviderService; } + public static DeviceTypeCommonService getDeviceTypeCommonService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceTypeCommonService deviceTypeCommonService = (DeviceTypeCommonService) ctx + .getOSGiService(DeviceTypeCommonService.class, null); + if (deviceTypeCommonService == null) { + String msg = "Device Type Common service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceTypeCommonService; + } + public static DeviceInformationManager getDeviceInformationManagerService() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); DeviceInformationManager informationManager =