From 742081c94e898bf9124845c2465ee225910bee18 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Tue, 5 Nov 2019 16:06:09 +0530 Subject: [PATCH 1/3] insert user improvement for Google EMM --- .../impl/AndroidEnterpriseServiceImpl.java | 78 ++++++++++--------- .../impl/DeviceManagementServiceImpl.java | 40 +++++++++- 2 files changed, 81 insertions(+), 37 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/AndroidEnterpriseServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/AndroidEnterpriseServiceImpl.java index fd735c35c..26805efd7 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/AndroidEnterpriseServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/AndroidEnterpriseServiceImpl.java @@ -99,43 +99,9 @@ public class AndroidEnterpriseServiceImpl implements AndroidEnterpriseService { .build(); } - EnterpriseConfigs enterpriseConfigs = AndroidEnterpriseUtils.getEnterpriseConfigs(); String token; - boolean deviceIdExist = false; - try { - String googleUserId; - List androidEnterpriseUsers = AndroidAPIUtils.getAndroidPluginService() - .getEnterpriseUser(CarbonContext.getThreadLocalCarbonContext().getUsername()); - GoogleAPIInvoker googleAPIInvoker = new GoogleAPIInvoker(enterpriseConfigs.getEsa()); - if (androidEnterpriseUsers != null && androidEnterpriseUsers.size() > 0) { - googleUserId = androidEnterpriseUsers.get(0).getGoogleUserId(); - // If this device is also present, only need to provide a token for this request. - for (AndroidEnterpriseUser enterprise : androidEnterpriseUsers) { - if (enterprise.getEmmDeviceId() != null - && enterprise.getEmmDeviceId().equals(enterpriseUser.getAndroidPlayDeviceId())) { - deviceIdExist = true; - } - } - } else { - googleUserId = googleAPIInvoker.insertUser(enterpriseConfigs.getEnterpriseId(), CarbonContext - .getThreadLocalCarbonContext() - .getUsername()); - } - // Fetching an auth token from Google EMM API - token = googleAPIInvoker.getToken(enterpriseConfigs.getEnterpriseId(), googleUserId); - - if (!deviceIdExist) { - AndroidEnterpriseUser androidEnterpriseUser = new AndroidEnterpriseUser(); - androidEnterpriseUser.setEmmUsername(CarbonContext.getThreadLocalCarbonContext().getUsername()); - androidEnterpriseUser.setTenantId(CarbonContext.getThreadLocalCarbonContext().getTenantId()); - androidEnterpriseUser.setAndroidPlayDeviceId(enterpriseUser.getAndroidPlayDeviceId()); - androidEnterpriseUser.setEnterpriseId(enterpriseConfigs.getEnterpriseId()); - androidEnterpriseUser.setEmmDeviceId(enterpriseUser.getEmmDeviceIdentifier()); - androidEnterpriseUser.setGoogleUserId(googleUserId); - - AndroidAPIUtils.getAndroidPluginService().addEnterpriseUser(androidEnterpriseUser); - } + token = insertUser(enterpriseUser); if (token == null) { return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage("Error when fetching token").build()) @@ -149,6 +115,48 @@ public class AndroidEnterpriseServiceImpl implements AndroidEnterpriseService { return Response.status(Response.Status.OK).entity(token).build(); } + public String insertUser(EnterpriseUser enterpriseUser) throws EnterpriseServiceException { + EnterpriseConfigs enterpriseConfigs = AndroidEnterpriseUtils.getEnterpriseConfigs(); + String token; + boolean deviceIdExist = false; + + String googleUserId; + List androidEnterpriseUsers = AndroidAPIUtils.getAndroidPluginService() + .getEnterpriseUser(CarbonContext.getThreadLocalCarbonContext().getUsername()); + GoogleAPIInvoker googleAPIInvoker = new GoogleAPIInvoker(enterpriseConfigs.getEsa()); + if (androidEnterpriseUsers != null && androidEnterpriseUsers.size() > 0) { + googleUserId = androidEnterpriseUsers.get(0).getGoogleUserId(); + // If this device is also present, only need to provide a token for this request. + for (AndroidEnterpriseUser enterprise : androidEnterpriseUsers) { + if (enterprise.getEmmDeviceId() != null + && enterprise.getEmmDeviceId().equals(enterpriseUser.getAndroidPlayDeviceId())) { + deviceIdExist = true; + } + } + } else { + googleUserId = googleAPIInvoker.insertUser(enterpriseConfigs.getEnterpriseId(), CarbonContext + .getThreadLocalCarbonContext() + .getUsername()); + } + // Fetching an auth token from Google EMM API + token = googleAPIInvoker.getToken(enterpriseConfigs.getEnterpriseId(), googleUserId); + + if (!deviceIdExist) { + AndroidEnterpriseUser androidEnterpriseUser = new AndroidEnterpriseUser(); + androidEnterpriseUser.setEmmUsername(CarbonContext.getThreadLocalCarbonContext().getUsername()); + androidEnterpriseUser.setTenantId(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + androidEnterpriseUser.setAndroidPlayDeviceId(enterpriseUser.getAndroidPlayDeviceId()); + androidEnterpriseUser.setEnterpriseId(enterpriseConfigs.getEnterpriseId()); + androidEnterpriseUser.setEmmDeviceId(enterpriseUser.getEmmDeviceIdentifier()); + androidEnterpriseUser.setGoogleUserId(googleUserId); + + AndroidAPIUtils.getAndroidPluginService().addEnterpriseUser(androidEnterpriseUser); + } + + return token; + + } + @Override @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java index fb9a3f2bd..1ffadb887 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java @@ -54,9 +54,11 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceExce import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.mobile.android.impl.EnterpriseServiceException; import org.wso2.carbon.mdm.services.android.bean.ErrorResponse; import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidApplication; import org.wso2.carbon.mdm.services.android.bean.wrapper.AndroidDevice; +import org.wso2.carbon.mdm.services.android.bean.wrapper.EnterpriseUser; import org.wso2.carbon.mdm.services.android.exception.UnexpectedServerErrorException; import org.wso2.carbon.mdm.services.android.services.DeviceManagementService; import org.wso2.carbon.mdm.services.android.util.AndroidAPIUtils; @@ -92,6 +94,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { private static final String OPERATION_ERROR_STATUS = "ERROR"; private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); + public static final String GOOGLE_AFW_EMM_ANDROID_ID = "googleEMMAndroidId"; + public static final String GOOGLE_AFW_DEVICE_ID = "googleEMMDeviceId"; @PUT @Path("/{id}/applications") @@ -233,6 +237,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); } try { + String token = null; Device device = new Device(); device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); device.setEnrolmentInfo(androidDevice.getEnrolmentInfo()); @@ -244,6 +249,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { device.setFeatures(androidDevice.getFeatures()); device.setProperties(androidDevice.getProperties()); + String googleEMMAndroidId = null; + String googleEMMDeviceId = null; + if (androidDevice.getProperties() != null) { + for (Device.Property property : androidDevice.getProperties()) { + if (property.getName().equals(GOOGLE_AFW_EMM_ANDROID_ID)) { + googleEMMAndroidId = property.getValue(); + } else if (property.getName().equals(GOOGLE_AFW_DEVICE_ID)) { + googleEMMDeviceId = property.getValue(); + } + } + + if (googleEMMAndroidId != null && googleEMMDeviceId != null) { + EnterpriseUser user = new EnterpriseUser(); + user.setAndroidPlayDeviceId(googleEMMAndroidId); + user.setEmmDeviceIdentifier(googleEMMDeviceId); + AndroidEnterpriseServiceImpl enterpriseService = new AndroidEnterpriseServiceImpl(); + token = enterpriseService.insertUser(user); + } + } + + boolean status = AndroidAPIUtils.getDeviceManagementService().enrollDevice(device); if (status) { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(androidDevice.getDeviceIdentifier(), @@ -301,8 +327,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { Message responseMessage = new Message(); responseMessage.setResponseCode(Response.Status.OK.toString()); - responseMessage.setResponseMessage("Android device, which carries the id '" + - androidDevice.getDeviceIdentifier() + "' has successfully been enrolled"); + if (token == null) { + responseMessage.setResponseMessage("Android device, which carries the id '" + + androidDevice.getDeviceIdentifier() + "' has successfully been enrolled"); + } else { + responseMessage.setResponseMessage("Google response token" + token); + } return Response.status(Response.Status.OK).entity(responseMessage).build(); } else { Message responseMessage = new Message(); @@ -339,6 +369,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { log.error(msg, e); throw new UnexpectedServerErrorException( new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + } catch (EnterpriseServiceException e) { + String msg = "Error occurred while adding user via Google Apis '" + + androidDevice.getDeviceIdentifier() + "'"; + log.error(msg, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } From fd7b9850250dc1ed0786bcfee92a222cee7cae70 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Wed, 6 Nov 2019 14:33:21 +0530 Subject: [PATCH 2/3] Enterprise unenroll fix --- .../remote.session/RemoteSessionManagementServiceImpl.java | 1 + .../public/js/platform-configuration.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java index cdd453a48..3b4f6c61f 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java @@ -110,6 +110,7 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme + " , deviceId : " + deviceId); } } catch (OperationManagementException | InvalidDeviceException e) { + log.error("error", e); throw new RemoteSessionManagementException("Error occurred while adding initial operation for the " + "device Type : " + deviceType + " , deviceId : " + deviceId); } catch (DeviceAccessAuthorizationException e) { diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android.platform.configuration/public/js/platform-configuration.js b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android.platform.configuration/public/js/platform-configuration.js index 48069ebec..b14da8670 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android.platform.configuration/public/js/platform-configuration.js +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android.platform.configuration/public/js/platform-configuration.js @@ -479,7 +479,8 @@ $(document).ready(function () { showPopup(); $("a#remove-unenroll-yes-link").click(function () { - var serverDetails = $("input#afw-server-details").val() + "/api/device-mgt/android/v1.0/enterprise/324/unenroll"; + var serverDetails = $("input#afw-server-details").val() + + "/api/android-for-work/v1.0/google/enterprise/unenroll/" + $("input#afw-enterprise-id").val(); var emmToken = $("input#afw-backend-token").val(); unenroll(serverDetails, emmToken); hidePopup(); From 64e55629f18dd8689400fb6879d1c8c6bd932a52 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Wed, 6 Nov 2019 14:59:06 +0530 Subject: [PATCH 3/3] Refactor the error message --- .../RemoteSessionManagementServiceImpl.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java index 3b4f6c61f..9af8618ee 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/RemoteSessionManagementServiceImpl.java @@ -110,12 +110,15 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme + " , deviceId : " + deviceId); } } catch (OperationManagementException | InvalidDeviceException e) { - log.error("error", e); - throw new RemoteSessionManagementException("Error occurred while adding initial operation for the " + - "device Type : " + deviceType + " , deviceId : " + deviceId); + String msg = "Error occurred while adding initial operation for the " + + "device Type : " + deviceType + " , deviceId : " + deviceId; + log.error(msg, e); + throw new RemoteSessionManagementException(msg); } catch (DeviceAccessAuthorizationException e) { - throw new RemoteSessionManagementException("Error occurred while device access authorization for the " + - "device Type : " + deviceType + " , " + "deviceId : " + deviceId); + String msg = "Error occurred while device access authorization for the " + + "device Type : " + deviceType + " , " + "deviceId : " + deviceId; + log.error(msg, e); + throw new RemoteSessionManagementException(msg); } finally { PrivilegedCarbonContext.endTenantFlow(); }