Refactored dynamic-client module to use remote services.

merge-requests/7/head
harshanl 9 years ago
parent bdbbca5a18
commit 89e6f0786d

@ -1,33 +0,0 @@
/*
* 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;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/connect")
public interface ConfigurationService {
@GET
@Path("/register")
Response getProfile(@PathParam("client_id") String clientId);
}

@ -89,7 +89,7 @@ public class RegistrationServiceImpl implements RegistrationService {
boolean status = dynamicClientRegistrationService.unregisterOAuthApplication(userId, applicationName, boolean status = dynamicClientRegistrationService.unregisterOAuthApplication(userId, applicationName,
consumerKey); consumerKey);
if (status) { if (status) {
return Response.status(Response.Status.ACCEPTED).build(); return Response.status(Response.Status.OK).build();
} }
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }

@ -66,6 +66,7 @@
org.wso2.carbon.identity.application.common, org.wso2.carbon.identity.application.common,
org.wso2.carbon.identity.application.common.model, org.wso2.carbon.identity.application.common.model,
org.wso2.carbon.identity.application.mgt, org.wso2.carbon.identity.application.mgt,
org.wso2.carbon.identity.application.mgt.stub,
org.wso2.carbon.identity.base, org.wso2.carbon.identity.base,
org.wso2.carbon.identity.oauth, org.wso2.carbon.identity.oauth,
org.wso2.carbon.identity.oauth.dto, org.wso2.carbon.identity.oauth.dto,

@ -18,6 +18,7 @@
package org.wso2.carbon.dynamic.client.registration; package org.wso2.carbon.dynamic.client.registration;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.wso2.carbon.dynamic.client.registration.util.DCRConstants;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -87,10 +88,10 @@ public class OAuthApplicationInfo {
public String toString() { public String toString() {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_ID, this.getClientId()); obj.put(DCRConstants.ClientMetadata.OAUTH_CLIENT_ID, this.getClientId());
obj.put(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_NAME, this.getClientName()); obj.put(DCRConstants.ClientMetadata.OAUTH_CLIENT_NAME, this.getClientName());
obj.put(ApplicationConstants.ClientMetadata.OAUTH_CALLBACK_URIS, this.getCallBackURL()); obj.put(DCRConstants.ClientMetadata.OAUTH_CALLBACK_URIS, this.getCallBackURL());
obj.put(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_SECRET, this.getClientSecret()); obj.put(DCRConstants.ClientMetadata.OAUTH_CLIENT_SECRET, this.getClientSecret());
return obj.toString(); return obj.toString();
} }

@ -27,6 +27,8 @@ import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.dynamic.client.registration.*; import org.wso2.carbon.dynamic.client.registration.*;
import org.wso2.carbon.dynamic.client.registration.internal.DynamicClientRegistrationDataHolder; import org.wso2.carbon.dynamic.client.registration.internal.DynamicClientRegistrationDataHolder;
import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile;
import org.wso2.carbon.dynamic.client.registration.util.DCRConstants;
import org.wso2.carbon.dynamic.client.registration.util.DynamicClientRegistrationUtil;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.*; import org.wso2.carbon.identity.application.common.model.*;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
@ -94,15 +96,15 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
try { try {
JSONObject jsonObject = new JSONObject(info.getJsonString()); JSONObject jsonObject = new JSONObject(info.getJsonString());
if (jsonObject.has(ApplicationConstants.ClientMetadata.OAUTH_REDIRECT_URIS)) { if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS)) {
oAuthApplicationInfo.addParameter(ApplicationConstants.ClientMetadata.OAUTH_REDIRECT_URIS, oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS,
jsonObject jsonObject
.get(ApplicationConstants.ClientMetadata.OAUTH_REDIRECT_URIS)); .get(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS));
} }
if (jsonObject.has(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_GRANT)) { if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT)) {
oAuthApplicationInfo.addParameter(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_GRANT, jsonObject oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT, jsonObject
.get(ApplicationConstants.ClientMetadata.OAUTH_CLIENT_GRANT)); .get(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT));
} }
} catch (JSONException e) { } catch (JSONException e) {
throw new DynamicClientRegistrationException( throw new DynamicClientRegistrationException(
@ -160,7 +162,6 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
} }
ServiceProvider existingServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain); ServiceProvider existingServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain);
if (existingServiceProvider == null) { if (existingServiceProvider == null) {
appMgtService.createApplication(serviceProvider, tenantDomain, userName); appMgtService.createApplication(serviceProvider, tenantDomain, userName);
} }
@ -267,9 +268,9 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
oAuthApplicationInfo.setClientName(createdApp.getApplicationName()); oAuthApplicationInfo.setClientName(createdApp.getApplicationName());
oAuthApplicationInfo.addParameter( oAuthApplicationInfo.addParameter(
ApplicationConstants.ClientMetadata.OAUTH_REDIRECT_URIS, createdApp.getCallbackUrl()); DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS, createdApp.getCallbackUrl());
oAuthApplicationInfo.addParameter( oAuthApplicationInfo.addParameter(
ApplicationConstants.ClientMetadata.OAUTH_CLIENT_GRANT, createdApp.getGrantTypes()); DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT, createdApp.getGrantTypes());
return oAuthApplicationInfo; return oAuthApplicationInfo;
} catch (IdentityApplicationManagementException e) { } catch (IdentityApplicationManagementException e) {

@ -20,7 +20,6 @@ package org.wso2.carbon.dynamic.client.registration.internal;
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.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService;
import org.wso2.carbon.dynamic.client.registration.impl.DynamicClientRegistrationServiceImpl; import org.wso2.carbon.dynamic.client.registration.impl.DynamicClientRegistrationServiceImpl;
@ -44,10 +43,8 @@ public class DynamicClientRegistrationServiceComponent {
if(log.isDebugEnabled()){ if(log.isDebugEnabled()){
log.debug("Starting DynamicClientRegistrationServiceComponent"); log.debug("Starting DynamicClientRegistrationServiceComponent");
} }
DynamicClientRegistrationService dynamicClientRegistrationService =
new DynamicClientRegistrationServiceImpl();
componentContext.getBundleContext().registerService( componentContext.getBundleContext().registerService(
DynamicClientRegistrationService.class.getName(), dynamicClientRegistrationService, null); DynamicClientRegistrationService.class.getName(), new DynamicClientRegistrationServiceImpl(), null);
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")

@ -15,18 +15,18 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.dynamic.client.registration; package org.wso2.carbon.dynamic.client.registration.util;
/** /**
* This class holds the constants used by DynamicClientRegistration component. * This class holds the constants used by DynamicClientRegistration component.
*/ */
public final class ApplicationConstants { public final class DCRConstants {
public static class ClientMetadata { public static class ClientMetadata {
private ClientMetadata() { private ClientMetadata() {
throw new AssertionError(); throw new AssertionError();
} }
//todo refactor names
public static final String OAUTH_CLIENT_ID = "client_id"; //this means consumer key public static final String OAUTH_CLIENT_ID = "client_id"; //this means consumer key
public static final String OAUTH_CLIENT_SECRET = "client_secret"; public static final String OAUTH_CLIENT_SECRET = "client_secret";
public static final String OAUTH_REDIRECT_URIS = "redirect_uris"; public static final String OAUTH_REDIRECT_URIS = "redirect_uris";
@ -55,4 +55,4 @@ public final class ApplicationConstants {
public static final String VALIDITY_PERIOD = "validityPeriod"; public static final String VALIDITY_PERIOD = "validityPeriod";
} }
} }

@ -15,8 +15,11 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.dynamic.client.registration; package org.wso2.carbon.dynamic.client.registration.util;
/**
* This class holds the util methods used by DCR component.
*/
public class DynamicClientRegistrationUtil { public class DynamicClientRegistrationUtil {
public static void validateUsername(String username) { public static void validateUsername(String username) {
@ -36,5 +39,4 @@ public class DynamicClientRegistrationUtil {
throw new IllegalArgumentException("Consumer Key cannot be null or empty"); throw new IllegalArgumentException("Consumer Key cannot be null or empty");
} }
} }
}
}

