|
|
@ -17,59 +17,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.mobile.android.core.impl;
|
|
|
|
package org.wso2.carbon.device.mgt.mobile.android.core.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import org.wso2.carbon.base.ServerConfiguration;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
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.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.ConfigurationEntry;
|
|
|
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
|
|
|
|
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.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.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.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.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.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public class DeviceTypeCommonServiceImpl implements DeviceTypeCommonService {
|
|
|
|
public class DeviceTypeCommonServiceImpl implements DeviceTypeCommonService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceTypeCommonServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Map<EnrolmentInfo.OwnerShip, String> getEnrollmentQRCode() throws DeviceManagementException {
|
|
|
|
public Map<String, Object> getEnrollmentQRCode(String ownershipType) throws DeviceManagementException {
|
|
|
|
return null;
|
|
|
|
AndroidService androidService = AndroidAPIUtils.getAndroidService();
|
|
|
|
}
|
|
|
|
PlatformConfiguration platformConfiguration = androidService.getPlatformConfig();
|
|
|
|
|
|
|
|
|
|
|
|
// private void getPlatformConfig() {
|
|
|
|
Map<String, Object> qrEnrollmentPayload = new HashMap<>();
|
|
|
|
// PlatformConfiguration platformConfiguration;
|
|
|
|
Map<String, Object> defaultQREnrollmentPayload = new HashMap<>();
|
|
|
|
// List<ConfigurationEntry> configs;
|
|
|
|
|
|
|
|
// try {
|
|
|
|
if (Arrays.stream(EnrolmentInfo.OwnerShip.values())
|
|
|
|
// platformConfiguration = AndroidAPIUtils.getDeviceManagementService().
|
|
|
|
.noneMatch(ownerShip -> ownerShip.toString().equalsIgnoreCase(ownershipType))) {
|
|
|
|
// getConfiguration(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
|
|
|
String msg = "Request to get QR enrollment code for invalid device ownership type " + ownershipType;
|
|
|
|
// if (platformConfiguration != null) {
|
|
|
|
log.error(msg);
|
|
|
|
// configs = platformConfiguration.getConfiguration();
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
// } else {
|
|
|
|
}
|
|
|
|
// platformConfiguration = new PlatformConfiguration();
|
|
|
|
|
|
|
|
// configs = new ArrayList<>();
|
|
|
|
String accessToken = getAccessTokenToEnroll();
|
|
|
|
// }
|
|
|
|
if (StringUtils.isBlank(accessToken)) {
|
|
|
|
// ConfigurationEntry entry = new ConfigurationEntry();
|
|
|
|
String msg = "Couldn't get a access token for user " + PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
|
|
|
// License license = AndroidAPIUtils.getDeviceManagementService().getLicense(
|
|
|
|
.getUsername();
|
|
|
|
// DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID, MDMAppConstants.AndroidConstants.
|
|
|
|
log.error(msg);
|
|
|
|
// TenantConfigProperties.LANGUAGE_US);
|
|
|
|
throw new DeviceManagementException(msg);
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// if (configs != null) {
|
|
|
|
|
|
|
|
// ConfigurationEntry versionEntry = new ConfigurationEntry();
|
|
|
|
for (ConfigurationEntry configEntry : platformConfiguration.getConfiguration()) {
|
|
|
|
// versionEntry.setContentType(AndroidConstants.TenantConfigProperties.CONTENT_TYPE_TEXT);
|
|
|
|
if (AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP.equals(configEntry.getName())) {
|
|
|
|
// versionEntry.setName(AndroidConstants.TenantConfigProperties.SERVER_VERSION);
|
|
|
|
defaultQREnrollmentPayload
|
|
|
|
// versionEntry.setValue(ServerConfiguration.getInstance().getFirstProperty("Version"));
|
|
|
|
.put(AndroidConstants.PlatformConfigs.DefaultConfigs.DEFAULT_OWNERSHIP, configEntry.getValue());
|
|
|
|
// configs.add(versionEntry);
|
|
|
|
}
|
|
|
|
// if (license != null) {
|
|
|
|
if (AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP.equals(configEntry.getName())) {
|
|
|
|
// entry.setContentType(AndroidConstants.TenantConfigProperties.CONTENT_TYPE_TEXT);
|
|
|
|
defaultQREnrollmentPayload
|
|
|
|
// entry.setName(AndroidConstants.TenantConfigProperties.LICENSE_KEY);
|
|
|
|
.put(AndroidConstants.PlatformConfigs.DefaultConfigs.SERVER_IP, configEntry.getValue());
|
|
|
|
// entry.setValue(license.getText());
|
|
|
|
}
|
|
|
|
// configs.add(entry);
|
|
|
|
|
|
|
|
// }
|
|
|
|
if (EnrolmentInfo.OwnerShip.COSU.toString().equalsIgnoreCase(ownershipType) || EnrolmentInfo.OwnerShip.COPE
|
|
|
|
// platformConfiguration.setConfiguration(configs);
|
|
|
|
.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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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<String, Object> 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<String, Object> defaultPayload = (Map<String, Object>) 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|