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

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

@ -76,7 +76,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Setting Realm Service");
}
DynamicClientRegistrationDataHolder.getInstance().setRealmService(realmService);
DynamicClientWebAppRegistrationDataHolder.getInstance().setRealmService(realmService);
}
/**
@ -88,7 +88,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Unsetting Realm Service");
}
DynamicClientRegistrationDataHolder.getInstance().setRealmService(null);
DynamicClientWebAppRegistrationDataHolder.getInstance().setRealmService(null);
}
/**
@ -100,7 +100,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Setting Registry Service");
}
DynamicClientRegistrationDataHolder.getInstance().setRegistryService(registryService);
DynamicClientWebAppRegistrationDataHolder.getInstance().setRegistryService(registryService);
}
/**
@ -112,7 +112,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Un setting Registry Service");
}
DynamicClientRegistrationDataHolder.getInstance().setRegistryService(null);
DynamicClientWebAppRegistrationDataHolder.getInstance().setRegistryService(null);
}
/**
@ -124,7 +124,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Setting Dynamic Client Registration Service");
}
DynamicClientRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(
DynamicClientWebAppRegistrationDataHolder.getInstance().setDynamicClientRegistrationService(
dynamicClientRegistrationService);
}
@ -137,7 +137,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
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()) {
log.debug("Setting ConfigurationContextService");
}
DynamicClientRegistrationDataHolder.getInstance().setConfigurationContextService(configurationContextService);
DynamicClientWebAppRegistrationDataHolder.getInstance().setConfigurationContextService(configurationContextService);
}
/**
@ -161,7 +161,7 @@ public class DynamicClientWebAppRegistrationServiceComponent {
if (log.isDebugEnabled()) {
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.commons.logging.Log;
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
@ -40,7 +40,7 @@ public class DynamicClientWebAppDeploymentLifecycleListener implements Lifecycle
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
DynamicRegistrationManager.getInstance().initiateDynamicClientRegistrationProcess(
DynamicClientWebAppRegistrationManager.getInstance().initiateDynamicClientRegistrationProcess(
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.
*/
public class DynamicClientRegistrationConstants {
public class DynamicClientWebAppRegistrationConstants {
public final static String OAUTH_APP_DATA_REGISTRY_PATH = "/OAuth";
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.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.dynamic.client.web.app.registration.internal.DynamicClientWebAppRegistrationDataHolder;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.Registry;
@ -62,7 +62,7 @@ public class DynamicClientWebAppRegistrationUtil {
public static Registry getGovernanceRegistry() throws DynamicClientRegistrationException {
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
return DynamicClientRegistrationDataHolder.getInstance().getRegistryService()
return DynamicClientWebAppRegistrationDataHolder.getInstance().getRegistryService()
.getGovernanceSystemRegistry(
tenantId);
} catch (RegistryException e) {
@ -76,7 +76,7 @@ public class DynamicClientWebAppRegistrationUtil {
throws DynamicClientRegistrationException {
Resource resource;
String resourcePath =
DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName;
DynamicClientWebAppRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" + appName;
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving OAuth application " + appName + " data from Registry");
@ -88,7 +88,7 @@ public class DynamicClientWebAppRegistrationUtil {
return (OAuthApp) unmarshaller.unmarshal(
new StringReader(new String((byte[]) resource.getContent(), Charset
.forName(
DynamicClientRegistrationConstants.CharSets.CHARSET_UTF8))));
DynamicClientWebAppRegistrationConstants.CharSets.CHARSET_UTF8))));
}
return new OAuthApp();
} catch (JAXBException e) {
@ -116,9 +116,9 @@ public class DynamicClientWebAppRegistrationUtil {
Resource resource =
DynamicClientWebAppRegistrationUtil.getGovernanceRegistry().newResource();
resource.setContent(writer.toString());
resource.setMediaType(DynamicClientRegistrationConstants.ContentTypes.MEDIA_TYPE_XML);
resource.setMediaType(DynamicClientWebAppRegistrationConstants.ContentTypes.MEDIA_TYPE_XML);
String resourcePath =
DynamicClientRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" +
DynamicClientWebAppRegistrationConstants.OAUTH_APP_DATA_REGISTRY_PATH + "/" +
oAuthApp.getWebAppName();
status =
DynamicClientWebAppRegistrationUtil.putRegistryResource(resourcePath, resource);
@ -172,7 +172,7 @@ public class DynamicClientWebAppRegistrationUtil {
public static String getUserName() {
String username = "";
RealmService realmService =
DynamicClientRegistrationDataHolder.getInstance().getRealmService();
DynamicClientWebAppRegistrationDataHolder.getInstance().getRealmService();
if (realmService != null) {
username = realmService.getBootstrapRealmConfiguration().getAdminUserName();
}
@ -252,7 +252,7 @@ public class DynamicClientWebAppRegistrationUtil {
while (reader.hasNext()) {
String key = reader.nextName();
switch (key) {
case DynamicClientRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG:
case DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG:
oAuthSettings.setRequireDynamicClientRegistration(reader.nextBoolean());
break;
case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE:
@ -289,7 +289,7 @@ public class DynamicClientWebAppRegistrationUtil {
// HTTPS port
String mgtConsoleTransport = CarbonUtils.getManagementTransport();
ConfigurationContextService configContextService =
DynamicClientRegistrationDataHolder.getInstance().getConfigurationContextService();
DynamicClientWebAppRegistrationDataHolder.getInstance().getConfigurationContextService();
int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
int httpsProxyPort =
CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
@ -310,9 +310,9 @@ public class DynamicClientWebAppRegistrationUtil {
//Check for client credentials
if ((oAuthApp.getClientKey() != null && !oAuthApp.getClientKey().isEmpty()) &&
(oAuthApp.getClientSecret() != null && !oAuthApp.getClientSecret().isEmpty())) {
servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_KEY,
servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_KEY,
oAuthApp.getClientKey());
servletContext.setAttribute(DynamicClientRegistrationConstants.OAUTH_CLIENT_SECRET,
servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_SECRET,
oAuthApp.getClientSecret());
} else {
log.warn("Client credentials not found for web app : " + oAuthApp.getWebAppName());

Loading…
Cancel
Save