@ -115,5 +115,13 @@
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.dynamic.client.registration</artifactId> <artifactId>org.wso2.carbon.dynamic.client.registration</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -52,8 +52,7 @@ public class DynamicClientWebAppRegistrationManager {
if (dynamicClientWebAppRegistrationManager == null) { if (dynamicClientWebAppRegistrationManager == null) {
synchronized (DynamicClientWebAppRegistrationManager.class) { synchronized (DynamicClientWebAppRegistrationManager.class) {
if (dynamicClientWebAppRegistrationManager == null) { if (dynamicClientWebAppRegistrationManager == null) {
dynamicClientWebAppRegistrationManager = dynamicClientWebAppRegistrationManager = new DynamicClientWebAppRegistrationManager();
new DynamicClientWebAppRegistrationManager();
} }
} }
} }
@ -65,11 +64,9 @@ public class DynamicClientWebAppRegistrationManager {
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 =
DynamicClientWebAppRegistrationDataHolder.getInstance().getDynamicClientRegistrationService();
try { try {
OAuthApplicationInfo oAuthApplicationInfo = OAuthApplicationInfo oAuthApplicationInfo =
dynamicClientRegistrationService.registerOAuthApplication(registrationProfile); DynamicClientWebAppRegistrationUtil.registerOAuthApplication(registrationProfile);
OAuthAppDetails oAuthAppDetails = new OAuthAppDetails(); OAuthAppDetails oAuthAppDetails = new OAuthAppDetails();
oAuthAppDetails.setWebAppName(registrationProfile.getClientName()); oAuthAppDetails.setWebAppName(registrationProfile.getClientName());
oAuthAppDetails.setClientName(oAuthApplicationInfo.getClientName()); oAuthAppDetails.setClientName(oAuthApplicationInfo.getClientName());
@ -79,7 +76,7 @@ public class DynamicClientWebAppRegistrationManager {
if (DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthAppDetails)) { if (DynamicClientWebAppRegistrationUtil.putOAuthApplicationData(oAuthAppDetails)) {
return oAuthAppDetails; return oAuthAppDetails;
} else { } else {
dynamicClientRegistrationService.unregisterOAuthApplication(registrationProfile.getOwner(), DynamicClientWebAppRegistrationUtil.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.");

@ -15,18 +15,30 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.dynamic.client.web.impl;
import org.wso2.carbon.dynamic.client.web.ConfigurationService; package org.wso2.carbon.dynamic.client.web.app.registration.dto;
import javax.ws.rs.PathParam; /**
import javax.ws.rs.core.Response; * Represents Configuration setting related to OAuth in authenticators.xml.
*/
public class DynamicClientRegistrationSettings {
private boolean isRemote;
private String host;
public boolean isRemote() {
return isRemote;
}
public class ConfigurationServiceImpl implements ConfigurationService { public void setIsRemote(boolean isRemote) {
this.isRemote = isRemote;
}
@Override public String getHost() {
public Response getProfile(@PathParam("client_id") String clientId) { return host;
return null;
} }
} public void setHost(String host) {
this.host = host;
}
}

@ -46,4 +46,23 @@ public class DynamicClientWebAppRegistrationConstants {
public static final String CHARSET_UTF8 = "UTF8"; public static final String CHARSET_UTF8 = "UTF8";
} }
}
public static class ConfigurationProperties {
private ConfigurationProperties() {
throw new AssertionError();
}
public static final String AUTHENTICATOR_NAME = "OAuthAuthenticator";
public static final String AUTHENTICATOR_CONFIG_IS_REMOTE = "isRemote";
public static final String AUTHENTICATOR_CONFIG_HOST_URL = "hostURL";
}
public static class RemoteServiceProperties {
private RemoteServiceProperties() {
throw new AssertionError();
}
public static final String DYNAMIC_CLIENT_SERVICE_ENDPOINT = "/dynamic-client-web/register";
public static final String DYNAMIC_CLIENT_SERVICE_PROTOCOL = "https";
}
}

@ -22,8 +22,11 @@ import com.google.gson.stream.JsonReader;
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.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.security.AuthenticatorsConfiguration;
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException; import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException;
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.DynamicClientRegistrationSettings;
import org.wso2.carbon.dynamic.client.web.app.registration.dto.OAuthAppDetails; 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.dto.JaggeryOAuthConfigurationSettings;
import org.wso2.carbon.dynamic.client.web.app.registration.internal.DynamicClientWebAppRegistrationDataHolder; import org.wso2.carbon.dynamic.client.web.app.registration.internal.DynamicClientWebAppRegistrationDataHolder;
@ -315,4 +318,65 @@ public class DynamicClientWebAppRegistrationUtil {
public static String replaceInvalidChars(String username) { public static String replaceInvalidChars(String username) {
return username.replaceAll("@","_AT_"); return username.replaceAll("@","_AT_");
} }
private static DynamicClientRegistrationSettings getDynamicClientRegistrationSettings()
throws IllegalArgumentException {
AuthenticatorsConfiguration authenticatorsConfiguration = AuthenticatorsConfiguration.getInstance();
AuthenticatorsConfiguration.AuthenticatorConfig authenticatorConfig = authenticatorsConfiguration.
getAuthenticatorConfig(DynamicClientWebAppRegistrationConstants.
ConfigurationProperties.AUTHENTICATOR_NAME);
DynamicClientRegistrationSettings dynamicClientRegistrationSettings = new DynamicClientRegistrationSettings();
if (authenticatorConfig != null && authenticatorConfig.getParameters() != null) {
dynamicClientRegistrationSettings.setIsRemote(Boolean.parseBoolean(
authenticatorConfig.getParameters().get(
DynamicClientWebAppRegistrationConstants.ConfigurationProperties.
AUTHENTICATOR_CONFIG_IS_REMOTE)));
dynamicClientRegistrationSettings.setHost(authenticatorConfig.getParameters().
get(DynamicClientWebAppRegistrationConstants.ConfigurationProperties.AUTHENTICATOR_CONFIG_HOST_URL));
}else{
throw new IllegalArgumentException("Configuration parameters need to be defined in Authenticators.xml.");
}
return dynamicClientRegistrationSettings;
}
//This method will choose the best DynamicClientRegistrationService based on server configurations and
//registers OAuth client.
public static OAuthApplicationInfo registerOAuthApplication(RegistrationProfile registrationProfile)
throws DynamicClientRegistrationException {
DynamicClientRegistrationSettings dynamicClientRegistrationSettings = getDynamicClientRegistrationSettings();
if (dynamicClientRegistrationSettings.isRemote()) {
return RemoteDCRClient.createOAuthApplication(registrationProfile,
getHostName(dynamicClientRegistrationSettings.getHost()));
} else {
return DynamicClientWebAppRegistrationDataHolder.getInstance().
getDynamicClientRegistrationService().registerOAuthApplication(registrationProfile);
}
}
//This method will choose the best DynamicClientRegistrationService based on server configurations and
//unregisters OAuth client.
public static boolean unregisterOAuthApplication(String owner, String clientName, String clientId)
throws DynamicClientRegistrationException {
DynamicClientRegistrationSettings dynamicClientRegistrationSettings = getDynamicClientRegistrationSettings();
if (dynamicClientRegistrationSettings.isRemote()) {
return RemoteDCRClient.deleteOAuthApplication(owner, clientName, clientId,
getHostName(dynamicClientRegistrationSettings.getHost()));
} else {
return DynamicClientWebAppRegistrationDataHolder.getInstance().
getDynamicClientRegistrationService().unregisterOAuthApplication(owner, clientName, clientId);
}
}
private static String getHostName(String host) {
if (host != null && !host.isEmpty()) {
if (host.contains("https://")) {
return host.replace("https://","");
}
} else {
throw new IllegalArgumentException("Remote Host parameter must defined in Authenticators.xml.");
}
return null;
}
} }

@ -0,0 +1,182 @@
/*
* 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.util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.util.EntityUtils;
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException;
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.DynamicClientWebAppRegistrationDataHolder;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* This class holds the necessary logic to create and delete service-providers by invoking the
* dynamic-client-registration endpoint.
*/
public class RemoteDCRClient {
private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
private static final String CHARSET_UTF_8 = "UTF-8";
public static OAuthApplicationInfo createOAuthApplication(RegistrationProfile registrationProfile, String host)
throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient();
String clientName = registrationProfile.getClientName();
try {
// Setup the HTTPS settings to accept any certificate.
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL, socketFactory, getServerHTTPSPort()));
SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry);
httpClient = new DefaultHttpClient(mgr, httpClient.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
URI uri = new URIBuilder().setScheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT).build();
Gson gson = new Gson();
StringEntity entity = new StringEntity(gson.toJson(registrationProfile), CONTENT_TYPE_APPLICATION_JSON,
CHARSET_UTF_8);
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode();
HttpEntity responseData = response.getEntity();
String responseString = EntityUtils.toString(responseData, CHARSET_UTF_8);
if (status != 201) {
throw new DynamicClientRegistrationException("Backend server error occurred while invoking DCR endpoint for " +
"registering service-provider for web-app : " + clientName);
}
return getOAuthApplicationInfo(gson.fromJson(responseString, JsonElement.class));
} catch (URISyntaxException e) {
throw new DynamicClientRegistrationException("Exception occurred while constructing the URI for invoking " +
"DCR endpoint for registering service-provider for web-app : "
+ clientName, e);
} catch (UnsupportedEncodingException e) {
throw new DynamicClientRegistrationException("Exception occurred while constructing the payload for invoking " +
"DCR endpoint for registering service-provider for web-app : "
+ clientName, e);
} catch (IOException e) {
throw new DynamicClientRegistrationException("Connection error occurred while invoking DCR endpoint for" +
" registering service-provider for web-app : " + clientName, e);
}
}
public static boolean deleteOAuthApplication(String user, String appName, String clientid, String host)
throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
// Setup the HTTPS settings to accept any certificate.
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL, socketFactory, getServerHTTPSPort()));
SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry);
httpClient = new DefaultHttpClient(mgr, httpClient.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
URI uri = new URIBuilder().setScheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
.setParameter("applicationName", appName)
.setParameter("userId", user)
.setParameter("consumerKey", clientid).build();
HttpDelete httpDelete = new HttpDelete(uri);
HttpResponse response = httpClient.execute(httpDelete);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
return true;
}
} catch (IOException e) {
throw new DynamicClientRegistrationException("Connection error occurred while constructing the payload for " +
"invoking DCR endpoint for unregistering the web-app : " + appName, e);
} catch (URISyntaxException e) {
throw new DynamicClientRegistrationException("Exception occurred while constructing the URI for invoking " +
"DCR endpoint for unregistering the web-app : " + appName, e);
}
return false;
}
private static int getServerHTTPSPort() {
// HTTPS port
String mgtConsoleTransport = CarbonUtils.getManagementTransport();
ConfigurationContextService configContextService =
DynamicClientWebAppRegistrationDataHolder.getInstance().getConfigurationContextService();
int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
int httpsProxyPort =
CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
mgtConsoleTransport);
if (httpsProxyPort > 0) {
port = httpsProxyPort;
}
return port;
}
private static OAuthApplicationInfo getOAuthApplicationInfo(JsonElement jsonData) {
JsonObject jsonObject = jsonData.getAsJsonObject();
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
JsonElement property = jsonObject.get("client_id");
if (property != null) {
oAuthApplicationInfo.setClientId(property.getAsString());
}
property = jsonObject.get("client_name");
if (property != null) {
oAuthApplicationInfo.setClientName(property.getAsString());
}
property = jsonObject.get("client_secret");
if (property != null) {
oAuthApplicationInfo.setClientSecret(property.getAsString());
}
return oAuthApplicationInfo;
}
}

