diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 919a901fa5..494b03da1d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -603,6 +603,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { DeviceManager deviceManager = this.getDeviceManager(deviceType); + License license; if (deviceManager == null) { if (log.isDebugEnabled()) { log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " + @@ -611,7 +612,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } try { - return deviceManager.getLicense(languageCode); + license = deviceManager.getLicense(languageCode); + if (license == null) { + if (log.isDebugEnabled()) { + log.debug("Cannot find a license for '" + deviceType + "' device type"); + } + } + return license; } catch (LicenseManagementException e) { throw new DeviceManagementException("Error occurred while retrieving license configured for " + "device type '" + deviceType + "' and language code '" + languageCode + "'", e); @@ -851,7 +858,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - public List getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException { List devices = new ArrayList<>(); List allDevices; diff --git a/components/dynamic-client-registration/dynamic-client-web/src/main/java/org/wso2/carbon/dynamic/client/web/impl/RegistrationServiceImpl.java b/components/dynamic-client-registration/dynamic-client-web/src/main/java/org/wso2/carbon/dynamic/client/web/impl/RegistrationServiceImpl.java index 0640943479..ac5eab14b2 100644 --- a/components/dynamic-client-registration/dynamic-client-web/src/main/java/org/wso2/carbon/dynamic/client/web/impl/RegistrationServiceImpl.java +++ b/components/dynamic-client-registration/dynamic-client-web/src/main/java/org/wso2/carbon/dynamic/client/web/impl/RegistrationServiceImpl.java @@ -19,7 +19,6 @@ package org.wso2.carbon.dynamic.client.web.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml index 563b5370f8..206193a9b0 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml @@ -80,18 +80,6 @@ org.wso2.carbon.identity org.wso2.carbon.identity.application.mgt - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.api - - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.impl - - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.keymgt.client - org.wso2.carbon.identity org.wso2.carbon.identity.oauth diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/pom.xml b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/pom.xml index 1e5d699887..79be0e0b4f 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/pom.xml +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/pom.xml @@ -83,18 +83,6 @@ org.wso2.carbon.identity org.wso2.carbon.identity.application.mgt - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.api - - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.impl - - - org.wso2.carbon.apimgt - org.wso2.carbon.apimgt.keymgt.client - org.wso2.carbon.identity org.wso2.carbon.identity.oauth diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicRegistrationManager.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicRegistrationManager.java index cbe973aae3..a279387332 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicRegistrationManager.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicRegistrationManager.java @@ -18,6 +18,7 @@ package org.wso2.carbon.dynamic.client.web.app.registration; +import org.apache.catalina.core.StandardContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException; @@ -25,8 +26,11 @@ import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationServ import org.wso2.carbon.dynamic.client.registration.OAuthApplicationInfo; import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; import org.wso2.carbon.dynamic.client.web.app.registration.internal.DynamicClientRegistrationDataHolder; +import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientRegistrationConstants; import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil; +import javax.servlet.ServletContext; + /** * This class contains the logic to handle the OAuth application creation process. */ @@ -50,31 +54,47 @@ public class DynamicRegistrationManager { return dynamicRegistrationManager; } - public boolean registerOAuthApplication(RegistrationProfile registrationProfile) { - DynamicClientRegistrationService dynamicClientRegistrationService = - DynamicClientRegistrationDataHolder.getInstance() - .getDynamicClientRegistrationService(); - try { - OAuthApplicationInfo oAuthApplicationInfo = - dynamicClientRegistrationService.registerOAuthApplication(registrationProfile); - OAuthApp oAuthApp = new OAuthApp(); - oAuthApp.setWebAppName(registrationProfile.getClientName()); - oAuthApp.setClientName(oAuthApplicationInfo.getClientName()); - oAuthApp.setClientKey(oAuthApplicationInfo.getClientId()); - oAuthApp.setClientSecret(oAuthApplicationInfo.getClientSecret()); - //store it in registry - return DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthApp); - } catch (DynamicClientRegistrationException e) { - log.error("Error occurred while registering the OAuth application.",e); + public OAuthApp registerOAuthApplication(RegistrationProfile registrationProfile) { + if (log.isDebugEnabled()) { + log.debug("Registering OAuth application for web app : " + registrationProfile.getClientName()); } - return false; + if (DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)) { + DynamicClientRegistrationService dynamicClientRegistrationService = + DynamicClientRegistrationDataHolder.getInstance() + .getDynamicClientRegistrationService(); + try { + OAuthApplicationInfo oAuthApplicationInfo = + dynamicClientRegistrationService + .registerOAuthApplication(registrationProfile); + OAuthApp oAuthApp = new OAuthApp(); + oAuthApp.setWebAppName(registrationProfile.getClientName()); + oAuthApp.setClientName(oAuthApplicationInfo.getClientName()); + oAuthApp.setClientKey(oAuthApplicationInfo.getClientId()); + oAuthApp.setClientSecret(oAuthApplicationInfo.getClientSecret()); + //store it in registry + if (DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthApp)) { + return oAuthApp; + } else { + dynamicClientRegistrationService + .unregisterOAuthApplication(registrationProfile.getOwner(), + oAuthApplicationInfo.getClientName(), + oAuthApplicationInfo.getClientId()); + log.warn("Error occurred while persisting the OAuth application data in registry."); + } + } catch (DynamicClientRegistrationException e) { + log.error("Error occurred while registering the OAuth application : " + + registrationProfile.getClientName(), e); + } + } + return new OAuthApp(); } public OAuthApp getOAuthApplicationData(String clientName) { try { return DynamicClientWebAppRegistrationUtil.getOAuthApplicationData(clientName); } catch (DynamicClientRegistrationException e) { - log.error("Error occurred while fetching the OAuth application data for web app : " + clientName, e); + log.error("Error occurred while fetching the OAuth application data for web app : " + + clientName, e); } return new OAuthApp(); } @@ -87,4 +107,44 @@ public class DynamicRegistrationManager { return false; } + public void initiateDynamicClientRegistrationProcess(StandardContext context) { + ServletContext servletContext = context.getServletContext(); + String requiredDynamicClientRegistration = servletContext.getInitParameter( + DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG); + DynamicRegistrationManager dynamicRegistrationManager = + DynamicRegistrationManager.getInstance(); + //Get the application name from web-context + String webAppName = context.getBaseName(); + RegistrationProfile registrationProfile; + OAuthApp oAuthApp = null; + //Java web-app section + if ((requiredDynamicClientRegistration != null) && + (Boolean.parseBoolean(requiredDynamicClientRegistration))) { + //Check whether this is an already registered application + if (!dynamicRegistrationManager.isRegisteredOAuthApplication(webAppName)) { + //Construct the RegistrationProfile + registrationProfile = DynamicClientWebAppRegistrationUtil + .constructRegistrationProfile(servletContext, webAppName); + //Register the OAuth application + oAuthApp = dynamicRegistrationManager.registerOAuthApplication( + registrationProfile); + + } + } else { + //Jaggery apps + OAuthSettings oAuthSettings = DynamicClientWebAppRegistrationUtil + .getJaggeryAppOAuthSettings(servletContext); + if (oAuthSettings.isRequireDynamicClientRegistration()) { + if (!dynamicRegistrationManager.isRegisteredOAuthApplication(webAppName)) { + registrationProfile = DynamicClientWebAppRegistrationUtil + .constructRegistrationProfile(oAuthSettings, webAppName); + oAuthApp = dynamicRegistrationManager + .registerOAuthApplication(registrationProfile); + } + } + } + DynamicClientWebAppRegistrationUtil.addClientCredentialsToWebContext(oAuthApp, + servletContext); + } + } diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthApp.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthApp.java index ec8ea8e762..7195f55706 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthApp.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthApp.java @@ -22,7 +22,7 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * Represents a OAuth application with basic data. + * Represents an OAuth application with basic data. */ @XmlRootElement(name = "OAuthApp") public class OAuthApp { diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthSettings.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthSettings.java new file mode 100644 index 0000000000..42ee098f28 --- /dev/null +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/OAuthSettings.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2015, 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.dynamic.client.web.app.registration; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Represents OAuthConfiguration data. + */ +@XmlRootElement(name = "OAuthSettings") +public class OAuthSettings { + + private String grantType; + private boolean saasApp; + private String callbackURL; + private String tokenScope; + private boolean requireDynamicClientRegistration; + + @XmlElement(name = "saasApp", required = true) + public boolean isSaasApp() { + return saasApp; + } + + public void setSaasApp(boolean saasApp) { + this.saasApp = saasApp; + } + + @XmlElement(name = "callbackURL", required = false) + public String getCallbackURL() { + return callbackURL; + } + + public void setCallbackURL(String callbackURL) { + this.callbackURL = callbackURL; + } + + @XmlElement(name = "tokenScope", required = false) + public String getTokenScope() { + return tokenScope; + } + + public void setTokenScope(String tokenScope) { + this.tokenScope = tokenScope; + } + + @XmlElement(name = "grantType", required = true) + public String getGrantType() { + return grantType; + } + + public void setGrantType(String grantType) { + this.grantType = grantType; + } + + @XmlElement(name = "requireDynamicClientRegistration", required = true) + public boolean isRequireDynamicClientRegistration() { + return requireDynamicClientRegistration; + } + + public void setRequireDynamicClientRegistration(boolean requireDynamicClientRegistration) { + this.requireDynamicClientRegistration = requireDynamicClientRegistration; + } +} diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientRegistrationDataHolder.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientRegistrationDataHolder.java index 5d8cf806cd..aea9739b17 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientRegistrationDataHolder.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientRegistrationDataHolder.java @@ -21,46 +21,75 @@ package org.wso2.carbon.dynamic.client.web.app.registration.internal; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.ConfigurationContextService; /** * Dataholder class of DynamicClient Webapp Registration component. */ public class DynamicClientRegistrationDataHolder { - private RealmService realmService; - private RegistryService registryService; - private DynamicClientRegistrationService dynamicClientRegistrationService; + private RealmService realmService; + private RegistryService registryService; + private DynamicClientRegistrationService dynamicClientRegistrationService; + private ConfigurationContextService configurationContextService; - public DynamicClientRegistrationService getDynamicClientRegistrationService() { - return dynamicClientRegistrationService; - } + private static DynamicClientRegistrationDataHolder thisInstance = + new DynamicClientRegistrationDataHolder(); - public void setDynamicClientRegistrationService( - DynamicClientRegistrationService dynamicClientRegistrationService) { - this.dynamicClientRegistrationService = dynamicClientRegistrationService; - } + private DynamicClientRegistrationDataHolder() { + } - private static DynamicClientRegistrationDataHolder thisInstance = new DynamicClientRegistrationDataHolder(); + public static DynamicClientRegistrationDataHolder getInstance() { + return thisInstance; + } - private DynamicClientRegistrationDataHolder() {} + public ConfigurationContextService getConfigurationContextService() { + if(configurationContextService != null){ + return configurationContextService; + } else { + throw new IllegalStateException("ConfigurationContext service has not initialized properly"); + } + } - public static DynamicClientRegistrationDataHolder getInstance() { - return thisInstance; - } + public void setConfigurationContextService( + ConfigurationContextService configurationContextService) { + this.configurationContextService = configurationContextService; + } - public RealmService getRealmService() { - return realmService; - } + public DynamicClientRegistrationService getDynamicClientRegistrationService() { + if(dynamicClientRegistrationService != null){ + return dynamicClientRegistrationService; + } else { + throw new IllegalStateException("DynamicClientRegistration service has not initialized properly"); + } + } - public void setRealmService(RealmService realmService) { - this.realmService = realmService; - } + public void setDynamicClientRegistrationService( + DynamicClientRegistrationService dynamicClientRegistrationService) { + this.dynamicClientRegistrationService = dynamicClientRegistrationService; + } - public RegistryService getRegistryService() { - return registryService; - } + public RealmService getRealmService() { + if(realmService != null){ + return realmService; + } else { + throw new IllegalStateException("RealmService has not initialized properly"); + } + } - public void setRegistryService(RegistryService registryService) { - this.registryService = registryService; - } + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + } + + public RegistryService getRegistryService() { + if(registryService != null){ + return registryService; + } else { + throw new IllegalStateException("Registry Service has not initialized properly"); + } + } + + public void setRegistryService(RegistryService registryService) { + this.registryService = registryService; + } } diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientWebAppRegistrationServiceComponent.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientWebAppRegistrationServiceComponent.java index 84572e93cb..9f1eaf4984 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientWebAppRegistrationServiceComponent.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/internal/DynamicClientWebAppRegistrationServiceComponent.java @@ -24,6 +24,7 @@ import org.osgi.service.component.ComponentContext; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.ConfigurationContextService; /** * @scr.component name="org.wso2.carbon.device.manager" immediate="true" @@ -45,6 +46,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setDynamicClientService" * unbind="unsetDynamicClientService" + * @scr.reference name="config.context.service" + * interface="org.wso2.carbon.utils.ConfigurationContextService" + * cardinality="0..1" + * policy="dynamic" + * bind="setConfigurationContextService" + * unbind="unsetConfigurationContextService" */ public class DynamicClientWebAppRegistrationServiceComponent { @@ -133,4 +140,28 @@ public class DynamicClientWebAppRegistrationServiceComponent { DynamicClientRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(null); } + /** + * Sets ConfigurationContext Service. + * + * @param configurationContextService An instance of ConfigurationContextService + */ + protected void setConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Setting ConfigurationContextService"); + } + DynamicClientRegistrationDataHolder.getInstance().setConfigurationContextService(configurationContextService); + } + + /** + * Unsets ConfigurationContext Service. + * + * @param configurationContextService An instance of ConfigurationContextService + */ + protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Un-setting ConfigurationContextService"); + } + DynamicClientRegistrationDataHolder.getInstance().setConfigurationContextService(null); + } + } diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/listner/DynamicClientWebAppDeploymentLifecycleListener.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/listner/DynamicClientWebAppDeploymentLifecycleListener.java index 32c3d1c382..0c3f8674fb 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/listner/DynamicClientWebAppDeploymentLifecycleListener.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/listner/DynamicClientWebAppDeploymentLifecycleListener.java @@ -24,13 +24,7 @@ import org.apache.catalina.LifecycleListener; import org.apache.catalina.core.StandardContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException; -import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; import org.wso2.carbon.dynamic.client.web.app.registration.DynamicRegistrationManager; -import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientRegistrationConstants; -import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil; - -import javax.servlet.ServletContext; /** * This class initiates the dynamic client registration flow for Web applications upon on deployment @@ -46,25 +40,8 @@ public class DynamicClientWebAppDeploymentLifecycleListener implements Lifecycle public void lifecycleEvent(LifecycleEvent lifecycleEvent) { if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); - ServletContext servletContext = context.getServletContext(); - String requiredDynamicClientRegistration = servletContext.getInitParameter( - DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG_PARAM); - if ((requiredDynamicClientRegistration != null) && - (Boolean.parseBoolean(requiredDynamicClientRegistration))) { - DynamicRegistrationManager dynamicRegistrationManager = - DynamicRegistrationManager.getInstance(); - //Get the application name from web-context - String webAppName = context.getBaseName(); - if (!dynamicRegistrationManager.isRegisteredOAuthApplication(webAppName)) { - RegistrationProfile registrationProfile = DynamicClientWebAppRegistrationUtil - .constructRegistrationProfile(servletContext, webAppName); - if(DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)){ - dynamicRegistrationManager.registerOAuthApplication(registrationProfile); - } - } - } else { - //TODO: Need to have the necessary logic to handle jaggery webapp scenario - } + DynamicRegistrationManager.getInstance().initiateDynamicClientRegistrationProcess( + context); } } } diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientRegistrationConstants.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientRegistrationConstants.java index 7b8169d486..d9314c6277 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientRegistrationConstants.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientRegistrationConstants.java @@ -27,8 +27,8 @@ public class DynamicClientRegistrationConstants { public final static String OAUTH_APP_NAME = "appName"; public final static String OAUTH_CLIENT_KEY = "clientKey"; public final static String OAUTH_CLIENT_SECRET = "clientSecret"; - public final static String DYNAMIC_CLIENT_REQUIRED_FLAG_PARAM = - "require-dynamic-client-registration"; + public final static String DYNAMIC_CLIENT_REQUIRED_FLAG = + "requireDynamicClientRegistration"; public static final class ContentTypes { private ContentTypes() { diff --git a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java index 8977faefee..51939d80d7 100644 --- a/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java +++ b/components/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java @@ -18,26 +18,29 @@ package org.wso2.carbon.dynamic.client.web.app.registration.util; +import com.google.gson.stream.JsonReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException; import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; import org.wso2.carbon.dynamic.client.web.app.registration.OAuthApp; +import org.wso2.carbon.dynamic.client.web.app.registration.OAuthSettings; import org.wso2.carbon.dynamic.client.web.app.registration.internal.DynamicClientRegistrationDataHolder; import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.ConfigurationContextService; +import org.wso2.carbon.utils.NetworkUtils; import javax.servlet.ServletContext; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; -import java.io.StringReader; -import java.io.StringWriter; +import java.io.*; import java.nio.charset.Charset; /** @@ -45,12 +48,16 @@ import java.nio.charset.Charset; */ public class DynamicClientWebAppRegistrationUtil { - private final static String OAUTH_PARAM_GRANT_TYPE = "grant-type"; - private final static String OAUTH_PARAM_TOKEN_SCOPE = "token-scope"; - private final static String SP_PARAM_SAAS_APP = "saas-app"; + private final static String OAUTH_PARAM_GRANT_TYPE = "grantType"; + private final static String OAUTH_PARAM_TOKEN_SCOPE = "tokenScope"; + private final static String OAUTH_PARAM_SAAS_APP = "saasApp"; + private final static String OAUTH_PARAM_CALLBACK_URL = "callbackURL"; + private static final String JAGGERY_APP_OAUTH_CONFIG_PATH = + "config" + File.separator + "oauth.json"; private static final Log log = LogFactory.getLog(DynamicClientWebAppRegistrationUtil.class); + private static final String CHARSET_UTF_8 = "UTF-8"; public static Registry getGovernanceRegistry() throws DynamicClientRegistrationException { try { @@ -68,8 +75,12 @@ public class DynamicClientWebAppRegistrationUtil { public static OAuthApp getOAuthApplicationData(String appName) throws DynamicClientRegistrationException { Resource resource; - String resourcePath = DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName; + String resourcePath = + DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName; try { + if (log.isDebugEnabled()) { + log.debug("Retrieving OAuth application " + appName + " data from Registry"); + } resource = DynamicClientWebAppRegistrationUtil.getRegistryResource(resourcePath); if (resource != null) { JAXBContext context = JAXBContext.newInstance(OAuthApp.class); @@ -92,7 +103,7 @@ public class DynamicClientWebAppRegistrationUtil { public static boolean putOAuthApplicationData(OAuthApp oAuthApp) throws DynamicClientRegistrationException { - boolean status = false; + boolean status; try { if (log.isDebugEnabled()) { log.debug("Persisting OAuth application data in Registry"); @@ -102,19 +113,23 @@ public class DynamicClientWebAppRegistrationUtil { Marshaller marshaller = context.createMarshaller(); marshaller.marshal(oAuthApp, writer); - Resource resource = DynamicClientWebAppRegistrationUtil.getGovernanceRegistry().newResource(); + Resource resource = + DynamicClientWebAppRegistrationUtil.getGovernanceRegistry().newResource(); resource.setContent(writer.toString()); resource.setMediaType(DynamicClientRegistrationConstants.ContentTypes.MEDIA_TYPE_XML); String resourcePath = DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + oAuthApp.getWebAppName(); - status = DynamicClientWebAppRegistrationUtil.putRegistryResource(resourcePath, resource); + status = + DynamicClientWebAppRegistrationUtil.putRegistryResource(resourcePath, resource); } catch (RegistryException e) { throw new DynamicClientRegistrationException( "Error occurred while persisting OAuth application data : " + oAuthApp.getClientName(), e); } catch (JAXBException e) { - e.printStackTrace(); + throw new DynamicClientRegistrationException( + "Error occurred while parsing the OAuth application data : " + + oAuthApp.getWebAppName(), e); } return status; } @@ -154,43 +169,154 @@ public class DynamicClientWebAppRegistrationUtil { } } - public static String getUserName(){ + public static String getUserName() { String username = ""; RealmService realmService = DynamicClientRegistrationDataHolder.getInstance().getRealmService(); - if(realmService != null){ + if (realmService != null) { username = realmService.getBootstrapRealmConfiguration().getAdminUserName(); } return username; } - public static RegistrationProfile constructRegistrationProfile(ServletContext servletContext, String webAppName) { - RegistrationProfile registrationProfile = new RegistrationProfile(); + public static RegistrationProfile constructRegistrationProfile(ServletContext servletContext, + String webAppName) { + RegistrationProfile registrationProfile; + registrationProfile = new RegistrationProfile(); registrationProfile.setGrantType(servletContext.getInitParameter( DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE)); registrationProfile.setTokenScope(servletContext.getInitParameter( DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_TOKEN_SCOPE)); registrationProfile.setOwner(DynamicClientWebAppRegistrationUtil.getUserName()); - //TODO : Need to get the hostname properly - registrationProfile.setCallbackUrl("http://localhost:9763/" + webAppName); + String callbackURL = servletContext.getInitParameter( + DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_CALLBACK_URL); + if ((callbackURL != null) && !callbackURL.isEmpty()) { + registrationProfile.setCallbackUrl(callbackURL); + } else { + registrationProfile.setCallbackUrl(DynamicClientWebAppRegistrationUtil.getCallbackUrl( + webAppName)); + } registrationProfile.setClientName(webAppName); registrationProfile.setSaasApp(Boolean.parseBoolean(servletContext.getInitParameter( - DynamicClientWebAppRegistrationUtil.SP_PARAM_SAAS_APP))); + DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_SAAS_APP))); + + return registrationProfile; + } + + public static RegistrationProfile constructRegistrationProfile( + OAuthSettings oAuthSettings, String webAppName) { + RegistrationProfile registrationProfile = new RegistrationProfile(); + if (oAuthSettings != null) { + registrationProfile.setGrantType(oAuthSettings.getGrantType()); + registrationProfile.setTokenScope(oAuthSettings.getTokenScope()); + registrationProfile.setClientName(webAppName); + registrationProfile.setSaasApp(oAuthSettings.isSaasApp()); + registrationProfile.setOwner(DynamicClientWebAppRegistrationUtil.getUserName()); + if (oAuthSettings.getCallbackURL() != null) { + registrationProfile.setCallbackUrl(oAuthSettings.getCallbackURL()); + } else { + registrationProfile.setCallbackUrl( + DynamicClientWebAppRegistrationUtil.getCallbackUrl(webAppName)); + } + } else { + log.warn( + "Please configure OAuth settings properly for jaggery app : " + webAppName); + } return registrationProfile; } public static boolean validateRegistrationProfile(RegistrationProfile registrationProfile) { boolean status = true; - if(registrationProfile.getGrantType() == null){ + if (registrationProfile.getGrantType() == null) { status = false; - log.warn("Required parameter 'grant-type' is missing for initiating Dynamic-Client " + + log.warn("Required parameter 'grantType' is missing for initiating Dynamic-Client " + "registration for webapp : " + registrationProfile.getClientName()); } - if(registrationProfile.getTokenScope() == null){ + if (registrationProfile.getTokenScope() == null) { status = false; - log.warn("Required parameter 'token-scope' is missing for initiating Dynamic-Client " + + log.warn("Required parameter 'tokenScope' is missing for initiating Dynamic-Client " + "registration for webapp : " + registrationProfile.getClientName()); } return status; } -} + + public static OAuthSettings getJaggeryAppOAuthSettings(ServletContext servletContext) { + OAuthSettings oAuthSettings = new OAuthSettings(); + try { + InputStream inputStream = + servletContext.getResourceAsStream(JAGGERY_APP_OAUTH_CONFIG_PATH); + if (inputStream != null) { + JsonReader reader = + new JsonReader(new InputStreamReader(inputStream, CHARSET_UTF_8)); + reader.beginObject(); + while (reader.hasNext()) { + String key = reader.nextName(); + switch (key) { + case DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG: + oAuthSettings.setRequireDynamicClientRegistration(reader.nextBoolean()); + break; + case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE: + oAuthSettings.setGrantType(reader.nextString()); + break; + case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_TOKEN_SCOPE: + oAuthSettings.setTokenScope(reader.nextString()); + break; + case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_SAAS_APP: + oAuthSettings.setSaasApp(reader.nextBoolean()); + break; + case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_CALLBACK_URL: + oAuthSettings.setCallbackURL(reader.nextString()); + break; + } + } + return oAuthSettings; + } + } catch (UnsupportedEncodingException e) { + log.error("Error occurred while initializing OAuth settings for the Jaggery app.", e); + } catch (IOException e) { + log.error("Error occurred while initializing OAuth settings for the Jaggery app.", e); + } + return oAuthSettings; + } + + public static String getServerBaseUrl() { + // Hostname + String hostName = "localhost"; + try { + hostName = NetworkUtils.getMgtHostName(); + } catch (Exception ignored) { + } + // HTTPS port + String mgtConsoleTransport = CarbonUtils.getManagementTransport(); + ConfigurationContextService configContextService = + DynamicClientRegistrationDataHolder.getInstance().getConfigurationContextService(); + int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport); + int httpsProxyPort = + CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), + mgtConsoleTransport); + if (httpsProxyPort > 0) { + port = httpsProxyPort; + } + return "https://" + hostName + ":" + port; + } + + public static String getCallbackUrl(String context) { + return getServerBaseUrl() + "/" + context; + } + + public static void addClientCredentialsToWebContext(OAuthApp oAuthApp, + ServletContext servletContext) { + if(oAuthApp != null){ + //Check for client credentials + if ((oAuthApp.getClientKey() != null && !oAuthApp.getClientKey().isEmpty()) && + (oAuthApp.getClientSecret() != null && !oAuthApp.getClientSecret().isEmpty())) { + servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_KEY, + oAuthApp.getClientKey()); + servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_SECRET, + oAuthApp.getClientSecret()); + } else { + log.warn("Client credentials not found for web app : " + oAuthApp.getWebAppName()); + } + } + } +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index e96e4ff6a0..1ad71fa0f9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -119,43 +119,13 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException { try { - - List effectiveFeatures = PolicyManagementDataHolder.getInstance() - .getPolicyEvaluationPoint(). - getEffectiveFeatures(deviceIdentifier); - - List deviceIdentifiers = new ArrayList(); - deviceIdentifiers.add(deviceIdentifier); - List profileOperationList = new ArrayList(); - - if (!effectiveFeatures.isEmpty()) { - for (ProfileFeature feature : effectiveFeatures) { - ProfileOperation operation = new ProfileOperation(); - - operation.setCode(feature.getFeatureCode()); - operation.setPayLoad(feature.getContent()); - operation.setStatus(Operation.Status.PENDING); - operation.setType(Operation.Type.PROFILE); - operation.setEnabled(true); - profileOperationList.add(operation); - PolicyManagementDataHolder.getInstance().getDeviceManagementService(). - addOperation(operation, deviceIdentifiers); - } - } else { - return null; - } - - return effectiveFeatures; + return PolicyManagementDataHolder.getInstance(). + getPolicyEvaluationPoint().getEffectiveFeatures(deviceIdentifier); } catch (PolicyEvaluationException e) { String msg = "Error occurred while getting the effective features from the PEP service " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); throw new FeatureManagementException(msg, e); - } catch (OperationManagementException e) { - String msg = "Error occurred while adding the effective feature to database." + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); - log.error(msg, e); - throw new FeatureManagementException(msg, e); } }