Refactored dynamic-client registration code

4.x.x
harshanl 9 years ago
parent 52e0a43b5e
commit f6c65dd481

@ -25,8 +25,8 @@ import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationExce
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService;
import org.wso2.carbon.dynamic.client.registration.OAuthApplicationInfo; import org.wso2.carbon.dynamic.client.registration.OAuthApplicationInfo;
import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; 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.internal.DynamicClientWebAppRegistrationDataHolder;
import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientRegistrationConstants; import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationConstants;
import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil; import org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -34,24 +34,24 @@ import javax.servlet.ServletContext;
/** /**
* This class contains the logic to handle the OAuth application creation process. * This class contains the logic to handle the OAuth application creation process.
*/ */
public class DynamicRegistrationManager { public class DynamicClientWebAppRegistrationManager {
private static DynamicRegistrationManager dynamicRegistrationManager; private static DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager;
private static final Log log = private static final Log log =
LogFactory.getLog(DynamicRegistrationManager.class); LogFactory.getLog(DynamicClientWebAppRegistrationManager.class);
private DynamicRegistrationManager() { private DynamicClientWebAppRegistrationManager() {
} }
public static DynamicRegistrationManager getInstance() { public static DynamicClientWebAppRegistrationManager getInstance() {
if (dynamicRegistrationManager == null) { if (dynamicClientWebAppRegistrationManager == null) {
synchronized (DynamicRegistrationManager.class) { synchronized (DynamicClientWebAppRegistrationManager.class) {
if (dynamicRegistrationManager == null) { if (dynamicClientWebAppRegistrationManager == null) {
dynamicRegistrationManager = new DynamicRegistrationManager(); dynamicClientWebAppRegistrationManager = new DynamicClientWebAppRegistrationManager();
} }
} }
} }
return dynamicRegistrationManager; return dynamicClientWebAppRegistrationManager;
} }
public OAuthApp registerOAuthApplication(RegistrationProfile registrationProfile) { public OAuthApp registerOAuthApplication(RegistrationProfile registrationProfile) {
@ -60,7 +60,7 @@ public class DynamicRegistrationManager {
} }
if (DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)) { if (DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)) {
DynamicClientRegistrationService dynamicClientRegistrationService = DynamicClientRegistrationService dynamicClientRegistrationService =
DynamicClientRegistrationDataHolder.getInstance() DynamicClientWebAppRegistrationDataHolder.getInstance()
.getDynamicClientRegistrationService(); .getDynamicClientRegistrationService();
try { try {
OAuthApplicationInfo oAuthApplicationInfo = OAuthApplicationInfo oAuthApplicationInfo =
@ -110,9 +110,9 @@ public class DynamicRegistrationManager {
public void initiateDynamicClientRegistrationProcess(StandardContext context) { public void initiateDynamicClientRegistrationProcess(StandardContext context) {
ServletContext servletContext = context.getServletContext(); ServletContext servletContext = context.getServletContext();
String requiredDynamicClientRegistration = servletContext.getInitParameter( String requiredDynamicClientRegistration = servletContext.getInitParameter(
DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG); DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG);
DynamicRegistrationManager dynamicRegistrationManager = DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager =
DynamicRegistrationManager.getInstance(); DynamicClientWebAppRegistrationManager.getInstance();
//Get the application name from web-context //Get the application name from web-context
String webAppName = context.getBaseName(); String webAppName = context.getBaseName();
RegistrationProfile registrationProfile; RegistrationProfile registrationProfile;
@ -121,25 +121,29 @@ public class DynamicRegistrationManager {
if ((requiredDynamicClientRegistration != null) && if ((requiredDynamicClientRegistration != null) &&
(Boolean.parseBoolean(requiredDynamicClientRegistration))) { (Boolean.parseBoolean(requiredDynamicClientRegistration))) {
//Check whether this is an already registered application //Check whether this is an already registered application
if (!dynamicRegistrationManager.isRegisteredOAuthApplication(webAppName)) { if (!dynamicClientWebAppRegistrationManager.isRegisteredOAuthApplication(webAppName)) {
//Construct the RegistrationProfile //Construct the RegistrationProfile
registrationProfile = DynamicClientWebAppRegistrationUtil. registrationProfile = DynamicClientWebAppRegistrationUtil.
constructRegistrationProfile(servletContext, webAppName); constructRegistrationProfile(servletContext, webAppName);
//Register the OAuth application //Register the OAuth application
oAuthApp = dynamicRegistrationManager.registerOAuthApplication( oAuthApp = dynamicClientWebAppRegistrationManager.registerOAuthApplication(
registrationProfile); registrationProfile);
} else {
oAuthApp = dynamicClientWebAppRegistrationManager.getOAuthApplicationData(webAppName);
} }
} else { } else {
//Jaggery apps //Jaggery apps
OAuthSettings oAuthSettings = DynamicClientWebAppRegistrationUtil OAuthSettings oAuthSettings = DynamicClientWebAppRegistrationUtil
.getJaggeryAppOAuthSettings(servletContext); .getJaggeryAppOAuthSettings(servletContext);
if (oAuthSettings.isRequireDynamicClientRegistration()) { if (oAuthSettings.isRequireDynamicClientRegistration()) {
if (!dynamicRegistrationManager.isRegisteredOAuthApplication(webAppName)) { if (!dynamicClientWebAppRegistrationManager.isRegisteredOAuthApplication(webAppName)) {
registrationProfile = DynamicClientWebAppRegistrationUtil registrationProfile = DynamicClientWebAppRegistrationUtil
.constructRegistrationProfile(oAuthSettings, webAppName); .constructRegistrationProfile(oAuthSettings, webAppName);
oAuthApp = dynamicRegistrationManager oAuthApp = dynamicClientWebAppRegistrationManager
.registerOAuthApplication(registrationProfile); .registerOAuthApplication(registrationProfile);
} else {
oAuthApp = dynamicClientWebAppRegistrationManager.getOAuthApplicationData(webAppName);
} }
} }
} }

@ -26,20 +26,20 @@ import org.wso2.carbon.utils.ConfigurationContextService;
/** /**
* Dataholder class of DynamicClient Webapp Registration component. * Dataholder class of DynamicClient Webapp Registration component.
*/ */
public class DynamicClientRegistrationDataHolder { public class DynamicClientWebAppRegistrationDataHolder {
private RealmService realmService; private RealmService realmService;
private RegistryService registryService; private RegistryService registryService;
private DynamicClientRegistrationService dynamicClientRegistrationService; private DynamicClientRegistrationService dynamicClientRegistrationService;
private ConfigurationContextService configurationContextService; private ConfigurationContextService configurationContextService;
private static DynamicClientRegistrationDataHolder thisInstance = private static DynamicClientWebAppRegistrationDataHolder thisInstance =
new DynamicClientRegistrationDataHolder(); new DynamicClientWebAppRegistrationDataHolder();
private DynamicClientRegistrationDataHolder() { private DynamicClientWebAppRegistrationDataHolder() {
} }
public static DynamicClientRegistrationDataHolder getInstance() { public static DynamicClientWebAppRegistrationDataHolder getInstance() {
return thisInstance; return thisInstance;
} }

@ -76,7 +76,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting Realm Service"); log.debug("Setting Realm Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setRealmService(realmService); DynamicClientWebAppRegistrationDataHolder.getInstance().setRealmService(realmService);
} }
/** /**
@ -88,7 +88,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Unsetting Realm Service"); log.debug("Unsetting Realm Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setRealmService(null); DynamicClientWebAppRegistrationDataHolder.getInstance().setRealmService(null);
} }
/** /**
@ -100,7 +100,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting Registry Service"); log.debug("Setting Registry Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setRegistryService(registryService); DynamicClientWebAppRegistrationDataHolder.getInstance().setRegistryService(registryService);
} }
/** /**
@ -112,7 +112,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Un setting Registry Service"); log.debug("Un setting Registry Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setRegistryService(null); DynamicClientWebAppRegistrationDataHolder.getInstance().setRegistryService(null);
} }
/** /**
@ -124,7 +124,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting Dynamic Client Registration Service"); log.debug("Setting Dynamic Client Registration Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setDynamicClientRegistrationService( DynamicClientWebAppRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(
dynamicClientRegistrationService); dynamicClientRegistrationService);
} }
@ -137,7 +137,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Un setting Dynamic Client Registration Service"); log.debug("Un setting Dynamic Client Registration Service");
} }
DynamicClientRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(null); DynamicClientWebAppRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(null);
} }
/** /**
@ -149,7 +149,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting ConfigurationContextService"); log.debug("Setting ConfigurationContextService");
} }
DynamicClientRegistrationDataHolder.getInstance().setConfigurationContextService(configurationContextService); DynamicClientWebAppRegistrationDataHolder.getInstance().setConfigurationContextService(configurationContextService);
} }
/** /**
@ -161,7 +161,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Un-setting ConfigurationContextService"); log.debug("Un-setting ConfigurationContextService");
} }
DynamicClientRegistrationDataHolder.getInstance().setConfigurationContextService(null); DynamicClientWebAppRegistrationDataHolder.getInstance().setConfigurationContextService(null);
} }
} }

@ -24,7 +24,7 @@ import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.StandardContext; import org.apache.catalina.core.StandardContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.dynamic.client.web.app.registration.DynamicRegistrationManager; import org.wso2.carbon.dynamic.client.web.app.registration.DynamicClientWebAppRegistrationManager;
/** /**
* This class initiates the dynamic client registration flow for Web applications upon on deployment * This class initiates the dynamic client registration flow for Web applications upon on deployment
@ -40,7 +40,7 @@ public class DynamicClientWebAppDeploymentLifecycleListener implements Lifecycle
public void lifecycleEvent(LifecycleEvent lifecycleEvent) { public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
DynamicRegistrationManager.getInstance().initiateDynamicClientRegistrationProcess( DynamicClientWebAppRegistrationManager.getInstance().initiateDynamicClientRegistrationProcess(
context); context);
} }
} }

@ -21,7 +21,7 @@ package org.wso2.carbon.dynamic.client.web.app.registration.util;
/** /**
* Holds the constants to be used in Dynamic client web app registration component. * Holds the constants to be used in Dynamic client web app registration component.
*/ */
public class DynamicClientRegistrationConstants { public class DynamicClientWebAppRegistrationConstants {
public final static String OAUTH_APP_DATA_REGISTRY_PATH = "/OAuth"; public final static String OAUTH_APP_DATA_REGISTRY_PATH = "/OAuth";
public final static String OAUTH_APP_NAME = "appName"; public final static String OAUTH_APP_NAME = "appName";

@ -26,7 +26,7 @@ import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationExce
import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; 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.OAuthApp;
import org.wso2.carbon.dynamic.client.web.app.registration.OAuthSettings; 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.dynamic.client.web.app.registration.internal.DynamicClientWebAppRegistrationDataHolder;
import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Registry;
@ -62,7 +62,7 @@ public class DynamicClientWebAppRegistrationUtil {
public static Registry getGovernanceRegistry() throws DynamicClientRegistrationException { public static Registry getGovernanceRegistry() throws DynamicClientRegistrationException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
return DynamicClientRegistrationDataHolder.getInstance().getRegistryService() return DynamicClientWebAppRegistrationDataHolder.getInstance().getRegistryService()
.getGovernanceSystemRegistry( .getGovernanceSystemRegistry(
tenantId); tenantId);
} catch (RegistryException e) { } catch (RegistryException e) {
@ -76,7 +76,7 @@ public class DynamicClientWebAppRegistrationUtil {
throws DynamicClientRegistrationException { throws DynamicClientRegistrationException {
Resource resource; Resource resource;
String resourcePath = String resourcePath =
DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName; DynamicClientWebAppRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName;
try { try {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Retrieving OAuth application " + appName + " data from Registry"); log.debug("Retrieving OAuth application " + appName + " data from Registry");
@ -88,7 +88,7 @@ public class DynamicClientWebAppRegistrationUtil {
return (OAuthApp) unmarshaller.unmarshal( return (OAuthApp) unmarshaller.unmarshal(
new StringReader(new String((byte[]) resource.getContent(), Charset new StringReader(new String((byte[]) resource.getContent(), Charset
.forName( .forName(
DynamicClientRegistrationConstants.CharSets.CHARSET_UTF8)))); DynamicClientWebAppRegistrationConstants.CharSets.CHARSET_UTF8))));
} }
return new OAuthApp(); return new OAuthApp();
} catch (JAXBException e) { } catch (JAXBException e) {
@ -116,9 +116,9 @@ public class DynamicClientWebAppRegistrationUtil {
Resource resource = Resource resource =
DynamicClientWebAppRegistrationUtil.getGovernanceRegistry().newResource(); DynamicClientWebAppRegistrationUtil.getGovernanceRegistry().newResource();
resource.setContent(writer.toString()); resource.setContent(writer.toString());
resource.setMediaType(DynamicClientRegistrationConstants.ContentTypes.MEDIA_TYPE_XML); resource.setMediaType(DynamicClientWebAppRegistrationConstants.ContentTypes.MEDIA_TYPE_XML);
String resourcePath = String resourcePath =
DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + DynamicClientWebAppRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" +
oAuthApp.getWebAppName(); oAuthApp.getWebAppName();
status = status =
DynamicClientWebAppRegistrationUtil.putRegistryResource(resourcePath, resource); DynamicClientWebAppRegistrationUtil.putRegistryResource(resourcePath, resource);
@ -172,7 +172,7 @@ public class DynamicClientWebAppRegistrationUtil {
public static String getUserName() { public static String getUserName() {
String username = ""; String username = "";
RealmService realmService = RealmService realmService =
DynamicClientRegistrationDataHolder.getInstance().getRealmService(); DynamicClientWebAppRegistrationDataHolder.getInstance().getRealmService();
if (realmService != null) { if (realmService != null) {
username = realmService.getBootstrapRealmConfiguration().getAdminUserName(); username = realmService.getBootstrapRealmConfiguration().getAdminUserName();
} }
@ -252,7 +252,7 @@ public class DynamicClientWebAppRegistrationUtil {
while (reader.hasNext()) { while (reader.hasNext()) {
String key = reader.nextName(); String key = reader.nextName();
switch (key) { switch (key) {
case DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG: case DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG:
oAuthSettings.setRequireDynamicClientRegistration(reader.nextBoolean()); oAuthSettings.setRequireDynamicClientRegistration(reader.nextBoolean());
break; break;
case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE: case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE:
@ -289,7 +289,7 @@ public class DynamicClientWebAppRegistrationUtil {
// HTTPS port // HTTPS port
String mgtConsoleTransport = CarbonUtils.getManagementTransport(); String mgtConsoleTransport = CarbonUtils.getManagementTransport();
ConfigurationContextService configContextService = ConfigurationContextService configContextService =
DynamicClientRegistrationDataHolder.getInstance().getConfigurationContextService(); DynamicClientWebAppRegistrationDataHolder.getInstance().getConfigurationContextService();
int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport); int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
int httpsProxyPort = int httpsProxyPort =
CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
@ -310,9 +310,9 @@ public class DynamicClientWebAppRegistrationUtil {
//Check for client credentials //Check for client credentials
if ((oAuthApp.getClientKey() != null && !oAuthApp.getClientKey().isEmpty()) && if ((oAuthApp.getClientKey() != null && !oAuthApp.getClientKey().isEmpty()) &&
(oAuthApp.getClientSecret() != null && !oAuthApp.getClientSecret().isEmpty())) { (oAuthApp.getClientSecret() != null && !oAuthApp.getClientSecret().isEmpty())) {
servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_KEY, servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_KEY,
oAuthApp.getClientKey()); oAuthApp.getClientKey());
servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_SECRET, servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_SECRET,
oAuthApp.getClientSecret()); oAuthApp.getClientSecret());
} else { } else {
log.warn("Client credentials not found for web app : " + oAuthApp.getWebAppName()); log.warn("Client credentials not found for web app : " + oAuthApp.getWebAppName());

Loading…
Cancel
Save