@ -1247,6 +1247,11 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${google.gson.version}</version> <version>${google.gson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.http.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -1491,7 +1496,7 @@
<carbon.deployment.version>4.6.0</carbon.deployment.version> <carbon.deployment.version>4.6.0</carbon.deployment.version>
<!-- Carbon Identity --> <!-- Carbon Identity -->
<carbon.identity.version>5.0.3-SNAPSHOT</carbon.identity.version> <carbon.identity.version>5.0.3</carbon.identity.version>
<!-- Carbon Multi-tenancy --> <!-- Carbon Multi-tenancy -->
<carbon.multitenancy.version>4.5.0</carbon.multitenancy.version> <carbon.multitenancy.version>4.5.0</carbon.multitenancy.version>
@ -1542,6 +1547,7 @@
<google.gson.version>2.3.1</google.gson.version> <google.gson.version>2.3.1</google.gson.version>
<jsr311.version>1.1.1</jsr311.version> <jsr311.version>1.1.1</jsr311.version>
<commons.logging.version>1.2</commons.logging.version> <commons.logging.version>1.2</commons.logging.version>
<apache.http.version>4.5.1</apache.http.version>
<!-- Release plugin ID for github--> <!-- Release plugin ID for github-->
<project.scm.id>github-scm</project.scm.id> <project.scm.id>github-scm</project.scm.id>

Loading…
Cancel
Save