diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/pom.xml b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/pom.xml index 400534fdb..18b1ed7d0 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/pom.xml +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/pom.xml @@ -265,6 +265,11 @@ org.wso2.carbon.device.mgt.common provided + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.mobile.windows + provided + org.wso2.carbon.devicemgt org.wso2.carbon.webapp.authenticator.framework diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java index 7a81fdd10..d6b51faba 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.mobile.windows.api.common.util; import org.apache.commons.codec.binary.Base64; +import org.apache.http.auth.AUTH; import org.apache.ws.security.WSSecurityException; import org.apache.ws.security.handler.RequestData; import org.apache.ws.security.message.token.BinarySecurity; @@ -32,6 +33,9 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.AuthenticationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.OAuthTokenValidationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException; +import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import java.util.HashMap; @@ -94,12 +98,17 @@ public class BSTValidator implements Validator { private boolean authenticate(String binarySecurityToken, AuthenticationInfo authenticationInfo) throws AuthenticationException { WindowsAPIUtils.startTenantFlow(authenticationInfo); - if (DeviceUtil.getCacheEntry(binarySecurityToken) != null) { - CacheEntry cacheentry = (CacheEntry) DeviceUtil.getCacheEntry(binarySecurityToken); - String username = cacheentry.getUsername(); - return username != null; - } else { - return false; + MobileCacheEntry cacheEntry; + try { + cacheEntry = DeviceUtil.getTokenEntry(binarySecurityToken); + if (cacheEntry != null) { + String username = cacheEntry.getUsername(); + return username != null; + } else { + return false; + } + } catch (WindowsDeviceEnrolmentException e) { + throw new AuthenticationException("Authentication failure when fetching token entry", e); } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java index b378ae8c9..51cd42b28 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java @@ -18,14 +18,17 @@ package org.wso2.carbon.device.mgt.mobile.windows.api.common.util; -import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException; +import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; +import org.wso2.carbon.device.mgt.mobile.windows.impl.util.WindowsUtils; -import javax.cache.Cache; -import javax.cache.CacheConfiguration; -import javax.cache.CacheManager; -import javax.cache.Caching; import java.util.UUID; -import java.util.concurrent.TimeUnit; /** * Class for generate random token for XCEP and WSTEP. @@ -36,45 +39,106 @@ public class DeviceUtil { private static final String TOKEN_CACHE = "TOKEN_CACHE"; private static final long CACHE_DURATION = 15l; private static boolean isContextCacheInitialized = false; + private static WindowsTokenService tokenService; + + private static final Log log = LogFactory.getLog(DeviceUtil.class); + + static { + try { + tokenService = WindowsUtils.getEnrollmentTokenService(); + } catch (WindowsDeviceMgtPluginException e) { + log.error("WindowsTokenService cannot be initialized.", e); + } + } public static String generateRandomToken() { return String.valueOf(UUID.randomUUID()); } - public static void persistChallengeToken(String token, String deviceID, String username) { - - Object objCacheEntry = getCacheEntry(token); - CacheEntry cacheEntry; - if (objCacheEntry == null) { - cacheEntry = new CacheEntry(); - cacheEntry.setUsername(username); - } else { - cacheEntry = (CacheEntry) objCacheEntry; - } - if (deviceID != null) { - cacheEntry.setDeviceID(deviceID); + public static void persistChallengeToken(String token, String deviceID, String username) + throws WindowsDeviceEnrolmentException { + try { + if(tokenService == null) { + tokenService = WindowsUtils.getEnrollmentTokenService(); + } + MobileCacheEntry existingCacheEntry = tokenService.getCacheToken(token); + PrivilegedCarbonContext carbonCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + if (existingCacheEntry == null) { + MobileCacheEntry newCacheEntry = new MobileCacheEntry(); + newCacheEntry.setDeviceID(deviceID); + newCacheEntry.setUsername(username); + newCacheEntry.setCacheToken(token); + newCacheEntry.setTenantDomain(carbonCtx.getTenantDomain()); + newCacheEntry.setTenanatID(carbonCtx.getTenantId()); + tokenService.saveCacheToken(newCacheEntry); + } else { + existingCacheEntry.setDeviceID(deviceID); + existingCacheEntry.setCacheToken(token); + tokenService.updateCacheToken(existingCacheEntry); + } + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error occured when saving cache token for device: " + deviceID; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); + } catch (WindowsDeviceMgtPluginException e) { + String msg = "Error occured when retrieving enrollment token service"; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); } - getTokenCache().put(token.trim(), cacheEntry); } - public static void removeToken(String token) { - getTokenCache().remove(token); + public static MobileCacheEntry getTokenEntry(String token) + throws WindowsDeviceEnrolmentException { + MobileCacheEntry tokenEntry; + try { + if (tokenService == null) { + tokenService = WindowsUtils.getEnrollmentTokenService(); + } + tokenEntry = tokenService.getCacheToken(token); + } catch (WindowsDeviceMgtPluginException e) { + String msg = "Error occured when retrieving enrollment token service."; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error occured when retrieving enrollment token."; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); + } + return tokenEntry; } - public static Object getCacheEntry(String token) { - return getTokenCache().get(token); + public static MobileCacheEntry getTokenEntryFromDeviceId(String deviceId) + throws WindowsDeviceEnrolmentException { + MobileCacheEntry tokenEntry; + try { + if (tokenService == null) { + tokenService = WindowsUtils.getEnrollmentTokenService(); + } + tokenEntry = tokenService.getCacheTokenFromDeviceId(deviceId); + } catch (WindowsDeviceMgtPluginException e) { + String msg = "Error occured when retrieving enrollment token service."; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error occured when retrieving enrollment token."; + log.error(msg); + throw new WindowsDeviceEnrolmentException(msg, e); + } + return tokenEntry; } - private static Cache getTokenCache() { - CacheManager contextCacheManager = Caching.getCacheManager(TOKEN_CACHE_MANAGER). - getCache(TOKEN_CACHE).getCacheManager(); - if (!isContextCacheInitialized) { - return Caching.getCacheManager(TOKEN_CACHE_MANAGER).getCache(TOKEN_CACHE); - } else { - isContextCacheInitialized = true; - return contextCacheManager.createCacheBuilder(TOKEN_CACHE_MANAGER).setExpiry( - CacheConfiguration.ExpiryType.MODIFIED, - new CacheConfiguration.Duration(TimeUnit.MINUTES, CACHE_DURATION)).setStoreByValue(false).build(); + public static void removeTokenEntry(String token) { + try { + if (tokenService == null) { + tokenService = WindowsUtils.getEnrollmentTokenService(); + } + tokenService.removeCacheToken(token); + } catch (WindowsDeviceMgtPluginException e) { + String msg = "Error occured when retrieving enrollment token service."; + log.error(msg); + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error occured when removing enrollment token."; + log.error(msg); } } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java index b8e9287e4..25f963ab3 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; import org.json.JSONObject; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/enrollment/impl/EnrollmentServiceImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/enrollment/impl/EnrollmentServiceImpl.java index d45455f0c..ee3d62c9c 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/enrollment/impl/EnrollmentServiceImpl.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/enrollment/impl/EnrollmentServiceImpl.java @@ -44,6 +44,7 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.SyncmlCrede import org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.EnrollmentService; import org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.beans.*; import org.wso2.carbon.device.mgt.mobile.windows.api.services.syncml.beans.WindowsDevice; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.xml.sax.SAXException; @@ -296,7 +297,7 @@ public class EnrollmentServiceImpl implements EnrollmentService { //CacheEntry cacheEntry = (CacheEntry) DeviceUtil.getCacheEntry(headerBst); // String userName = cacheEntry.getUsername(); authNameNode.setTextContent(userName); - DeviceUtil.removeToken(headerBst); + DeviceUtil.removeTokenEntry(headerBst); String password = DeviceUtil.generateRandomToken(); Node passwordAuthPosition = wapParm.item(PluginConstants.CertificateEnrolment.APPAUTH_PASSWORD_POSITION); NamedNodeMap appSrvPasswordAttribute = passwordAuthPosition.getAttributes(); @@ -351,7 +352,12 @@ public class EnrollmentServiceImpl implements EnrollmentService { * @return User for given token. */ private String getRequestedUser(String bst) { - CacheEntry cacheEntry = (CacheEntry) DeviceUtil.getCacheEntry(bst); + MobileCacheEntry cacheEntry = null; + try { + cacheEntry = DeviceUtil.getTokenEntry(bst); + } catch (WindowsDeviceEnrolmentException e) { + + } return cacheEntry.getUsername(); } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/impl/DeviceManagementServiceImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/impl/DeviceManagementServiceImpl.java index 38dfde51d..ae0e59a9c 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/impl/DeviceManagementServiceImpl.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/impl/DeviceManagementServiceImpl.java @@ -43,6 +43,7 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.operations.*; import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.*; import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.DeviceInfo; import org.wso2.carbon.device.mgt.mobile.windows.api.services.DeviceManagementService; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -80,14 +81,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlHeader.getSource(). getLocURI()); msgId = syncmlHeader.getMsgID(); + if ((PluginConstants.SyncML.SYNCML_FIRST_MESSAGE_ID == msgId) && (PluginConstants.SyncML.SYNCML_FIRST_SESSION_ID == sessionId)) { token = syncmlHeader.getCredential().getData(); - CacheEntry cacheToken = (CacheEntry) DeviceUtil.getCacheEntry(token); + MobileCacheEntry cacheToken = DeviceUtil.getTokenEntry(token); + DeviceUtil.persistChallengeToken(token, deviceIdentifier.getId(), user); + PrivilegedCarbonContext carbonCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + carbonCtx.setTenantId(cacheToken.getTenanatID(), true); if ((cacheToken.getUsername() != null) && (cacheToken.getUsername().equals(user))) { - if (modifyEnrollWithMoreDetail(request)) { + if (modifyEnrollWithMoreDetail(request, cacheToken.getTenantDomain(), cacheToken.getTenanatID())) { pendingOperations = operationHandler.getPendingOperations(syncmlDocument); response = operationReply.generateReply(syncmlDocument, pendingOperations); return Response.status(Response.Status.OK).entity(response).build(); @@ -102,6 +107,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } } else { + MobileCacheEntry cacheToken = DeviceUtil.getTokenEntryFromDeviceId(deviceIdentifier.getId()); + PrivilegedCarbonContext carbonCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + carbonCtx.setTenantId(cacheToken.getTenanatID()); if ((syncmlDocument.getBody().getAlert() != null)) { if (!syncmlDocument.getBody().getAlert().getData().equals(Constants.DISENROLL_ALERT_DATA)) { pendingOperations = operationHandler.getPendingOperations(syncmlDocument); @@ -152,7 +160,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { * @throws WindowsDeviceEnrolmentException * @throws WindowsOperationException */ - private boolean modifyEnrollWithMoreDetail(Document request) throws WindowsDeviceEnrolmentException, + private boolean modifyEnrollWithMoreDetail(Document request, String tenantDomain, int tenantId) throws WindowsDeviceEnrolmentException, WindowsOperationException { String devMan = null; @@ -177,6 +185,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { user = syncmlDocument.getHeader().getSource().getLocName(); AuthenticationInfo authenticationInfo = new AuthenticationInfo(); authenticationInfo.setUsername(user); + authenticationInfo.setTenantId(tenantId); + authenticationInfo.setTenantDomain(tenantDomain); WindowsAPIUtils.startTenantFlow(authenticationInfo); DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlDocument. getHeader().getSource().getLocURI()); diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/syncml/impl/SyncmlServiceImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/syncml/impl/SyncmlServiceImpl.java index 4cc2f0ac8..356035f04 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/syncml/impl/SyncmlServiceImpl.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/syncml/impl/SyncmlServiceImpl.java @@ -22,12 +22,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; 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.mobile.windows.api.common.PluginConstants; -import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlMessageFormatException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlOperationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsConfigurationException; @@ -35,10 +38,20 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDe import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.AuthenticationInfo; import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.DeviceUtil; import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.WindowsAPIUtils; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.ItemTag; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.ReplaceTag; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.SyncmlDocument; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.SyncmlHeader; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.WindowsOperationException; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.Constants; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.DeviceInfo; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.OperationHandler; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.OperationReply; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.SyncmlGenerator; +import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.SyncmlParser; import org.wso2.carbon.device.mgt.mobile.windows.api.services.syncml.SyncmlService; import org.wso2.carbon.device.mgt.mobile.windows.api.services.syncml.beans.WindowsDevice; -import org.wso2.carbon.device.mgt.mobile.windows.api.operations.*; -import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.*; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -143,11 +156,11 @@ public class SyncmlServiceImpl implements SyncmlService { if ((PluginConstants.SyncML.SYNCML_FIRST_MESSAGE_ID == msgId) && (PluginConstants.SyncML.SYNCML_FIRST_SESSION_ID == sessionId)) { token = syncmlHeader.getCredential().getData(); - CacheEntry cacheToken = (CacheEntry) DeviceUtil.getCacheEntry(token); + MobileCacheEntry cacheToken = DeviceUtil.getTokenEntry(token); if ((cacheToken.getUsername() != null) && (cacheToken.getUsername().equals(user))) { - if (enrollDevice(request)) { + if (enrollDevice(request, cacheToken.getTenantDomain(), cacheToken.getTenanatID())) { deviceInfoOperations = deviceInfo.getDeviceInfo(); response = generateReply(syncmlDocument, deviceInfoOperations); return Response.status(Response.Status.OK).entity(response).build(); @@ -163,7 +176,8 @@ public class SyncmlServiceImpl implements SyncmlService { } } else if (PluginConstants.SyncML.SYNCML_SECOND_MESSAGE_ID == msgId && PluginConstants.SyncML.SYNCML_FIRST_SESSION_ID == sessionId) { - if (enrollDevice(request)) { + PrivilegedCarbonContext carbonCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + if (enrollDevice(request, carbonCtx.getTenantDomain(), carbonCtx.getTenantId())) { return Response.ok().entity(generateReply(syncmlDocument, null)).build(); } else { String msg = "Error occurred in modify enrollment."; @@ -223,7 +237,7 @@ public class SyncmlServiceImpl implements SyncmlService { * @throws WindowsDeviceEnrolmentException * @throws WindowsOperationException */ - private boolean enrollDevice(Document request) throws WindowsDeviceEnrolmentException, + private boolean enrollDevice(Document request, String tenantDomain, int tenantId) throws WindowsDeviceEnrolmentException, WindowsOperationException { String osVersion; @@ -256,6 +270,8 @@ public class SyncmlServiceImpl implements SyncmlService { user = syncmlDocument.getHeader().getSource().getLocName(); AuthenticationInfo authenticationInfo = new AuthenticationInfo(); authenticationInfo.setUsername(user); + authenticationInfo.setTenantDomain(tenantDomain); + authenticationInfo.setTenantId(tenantId); WindowsAPIUtils.startTenantFlow(authenticationInfo); if (log.isDebugEnabled()) { diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/wstep/impl/CertificateEnrollmentServiceImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/wstep/impl/CertificateEnrollmentServiceImpl.java index a749214ba..f71aa52e2 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/wstep/impl/CertificateEnrollmentServiceImpl.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/services/wstep/impl/CertificateEnrollmentServiceImpl.java @@ -26,14 +26,17 @@ import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.jaxws.context.WrappedMessageContext; import org.apache.cxf.message.Message; -import org.w3c.dom.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants; -import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.CertificateGenerationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlMessageFormatException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WAPProvisioningException; @@ -46,6 +49,7 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.services.wstep.beans.Additi import org.wso2.carbon.device.mgt.mobile.windows.api.services.wstep.beans.BinarySecurityToken; import org.wso2.carbon.device.mgt.mobile.windows.api.services.wstep.beans.RequestSecurityTokenResponse; import org.wso2.carbon.device.mgt.mobile.windows.api.services.wstep.beans.RequestedSecurityToken; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import org.xml.sax.SAXException; import javax.annotation.Resource; @@ -293,10 +297,10 @@ public class CertificateEnrollmentServiceImpl implements CertificateEnrollmentSe Node userNameAuthPosition = wapParm.item(PluginConstants.CertificateEnrolment.APPAUTH_USERNAME_POSITION); NamedNodeMap appServerAttribute = userNameAuthPosition.getAttributes(); Node authNameNode = appServerAttribute.getNamedItem(PluginConstants.CertificateEnrolment.VALUE); - CacheEntry cacheEntry = (CacheEntry) DeviceUtil.getCacheEntry(headerBst); + MobileCacheEntry cacheEntry = DeviceUtil.getTokenEntry(headerBst); String userName = cacheEntry.getUsername(); authNameNode.setTextContent(cacheEntry.getUsername()); - DeviceUtil.removeToken(headerBst); + DeviceUtil.removeTokenEntry(headerBst); String password = DeviceUtil.generateRandomToken(); Node passwordAuthPosition = wapParm.item(PluginConstants.CertificateEnrolment.APPAUTH_PASSWORD_POSITION); NamedNodeMap appSrvPasswordAttribute = passwordAuthPosition.getAttributes(); diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/pom.xml b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/pom.xml index 60387832e..e24ebfe0b 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/pom.xml +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/pom.xml @@ -64,6 +64,7 @@ org.wso2.carbon.context, org.wso2.carbon.utils.*, org.wso2.carbon.device.mgt.common.*, + org.wso2.carbon.device.mgt.core.dao.*, org.wso2.carbon.ndatasource.core, org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.registry.core, diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenService.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenService.java new file mode 100644 index 000000000..851a108c0 --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenService.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.mobile.windows.impl; + +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; + +public interface WindowsTokenService { + void saveCacheToken(MobileCacheEntry entry) throws MobileDeviceManagementDAOException; + void updateCacheToken(MobileCacheEntry entry) throws MobileDeviceManagementDAOException; + MobileCacheEntry getCacheToken(String token) throws MobileDeviceManagementDAOException; + MobileCacheEntry getCacheTokenFromDeviceId(String deviceId) throws MobileDeviceManagementDAOException; + void removeCacheToken(String token) throws MobileDeviceManagementDAOException; +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenServiceImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenServiceImpl.java new file mode 100644 index 000000000..68c40595f --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/WindowsTokenServiceImpl.java @@ -0,0 +1,81 @@ +package org.wso2.carbon.device.mgt.mobile.windows.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.WindowsDAOFactory; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.WindowsEnrollmentTokenDAO; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.impl.WindowsEnrollmentTokenDAOImpl; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; + +public class WindowsTokenServiceImpl implements WindowsTokenService { + + private static final Log log = LogFactory.getLog(WindowsTokenServiceImpl.class); + private static WindowsEnrollmentTokenDAO windowsEnrollmentTokenDAO; + + public WindowsTokenServiceImpl() { + WindowsDAOFactory windowsDAOFactory = new WindowsDAOFactory(); + windowsEnrollmentTokenDAO = new WindowsEnrollmentTokenDAOImpl(); + } + + @Override + public void saveCacheToken(MobileCacheEntry entry) throws MobileDeviceManagementDAOException { + try { + WindowsDAOFactory.beginTransaction(); + windowsEnrollmentTokenDAO.addCacheToken(entry); + WindowsDAOFactory.commitTransaction(); + } finally { + WindowsDAOFactory.closeConnection(); + } + } + + @Override + public void updateCacheToken(MobileCacheEntry entry) throws MobileDeviceManagementDAOException { + try { + WindowsDAOFactory.beginTransaction(); + windowsEnrollmentTokenDAO.updateCacheToken(entry); + WindowsDAOFactory.commitTransaction(); + } finally { + WindowsDAOFactory.closeConnection(); + } + } + + @Override + public MobileCacheEntry getCacheToken(String token) throws MobileDeviceManagementDAOException { + MobileCacheEntry cacheEntry = null; + try { + WindowsDAOFactory.beginTransaction(); + cacheEntry = windowsEnrollmentTokenDAO.getCacheToken(token); + WindowsDAOFactory.commitTransaction(); + } finally { + WindowsDAOFactory.closeConnection(); + } + return cacheEntry; + } + + @Override public MobileCacheEntry getCacheTokenFromDeviceId(String deviceId) + throws MobileDeviceManagementDAOException { + MobileCacheEntry cacheEntry = null; + try { + WindowsDAOFactory.beginTransaction(); + cacheEntry = windowsEnrollmentTokenDAO.getCacheTokenFromDeviceId(deviceId); + WindowsDAOFactory.commitTransaction(); + } finally { + WindowsDAOFactory.closeConnection(); + } + return cacheEntry; + } + + @Override + public void removeCacheToken(String token) throws MobileDeviceManagementDAOException { + try { + WindowsDAOFactory.beginTransaction(); + windowsEnrollmentTokenDAO.deleteCacheToken(token); + WindowsDAOFactory.commitTransaction(); + } finally { + WindowsDAOFactory.closeConnection(); + } + } +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsDAOFactory.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsDAOFactory.java index 6bbdf572f..760a8a5fd 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsDAOFactory.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsDAOFactory.java @@ -21,9 +21,13 @@ package org.wso2.carbon.device.mgt.mobile.windows.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.impl.WindowsDeviceDAOImpl; import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.impl.WindowsFeatureDAOImpl; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @@ -35,7 +39,18 @@ public class WindowsDAOFactory extends AbstractMobileDeviceManagementDAOFactory private static ThreadLocal currentConnection = new ThreadLocal<>(); public WindowsDAOFactory() { - this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); +// this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); + String dataSourceName = "jdbc/MobileWindowsDM_DS"; + initDAO(dataSourceName); + } + + public void initDAO(String datasourceName) { + try { + Context ctx = new InitialContext(); + dataSource = (DataSource) ctx.lookup(datasourceName); + } catch (NamingException e) { + + } } @Override diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsEnrollmentTokenDAO.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsEnrollmentTokenDAO.java new file mode 100644 index 000000000..9ab0a2b73 --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/WindowsEnrollmentTokenDAO.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.mobile.windows.impl.dao; + +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; + +public interface WindowsEnrollmentTokenDAO { + + MobileCacheEntry getCacheToken(String token) throws MobileDeviceManagementDAOException; + + MobileCacheEntry getCacheTokenFromDeviceId(String deviceId) throws MobileDeviceManagementDAOException; + + boolean addCacheToken(MobileCacheEntry cacheEntry) throws MobileDeviceManagementDAOException; + + boolean updateCacheToken(MobileCacheEntry cacheEntry) throws MobileDeviceManagementDAOException; + + boolean deleteCacheToken(String mobileDeviceId) throws MobileDeviceManagementDAOException; +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/impl/WindowsEnrollmentTokenDAOImpl.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/impl/WindowsEnrollmentTokenDAOImpl.java new file mode 100644 index 000000000..338818e79 --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dao/impl/WindowsEnrollmentTokenDAOImpl.java @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.mobile.windows.impl.dao.impl; + +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.WindowsDAOFactory; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.WindowsEnrollmentTokenDAO; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; +import org.wso2.carbon.device.mgt.mobile.windows.impl.util.WindowsPluginConstants; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class WindowsEnrollmentTokenDAOImpl implements WindowsEnrollmentTokenDAO { + + public WindowsEnrollmentTokenDAOImpl() { + } + + @Override + public MobileCacheEntry getCacheToken(String token) throws MobileDeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + MobileCacheEntry cacheEntry = null; + try { + conn = WindowsDAOFactory.getConnection(); + String selectDBQuery = "SELECT TENANT_DOMAIN, TENANT_ID, ENROLLMENT_TOKEN, DEVICE_ID, USERNAME, OWNERSHIP " + + "FROM WINDOWS_ENROLLMENT_TOKEN WHERE ENROLLMENT_TOKEN = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, token); + rs = stmt.executeQuery(); + + while (rs.next()) { + cacheEntry = new MobileCacheEntry(); + cacheEntry.setDeviceID(rs.getString(WindowsPluginConstants.DEVICE_ID)); + cacheEntry.setTenantDomain(rs.getString(WindowsPluginConstants.TENANT_DOMAIN)); + cacheEntry.setTenanatID(rs.getInt(WindowsPluginConstants.TENANT_ID)); + cacheEntry.setUsername(rs.getString(WindowsPluginConstants.USER_NAME)); + cacheEntry.setOwnership(rs.getString(WindowsPluginConstants.OWNERSHIP)); + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while fetching the Windows device token for the enrollment token '" + + token + "' from the Windows db.", e); + } + return cacheEntry; + } + + @Override public MobileCacheEntry getCacheTokenFromDeviceId(String deviceId) + throws MobileDeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + MobileCacheEntry cacheEntry = null; + try { + conn = WindowsDAOFactory.getConnection(); + String selectDBQuery = "SELECT TENANT_DOMAIN, TENANT_ID, ENROLLMENT_TOKEN, DEVICE_ID, USERNAME, OWNERSHIP " + + "FROM WINDOWS_ENROLLMENT_TOKEN WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + rs = stmt.executeQuery(); + + while (rs.next()) { + cacheEntry = new MobileCacheEntry(); + cacheEntry.setDeviceID(rs.getString(WindowsPluginConstants.DEVICE_ID)); + cacheEntry.setTenantDomain(rs.getString(WindowsPluginConstants.TENANT_DOMAIN)); + cacheEntry.setTenanatID(rs.getInt(WindowsPluginConstants.TENANT_ID)); + cacheEntry.setUsername(rs.getString(WindowsPluginConstants.USER_NAME)); + cacheEntry.setOwnership(rs.getString(WindowsPluginConstants.OWNERSHIP)); + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while fetching the Windows device token for the enrollment token '" + + deviceId + "' from the Windows db.", e); + } + return cacheEntry; + } + + @Override + public boolean addCacheToken(MobileCacheEntry cacheEntry) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "INSERT INTO WINDOWS_ENROLLMENT_TOKEN(TENANT_DOMAIN, TENANT_ID, ENROLLMENT_TOKEN, DEVICE_ID, " + + "USERNAME, OWNERSHIP) VALUES (?, ?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, cacheEntry.getTenantDomain()); + stmt.setInt(2, cacheEntry.getTenanatID()); + stmt.setString(3, cacheEntry.getCacheToken()); + stmt.setString(4, cacheEntry.getDeviceID()); + stmt.setString(5, cacheEntry.getUsername()); + stmt.setString(6, cacheEntry.getOwnership()); + int rows = stmt.executeUpdate(); + if(rows > 0) { + status = true; + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while adding the Windows device enrollment token for'" + + cacheEntry.getDeviceID() + "' to the Windows db.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean updateCacheToken(MobileCacheEntry cacheEntry) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String updateDBQuery = + "UPDATE WINDOWS_ENROLLMENT_TOKEN SET TENANT_DOMAIN = ?, TENANT_ID = ?, ENROLLMENT_TOKEN = ?, USERNAME = ?, " + + "DEVICE_ID = ? WHERE ENROLLMENT_TOKEN = ?"; + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, cacheEntry.getTenantDomain()); + stmt.setInt(2, cacheEntry.getTenanatID()); + stmt.setString(3, cacheEntry.getCacheToken()); + stmt.setString(4, cacheEntry.getUsername()); + stmt.setString(5, cacheEntry.getDeviceID()); + stmt.setString(6, cacheEntry.getCacheToken()); + int rows = stmt.executeUpdate(); + if(rows > 0) { + status = true; + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while updating the Windows device enrollment token for'" + + cacheEntry.getDeviceID() + "' to the Windows db.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean deleteCacheToken(String mobileDeviceId) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String deleteDBQuery = "DELETE FROM WINDOWS_ENROLLMENT_TOKEN WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, mobileDeviceId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while deleting windows device '" + + mobileDeviceId + "'", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dto/MobileCacheEntry.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dto/MobileCacheEntry.java new file mode 100644 index 000000000..6c138ec0a --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/dto/MobileCacheEntry.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.mobile.windows.impl.dto; + +public class MobileCacheEntry { + + private String cacheToken; + private String tenantDomain; + private int tenanatID; + private String deviceID; + private String username; + private String ownership; + + public String getTenantDomain() { + return tenantDomain; + } + + public void setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + } + + public int getTenanatID() { + return tenanatID; + } + + public void setTenanatID(int tenanatID) { + this.tenanatID = tenanatID; + } + + public String getCacheToken() { + return cacheToken; + } + + public void setCacheToken(String cacheToken) { + this.cacheToken = cacheToken; + } + + public String getDeviceID() { + return deviceID; + } + + public void setDeviceID(String deviceID) { + this.deviceID = deviceID; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getOwnership() { + return ownership; + } + + public void setOwnership(String ownership) { + this.ownership = ownership; + } +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsPluginConstants.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsPluginConstants.java index d7d516b8d..a243cc9c4 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsPluginConstants.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsPluginConstants.java @@ -52,5 +52,10 @@ public class WindowsPluginConstants { public static final String LANGUAGE_CODE_ENGLISH_US = "en_US"; public static final String LANGUAGE_CODE_ENGLISH_UK = "en_UK"; + //properties related to WINDOWS_ENROLLMENT_TOKEN table + public static final String TENANT_DOMAIN = "TENANT_DOMAIN"; + public static final String TENANT_ID = "TENANT_ID"; + public static final String USER_NAME = "USERNAME"; + public static final String OWNERSHIP = "OWNERSHIP"; } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java index 7fba2f5ca..994500f89 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java @@ -18,6 +18,10 @@ package org.wso2.carbon.device.mgt.mobile.windows.impl.util; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; + import java.util.Map; /** @@ -32,4 +36,17 @@ public class WindowsUtils { } return deviceProperty; } + + public static WindowsTokenService getEnrollmentTokenService() throws WindowsDeviceMgtPluginException { + + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + WindowsTokenService tokenService = (WindowsTokenService) + ctx.getOSGiService(WindowsTokenService.class, null); + + if(tokenService == null) { + String msg = "WindowsTokenService is not initialized"; + throw new WindowsDeviceMgtPluginException(msg); + } + return tokenService; + } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementDataHolder.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementDataHolder.java index 600aecc1c..dd2ef82f1 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementDataHolder.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementDataHolder.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.mobile.windows.internal; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; import org.wso2.carbon.registry.core.service.RegistryService; /** @@ -26,6 +27,7 @@ import org.wso2.carbon.registry.core.service.RegistryService; public class WindowsDeviceManagementDataHolder { private RegistryService registryService; + private WindowsTokenService tokenService; private static WindowsDeviceManagementDataHolder thisInstance = new WindowsDeviceManagementDataHolder(); @@ -44,4 +46,19 @@ public class WindowsDeviceManagementDataHolder { this.registryService = registryService; } + public WindowsTokenService getTokenService() { + return tokenService; + } + + public void setTokenService(WindowsTokenService tokenService) { + this.tokenService = tokenService; + } + + public static WindowsDeviceManagementDataHolder getThisInstance() { + return thisInstance; + } + + public static void setThisInstance(WindowsDeviceManagementDataHolder thisInstance) { + WindowsDeviceManagementDataHolder.thisInstance = thisInstance; + } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementServiceComponent.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementServiceComponent.java index fd1e661d3..93926a93d 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementServiceComponent.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/internal/WindowsDeviceManagementServiceComponent.java @@ -23,6 +23,8 @@ import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenServiceImpl; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -65,6 +67,8 @@ public class WindowsDeviceManagementServiceComponent { // bundleContext.registerService(PolicyMonitoringManager.class, // new WindowsPolicyMonitoringManager(), null); + //Enrollment token service + bundleContext.registerService(WindowsTokenService.class, new WindowsTokenServiceImpl(), null); if (log.isDebugEnabled()) { log.debug("Android Mobile Device Management Service Component has been successfully activated"); @@ -114,4 +118,18 @@ public class WindowsDeviceManagementServiceComponent { WindowsDeviceManagementDataHolder.getInstance().setRegistryService(null); } + protected void setEnrollmentTokenService(WindowsTokenService service) { + if (log.isDebugEnabled()) { + log.debug("Setting windows enrollment token service provider"); + } + WindowsDeviceManagementDataHolder.getInstance().setTokenService(service); + } + + protected void unsetEnrollmentTokenService() { + if (log.isDebugEnabled()) { + log.debug("Removing windows enrollment token service provider"); + } + WindowsDeviceManagementDataHolder.getInstance().setTokenService(null); + } + } diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/h2.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/h2.sql index f1749a8a8..7841ec87c 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/h2.sql +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/h2.sql @@ -28,3 +28,17 @@ CREATE TABLE IF NOT EXISTS `WIN_FEATURE` ( `DESCRIPTION` VARCHAR(200) NULL, PRIMARY KEY (`ID`) ); + +-- ----------------------------------------------------- +-- Table `WINDOWS_ENROLLMENT_TOKEN` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WINDOWS_ENROLLMENT_TOKEN` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `TENANT_DOMAIN` VARCHAR(45) NOT NULL, + `TENANT_ID` INTEGER DEFAULT 0, + `ENROLLMENT_TOKEN` VARCHAR(100) NULL, + `DEVICE_ID` VARCHAR(100) NULL, + `USERNAME` VARCHAR(45) NULL, + `OWNERSHIP` VARCHAR(45) NULL, + PRIMARY KEY (`ID`) +); diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql index 524a483c7..54a7dc720 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql @@ -30,3 +30,17 @@ CREATE TABLE WIN_DEVICE ( DEVICE_NAME VARCHAR(100) NULL DEFAULT NULL, PRIMARY KEY (DEVICE_ID) ); + +-- ----------------------------------------------------- +-- Table `WINDOWS_ENROLLMENT_TOKEN` +-- ----------------------------------------------------- +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WINDOWS_ENROLLMENT_TOKEN]') AND TYPE IN (N'U')) +CREATE TABLE WINDOWS_ENROLLMENT_TOKEN ( + ID INT NOT NULL IDENTITY, + TENANT_DOMAIN VARCHAR(45) NOT NULL, + ENROLLMENT_TOKEN VARCHAR (100) NULL DEFAULT NULL, + DEVICE_ID VARCHAR (100) NOT NULL, + USERNAME VARCHAR (45) NOT NULL, + OWNERSHIP VARCHAR (45) NULL DEFAULT NULL, + PRIMARY KEY (ID); +); \ No newline at end of file diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mysql.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mysql.sql index 2ebcc80f7..378d516da 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mysql.sql +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mysql.sql @@ -30,5 +30,18 @@ CREATE TABLE IF NOT EXISTS `WIN_FEATURE` ( PRIMARY KEY (`ID`)) ENGINE = InnoDB; +-- ----------------------------------------------------- +-- Table `WINDOWS_ENROLLMENT_TOKEN` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WINDOWS_ENROLLMENT_TOKEN` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `TENANT_DOMAIN` VARCHAR(45) NOT NULL, + `ENROLLMENT_TOKEN` VARCHAR(100) NULL, + `DEVICE_ID` VARCHAR(100) NULL, + `USERNAME` VARCHAR(45) NULL, + `OWNERSHIP` VARCHAR(45) NULL, + PRIMARY KEY (`ID`)) +ENGINE = InnoDB; + diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql index a7a389264..3371fab89 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql @@ -31,6 +31,20 @@ CREATE TABLE WIN_FEATURE ( ) / +-- ----------------------------------------------------- +-- Table `WINDOWS_ENROLLMENT_TOKEN` +-- ----------------------------------------------------- +CREATE TABLE WINDOWS_ENROLLMENT_TOKEN ( + ID INT NOT NULL, + TENANT_DOMAIN VARCHAR(45) NOT NULL, + ENROLLMENT_TOKEN VARCHAR(100) NULL, + DEVICE_ID VARCHAR(100) NULL, + USERNAME VARCHAR(45) NULL, + OWNERSHIP VARCHAR(45) NULL, + PRIMARY KEY (ID) +) +/ + -- ----------------------------------------------------- -- Sequence `WIN_FEATURE_ID_INC_SEQ` -- ----------------------------------------------------- diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/postgresql.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/postgresql.sql index d7444757d..c8af94cd6 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/postgresql.sql +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/postgresql.sql @@ -29,3 +29,15 @@ CREATE TABLE IF NOT EXISTS WIN_FEATURE ( PRIMARY KEY (ID) ); +-- ----------------------------------------------------- +-- Table `WINDOWS_ENROLLMENT_TOKEN` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS WINDOWS_ENROLLMENT_TOKEN ( + ID SERIAL NOT NULL, + TENANT_DOMAIN VARCHAR(45) NOT NULL, + ENROLLMENT_TOKEN VARCHAR(100) NULL, + DEVICE_ID VARCHAR(100) NULL, + USERNAME VARCHAR(45) NULL, + OWNERSHIP VARCHAR(45) NULL, + PRIMARY KEY (ID) +); diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml index 3c7782517..2efa4203e 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml @@ -166,6 +166,14 @@ + + DEVICE_INFO + 1 + + + DEVICE_LOCATION + 1 +