|
|
@ -19,17 +19,21 @@
|
|
|
|
package org.wso2.carbon.dynamic.client.web.app.registration;
|
|
|
|
package org.wso2.carbon.dynamic.client.web.app.registration;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.catalina.core.StandardContext;
|
|
|
|
import org.apache.catalina.core.StandardContext;
|
|
|
|
|
|
|
|
import org.apache.commons.collections.iterators.IteratorEnumeration;
|
|
|
|
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.registration.DynamicClientRegistrationException;
|
|
|
|
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException;
|
|
|
|
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.dto.OAuthAppDetails;
|
|
|
|
|
|
|
|
import org.wso2.carbon.dynamic.client.web.app.registration.dto.JaggeryOAuthConfigurationSettings;
|
|
|
|
import org.wso2.carbon.dynamic.client.web.app.registration.internal.DynamicClientWebAppRegistrationDataHolder;
|
|
|
|
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.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;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This class contains the logic to handle the OAuth application creation process.
|
|
|
|
* This class contains the logic to handle the OAuth application creation process.
|
|
|
@ -37,6 +41,9 @@ import javax.servlet.ServletContext;
|
|
|
|
public class DynamicClientWebAppRegistrationManager {
|
|
|
|
public class DynamicClientWebAppRegistrationManager {
|
|
|
|
|
|
|
|
|
|
|
|
private static DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager;
|
|
|
|
private static DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager;
|
|
|
|
|
|
|
|
private static List<RegistrationProfile> registrationProfileList = new ArrayList<>();
|
|
|
|
|
|
|
|
private static Map<String, ServletContext> webAppContexts = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log =
|
|
|
|
private static final Log log =
|
|
|
|
LogFactory.getLog(DynamicClientWebAppRegistrationManager.class);
|
|
|
|
LogFactory.getLog(DynamicClientWebAppRegistrationManager.class);
|
|
|
|
|
|
|
|
|
|
|
@ -47,108 +54,132 @@ public class DynamicClientWebAppRegistrationManager {
|
|
|
|
if (dynamicClientWebAppRegistrationManager == null) {
|
|
|
|
if (dynamicClientWebAppRegistrationManager == null) {
|
|
|
|
synchronized (DynamicClientWebAppRegistrationManager.class) {
|
|
|
|
synchronized (DynamicClientWebAppRegistrationManager.class) {
|
|
|
|
if (dynamicClientWebAppRegistrationManager == null) {
|
|
|
|
if (dynamicClientWebAppRegistrationManager == null) {
|
|
|
|
dynamicClientWebAppRegistrationManager = new DynamicClientWebAppRegistrationManager();
|
|
|
|
dynamicClientWebAppRegistrationManager =
|
|
|
|
|
|
|
|
new DynamicClientWebAppRegistrationManager();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dynamicClientWebAppRegistrationManager;
|
|
|
|
return dynamicClientWebAppRegistrationManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public OAuthApp registerOAuthApplication(RegistrationProfile registrationProfile) {
|
|
|
|
public OAuthAppDetails registerOAuthApplication(RegistrationProfile registrationProfile) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Registering OAuth application for web app : " + registrationProfile.getClientName());
|
|
|
|
log.debug("Registering OAuth application for web app : " +
|
|
|
|
|
|
|
|
registrationProfile.getClientName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)) {
|
|
|
|
if (DynamicClientWebAppRegistrationUtil.validateRegistrationProfile(registrationProfile)) {
|
|
|
|
DynamicClientRegistrationService dynamicClientRegistrationService =
|
|
|
|
DynamicClientRegistrationService dynamicClientRegistrationService =
|
|
|
|
DynamicClientWebAppRegistrationDataHolder.getInstance()
|
|
|
|
DynamicClientWebAppRegistrationDataHolder.getInstance()
|
|
|
|
.getDynamicClientRegistrationService();
|
|
|
|
.getDynamicClientRegistrationService();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
OAuthApplicationInfo oAuthApplicationInfo =
|
|
|
|
OAuthApplicationInfo oAuthApplicationInfo =
|
|
|
|
dynamicClientRegistrationService
|
|
|
|
dynamicClientRegistrationService
|
|
|
|
.registerOAuthApplication(registrationProfile);
|
|
|
|
.registerOAuthApplication(registrationProfile);
|
|
|
|
OAuthApp oAuthApp = new OAuthApp();
|
|
|
|
OAuthAppDetails oAuthAppDetails = new OAuthAppDetails();
|
|
|
|
oAuthApp.setWebAppName(registrationProfile.getClientName());
|
|
|
|
oAuthAppDetails.setWebAppName(registrationProfile.getClientName());
|
|
|
|
oAuthApp.setClientName(oAuthApplicationInfo.getClientName());
|
|
|
|
oAuthAppDetails.setClientName(oAuthApplicationInfo.getClientName());
|
|
|
|
oAuthApp.setClientKey(oAuthApplicationInfo.getClientId());
|
|
|
|
oAuthAppDetails.setClientKey(oAuthApplicationInfo.getClientId());
|
|
|
|
oAuthApp.setClientSecret(oAuthApplicationInfo.getClientSecret());
|
|
|
|
oAuthAppDetails.setClientSecret(oAuthApplicationInfo.getClientSecret());
|
|
|
|
//store it in registry
|
|
|
|
//store it in registry
|
|
|
|
if (DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthApp)) {
|
|
|
|
if (DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthAppDetails)) {
|
|
|
|
return oAuthApp;
|
|
|
|
return oAuthAppDetails;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
dynamicClientRegistrationService
|
|
|
|
dynamicClientRegistrationService
|
|
|
|
.unregisterOAuthApplication(registrationProfile.getOwner(),
|
|
|
|
.unregisterOAuthApplication(registrationProfile.getOwner(),
|
|
|
|
oAuthApplicationInfo.getClientName(),
|
|
|
|
oAuthApplicationInfo.getClientName(),
|
|
|
|
oAuthApplicationInfo.getClientId());
|
|
|
|
oAuthApplicationInfo.getClientId());
|
|
|
|
log.warn("Error occurred while persisting the OAuth application data in registry.");
|
|
|
|
log.warn(
|
|
|
|
|
|
|
|
"Error occurred while persisting the OAuth application data in registry.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DynamicClientRegistrationException e) {
|
|
|
|
} catch (DynamicClientRegistrationException e) {
|
|
|
|
log.error("Error occurred while registering the OAuth application : " +
|
|
|
|
log.error("Error occurred while registering the OAuth application : " +
|
|
|
|
registrationProfile.getClientName(), e);
|
|
|
|
registrationProfile.getClientName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new OAuthApp();
|
|
|
|
return new OAuthAppDetails();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public OAuthApp getOAuthApplicationData(String clientName) {
|
|
|
|
public OAuthAppDetails getOAuthApplicationData(String clientName) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return DynamicClientWebAppRegistrationUtil.getOAuthApplicationData(clientName);
|
|
|
|
return DynamicClientWebAppRegistrationUtil.getOAuthApplicationData(clientName);
|
|
|
|
} catch (DynamicClientRegistrationException e) {
|
|
|
|
} catch (DynamicClientRegistrationException e) {
|
|
|
|
log.error("Error occurred while fetching the OAuth application data for web app : " +
|
|
|
|
log.error("Error occurred while fetching the OAuth application data for web app : " +
|
|
|
|
clientName, e);
|
|
|
|
clientName, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new OAuthApp();
|
|
|
|
return new OAuthAppDetails();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isRegisteredOAuthApplication(String clientName) {
|
|
|
|
public boolean isRegisteredOAuthApplication(String clientName) {
|
|
|
|
OAuthApp oAuthApp = this.getOAuthApplicationData(clientName);
|
|
|
|
OAuthAppDetails oAuthAppDetails = this.getOAuthApplicationData(clientName);
|
|
|
|
if (oAuthApp.getClientKey() != null && oAuthApp.getClientSecret() != null) {
|
|
|
|
if (oAuthAppDetails.getClientKey() != null && oAuthAppDetails.getClientSecret() != null) {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void initiateDynamicClientRegistrationProcess(StandardContext context) {
|
|
|
|
public void saveServletContextToCache(StandardContext context) {
|
|
|
|
ServletContext servletContext = context.getServletContext();
|
|
|
|
DynamicClientWebAppRegistrationManager.webAppContexts.put(context.getBaseName(),
|
|
|
|
String requiredDynamicClientRegistration = servletContext.getInitParameter(
|
|
|
|
context.getServletContext());
|
|
|
|
DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void initiateDynamicClientRegistration() {
|
|
|
|
|
|
|
|
String requiredDynamicClientRegistration, webAppName;
|
|
|
|
|
|
|
|
ServletContext servletContext;
|
|
|
|
|
|
|
|
RegistrationProfile registrationProfile;
|
|
|
|
|
|
|
|
OAuthAppDetails oAuthAppDetails = new OAuthAppDetails();
|
|
|
|
DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager =
|
|
|
|
DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager =
|
|
|
|
DynamicClientWebAppRegistrationManager.getInstance();
|
|
|
|
DynamicClientWebAppRegistrationManager.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 (!dynamicClientWebAppRegistrationManager.isRegisteredOAuthApplication(webAppName)) {
|
|
|
|
|
|
|
|
//Construct the RegistrationProfile
|
|
|
|
|
|
|
|
registrationProfile = DynamicClientWebAppRegistrationUtil.
|
|
|
|
|
|
|
|
constructRegistrationProfile(servletContext, webAppName);
|
|
|
|
|
|
|
|
//Register the OAuth application
|
|
|
|
|
|
|
|
oAuthApp = dynamicClientWebAppRegistrationManager.registerOAuthApplication(
|
|
|
|
|
|
|
|
registrationProfile);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Enumeration enumeration = new IteratorEnumeration(DynamicClientWebAppRegistrationManager.
|
|
|
|
oAuthApp = dynamicClientWebAppRegistrationManager.getOAuthApplicationData(webAppName);
|
|
|
|
webAppContexts.keySet().iterator());
|
|
|
|
}
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
} else {
|
|
|
|
log.debug("Initiating the DynamicClientRegistration service for web-apps");
|
|
|
|
//Jaggery apps
|
|
|
|
}
|
|
|
|
OAuthSettings oAuthSettings = DynamicClientWebAppRegistrationUtil
|
|
|
|
while (enumeration.hasMoreElements()){
|
|
|
|
.getJaggeryAppOAuthSettings(servletContext);
|
|
|
|
webAppName = (String) enumeration.nextElement();
|
|
|
|
if (oAuthSettings.isRequireDynamicClientRegistration()) {
|
|
|
|
servletContext = DynamicClientWebAppRegistrationManager.webAppContexts.get(webAppName);
|
|
|
|
|
|
|
|
requiredDynamicClientRegistration = servletContext.getInitParameter(
|
|
|
|
|
|
|
|
DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG);
|
|
|
|
|
|
|
|
//Java web-app section
|
|
|
|
|
|
|
|
if ((requiredDynamicClientRegistration != null) &&
|
|
|
|
|
|
|
|
(Boolean.parseBoolean(requiredDynamicClientRegistration))) {
|
|
|
|
|
|
|
|
//Check whether this is an already registered application
|
|
|
|
if (!dynamicClientWebAppRegistrationManager.isRegisteredOAuthApplication(webAppName)) {
|
|
|
|
if (!dynamicClientWebAppRegistrationManager.isRegisteredOAuthApplication(webAppName)) {
|
|
|
|
registrationProfile = DynamicClientWebAppRegistrationUtil
|
|
|
|
//Construct the RegistrationProfile
|
|
|
|
.constructRegistrationProfile(oAuthSettings, webAppName);
|
|
|
|
registrationProfile = DynamicClientWebAppRegistrationUtil.
|
|
|
|
oAuthApp = dynamicClientWebAppRegistrationManager
|
|
|
|
constructRegistrationProfile(servletContext,
|
|
|
|
.registerOAuthApplication(registrationProfile);
|
|
|
|
webAppName);
|
|
|
|
|
|
|
|
//Register the OAuth application
|
|
|
|
|
|
|
|
oAuthAppDetails = dynamicClientWebAppRegistrationManager.registerOAuthApplication(
|
|
|
|
|
|
|
|
registrationProfile);
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
oAuthApp = dynamicClientWebAppRegistrationManager.getOAuthApplicationData(webAppName);
|
|
|
|
oAuthAppDetails =
|
|
|
|
|
|
|
|
dynamicClientWebAppRegistrationManager.getOAuthApplicationData(webAppName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (requiredDynamicClientRegistration == null) {
|
|
|
|
|
|
|
|
//Jaggery apps
|
|
|
|
|
|
|
|
JaggeryOAuthConfigurationSettings jaggeryOAuthConfigurationSettings = DynamicClientWebAppRegistrationUtil
|
|
|
|
|
|
|
|
.getJaggeryAppOAuthSettings(servletContext);
|
|
|
|
|
|
|
|
if (jaggeryOAuthConfigurationSettings.isRequireDynamicClientRegistration()) {
|
|
|
|
|
|
|
|
if (!dynamicClientWebAppRegistrationManager
|
|
|
|
|
|
|
|
.isRegisteredOAuthApplication(webAppName)) {
|
|
|
|
|
|
|
|
registrationProfile = DynamicClientWebAppRegistrationUtil
|
|
|
|
|
|
|
|
.constructRegistrationProfile(jaggeryOAuthConfigurationSettings, webAppName);
|
|
|
|
|
|
|
|
oAuthAppDetails = dynamicClientWebAppRegistrationManager
|
|
|
|
|
|
|
|
.registerOAuthApplication(registrationProfile);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
oAuthAppDetails = dynamicClientWebAppRegistrationManager
|
|
|
|
|
|
|
|
.getOAuthApplicationData(webAppName);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add client credentials to the web-context
|
|
|
|
|
|
|
|
if (oAuthAppDetails.getClientKey() != null) {
|
|
|
|
|
|
|
|
DynamicClientWebAppRegistrationUtil.addClientCredentialsToWebContext(oAuthAppDetails,
|
|
|
|
|
|
|
|
servletContext);
|
|
|
|
|
|
|
|
log.info("Added OAuth application credentials to webapp context of webapp : " + webAppName);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DynamicClientWebAppRegistrationUtil.addClientCredentialsToWebContext(oAuthApp,
|
|
|
|
|
|
|
|
servletContext);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|