Refactored dynamic-client registration

revert-70aa11f8
harshanl 9 years ago
parent 3445c49225
commit cb9616b02b

@ -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);
}
}

@ -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 {

@ -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;
}
}

@ -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;
}
}

@ -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);
}
}

@ -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);
}
}
}

@ -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() {

@ -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());
}
}
}
}
Loading…
Cancel
Save