revert-70aa11f8
pasinduj 9 years ago
commit 9a50455a29

@ -44,20 +44,6 @@
<warName>${project.artifactId}</warName> <warName>${project.artifactId}</warName>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
@ -101,11 +87,6 @@
<artifactId>cxf-rt-bindings-http</artifactId> <artifactId>cxf-rt-bindings-http</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.codehaus.jackson</groupId> <groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId> <artifactId>jackson-jaxrs</artifactId>

@ -20,18 +20,61 @@ package org.wso2.carbon.dynamic.client.web.proxy;
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.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.wso2.carbon.dynamic.client.web.proxy.util.Constants;
import org.wso2.carbon.dynamic.client.web.proxy.util.DCRProxyUtils;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by harshan on 12/10/15.
*/
public class OAuthEndpointProxy { public class OAuthEndpointProxy {
private static final Log log = LogFactory.getLog(OAuthEndpointProxy.class); private static final Log log = LogFactory.getLog(OAuthEndpointProxy.class);
@POST @POST
public String getAccessToken() { @Consumes("application/x-www-form-urlencoded")
return ""; @Produces("application/json")
public Response issueAccessToken(MultivaluedMap<String, String> paramMap) {
DefaultHttpClient httpClient = DCRProxyUtils.getHttpsClient();
String host = DCRProxyUtils.getKeyManagerHost();
Response response;
try {
URI uri = new URIBuilder().setScheme(Constants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
Constants.RemoteServiceProperties.OAUTH2_TOKEN_ENDPOINT).build();
HttpHost httpHost = new HttpHost(uri.toString());
CloseableHttpResponse serverResponse = httpClient.execute(httpHost, null);
HttpEntity responseData = serverResponse.getEntity();
int status = serverResponse.getStatusLine().getStatusCode();
String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF_8);
response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build();
} catch (URISyntaxException e) {
String msg = "Service invoke error occurred while registering client";
log.error(msg, e);
response = Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (UnsupportedEncodingException e) {
String msg = "Service invoke error occurred while registering client";
log.error(msg, e);
response = Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (IOException e) {
String msg = "Service invoke error occurred while registering client";
log.error(msg, e);
response = Response.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} finally {
httpClient.close();
}
return response;
} }
} }

@ -18,73 +18,111 @@
package org.wso2.carbon.dynamic.client.web.proxy; package org.wso2.carbon.dynamic.client.web.proxy;
import com.google.gson.Gson;
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.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException;
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.proxy.util.Constants; import org.wso2.carbon.dynamic.client.web.proxy.util.Constants;
import org.wso2.carbon.dynamic.client.web.proxy.util.DCRProxyUtils; import org.wso2.carbon.dynamic.client.web.proxy.util.DCRProxyUtils;
import org.wso2.carbon.dynamic.client.web.proxy.util.RemoteDCRClient;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
/** /**
* Created by harshan on 12/10/15. * This class implements the proxy-endpoint for Dynamic-client-registration web service endpoints.
*/ */
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RegistrationProxy { public class RegistrationProxy {
private static final Log log = LogFactory.getLog(RegistrationProxy.class); private static final Log log = LogFactory.getLog(RegistrationProxy.class);
@POST @POST
public Response register(RegistrationProfile profile) { @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response register(RegistrationProfile registrationProfile) {
DefaultHttpClient httpClient = DCRProxyUtils.getHttpsClient();
String host = DCRProxyUtils.getKeyManagerHost();
Response response; Response response;
try { try {
CloseableHttpResponse serverResponse = RemoteDCRClient.createOAuthApplication(profile); URI uri = new URIBuilder().setScheme(Constants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT).build();
Gson gson = new Gson();
StringEntity entity = new StringEntity(gson.toJson(registrationProfile), MediaType.APPLICATION_JSON,
Constants.CharSets.CHARSET_UTF_8);
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(entity);
CloseableHttpResponse serverResponse = httpClient.execute(httpPost);
HttpEntity responseData = serverResponse.getEntity(); HttpEntity responseData = serverResponse.getEntity();
int status = serverResponse.getStatusLine().getStatusCode(); int status = serverResponse.getStatusLine().getStatusCode();
String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF8); String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF_8);
response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build(); response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build();
} catch (DynamicClientRegistrationException e) { } catch (URISyntaxException e) {
String msg = "Server error occurred while registering client '" + profile.getClientName() + "'"; String msg = "Server error occurred while registering client '" + registrationProfile.getClientName() + "'";
log.error(msg, e); log.error(msg, e);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); response = Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (UnsupportedEncodingException e) {
String msg = "Request data encoding error occurred while registering client '" + registrationProfile.
getClientName() + "'";
log.error(msg, e);
response = Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(msg).build();
} catch (IOException e) { } catch (IOException e) {
String msg = "Service invoke error occurred while registering client '" + profile.getClientName() + "'"; String msg = "Service invoke error occurred while registering client.";
log.error(msg, e); log.error(msg, e);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} finally {
httpClient.close();
} }
return response; return response;
} }
@DELETE @DELETE
@Produces(MediaType.APPLICATION_JSON)
public Response unregister(@QueryParam("applicationName") String applicationName, public Response unregister(@QueryParam("applicationName") String applicationName,
@QueryParam("userId") String userId, @QueryParam("userId") String userId,
@QueryParam("consumerKey") String consumerKey) { @QueryParam("consumerKey") String consumerKey) {
Response response; Response response;
DefaultHttpClient httpClient = DCRProxyUtils.getHttpsClient();
String host = DCRProxyUtils.getKeyManagerHost();
try { try {
CloseableHttpResponse serverResponse = RemoteDCRClient.deleteOAuthApplication(userId, applicationName, URI uri = new URIBuilder().setScheme(Constants.RemoteServiceProperties.
consumerKey); DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
.setParameter("applicationName", applicationName)
.setParameter("userId", userId)
.setParameter("consumerKey", consumerKey).build();
HttpDelete httpDelete = new HttpDelete(uri);
CloseableHttpResponse serverResponse = httpClient.execute(httpDelete);
HttpEntity responseData = serverResponse.getEntity(); HttpEntity responseData = serverResponse.getEntity();
int status = serverResponse.getStatusLine().getStatusCode(); int status = serverResponse.getStatusLine().getStatusCode();
String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF8); String resp = EntityUtils.toString(responseData, Constants.CharSets.CHARSET_UTF_8);
response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build(); response = Response.status(DCRProxyUtils.getResponseStatus(status)).entity(resp).build();
} catch (DynamicClientRegistrationException e) { } catch (URISyntaxException e) {
String msg = "Server error occurred while deleting the client '" + applicationName + "'"; String msg = "Server error occurred while deleting the client '" + applicationName + "'";
log.error(msg, e); log.error(msg, e);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); response = Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (UnsupportedEncodingException e) {
String msg = "Request data encoding error occurred while deleting the client '" + applicationName + "'";
log.error(msg, e);
response = Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(msg).build();
} catch (IOException e) { } catch (IOException e) {
String msg = "Service invoke error occurred while deleting the client '" + applicationName + "'"; String msg = "Service invoke error occurred while deleting the client '" + applicationName + "'";
log.error(msg, e); log.error(msg, e);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} finally {
httpClient.close();
} }
return response; return response;
} }

@ -19,26 +19,16 @@
package org.wso2.carbon.dynamic.client.web.proxy.util; package org.wso2.carbon.dynamic.client.web.proxy.util;
/** /**
* Created by harshan on 12/10/15. * Holds the constants used by DCR proxy app.
*/ */
public class Constants { public class Constants {
public static final class ContentTypes {
private ContentTypes() {
throw new AssertionError();
}
public static final String CONTENT_TYPE_ANY = "*/*";
public static final String CONTENT_TYPE_XML = "application/xml";
public static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
}
public static final class CharSets { public static final class CharSets {
private CharSets() { private CharSets() {
throw new AssertionError(); throw new AssertionError();
} }
public static final String CHARSET_UTF8 = "UTF8"; public static final String CHARSET_UTF_8 = "UTF-8";
} }
public static class ConfigurationProperties { public static class ConfigurationProperties {
@ -47,7 +37,6 @@ public class Constants {
} }
public static final String AUTHENTICATOR_NAME = "OAuthAuthenticator"; 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 final String AUTHENTICATOR_CONFIG_HOST_URL = "hostURL";
} }
@ -57,6 +46,7 @@ public class Constants {
} }
public static final String DYNAMIC_CLIENT_SERVICE_ENDPOINT = "/dynamic-client-web/register"; public static final String DYNAMIC_CLIENT_SERVICE_ENDPOINT = "/dynamic-client-web/register";
public static final String OAUTH2_TOKEN_ENDPOINT = "/oauth2/token";
public static final String DYNAMIC_CLIENT_SERVICE_PROTOCOL = "https"; public static final String DYNAMIC_CLIENT_SERVICE_PROTOCOL = "https";
} }
} }

@ -18,14 +18,25 @@
package org.wso2.carbon.dynamic.client.web.proxy.util; package org.wso2.carbon.dynamic.client.web.proxy.util;
import org.apache.http.HttpHost;
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.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.security.AuthenticatorsConfiguration; import org.wso2.carbon.core.security.AuthenticatorsConfiguration;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.ConfigurationContextService;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**
* Created by harshan on 12/10/15. * Holds the utility methods used by DCR proxy app.
*/ */
public class DCRProxyUtils { public class DCRProxyUtils {
@ -34,6 +45,24 @@ public class DCRProxyUtils {
return (ConfigurationContextService) ctx.getOSGiService(ConfigurationContextService.class, null); return (ConfigurationContextService) ctx.getOSGiService(ConfigurationContextService.class, null);
} }
public static DefaultHttpClient getHttpsClient() {
DefaultHttpClient httpClient = new DefaultHttpClient();
// 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(Constants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL, socketFactory, DCRProxyUtils.getServerHTTPSPort()));
SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry);
httpClient = new DefaultHttpClient(mgr, httpClient.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
return httpClient;
}
public static Response.Status getResponseStatus(int statusCode) { public static Response.Status getResponseStatus(int statusCode) {
switch (statusCode) { switch (statusCode) {
case 200 : case 200 :
@ -42,6 +71,8 @@ public class DCRProxyUtils {
return Response.Status.CREATED; return Response.Status.CREATED;
case 400 : case 400 :
return Response.Status.BAD_REQUEST; return Response.Status.BAD_REQUEST;
case 415 :
return Response.Status.UNSUPPORTED_MEDIA_TYPE;
case 500 : case 500 :
return Response.Status.INTERNAL_SERVER_ERROR; return Response.Status.INTERNAL_SERVER_ERROR;
} }
@ -73,4 +104,19 @@ public class DCRProxyUtils {
} }
return null; return null;
} }
public static int getServerHTTPSPort() {
// HTTPS port
String mgtConsoleTransport = CarbonUtils.getManagementTransport();
ConfigurationContextService configContextService = DCRProxyUtils.getConfigurationContextService();
int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
int httpsProxyPort =
CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
mgtConsoleTransport);
if (httpsProxyPort > 0) {
port = httpsProxyPort;
}
return port;
}
} }

@ -1,150 +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.proxy.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.CloseableHttpResponse;
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.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationException;
import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile;
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 CloseableHttpResponse createOAuthApplication(RegistrationProfile registrationProfile)
throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient();
String clientName = registrationProfile.getClientName();
String host = DCRProxyUtils.getKeyManagerHost();
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(Constants.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(Constants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
Constants.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);
return httpClient.execute(httpPost);
} 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 CloseableHttpResponse deleteOAuthApplication(String user, String appName, String clientid)
throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient();
String host = DCRProxyUtils.getKeyManagerHost();
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(Constants.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(Constants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
.setParameter("applicationName", appName)
.setParameter("userId", user)
.setParameter("consumerKey", clientid).build();
HttpDelete httpDelete = new HttpDelete(uri);
return httpClient.execute(httpDelete);
} 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);
}
}
private static int getServerHTTPSPort() {
// HTTPS port
String mgtConsoleTransport = CarbonUtils.getManagementTransport();
ConfigurationContextService configContextService = DCRProxyUtils.getConfigurationContextService();
int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
int httpsProxyPort =
CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
mgtConsoleTransport);
if (httpsProxyPort > 0) {
port = httpsProxyPort;
}
return port;
}
}

@ -37,7 +37,7 @@
</jaxrs:server> </jaxrs:server>
<!-- OAuth Service Proxy Endpoint --> <!-- OAuth Service Proxy Endpoint -->
<jaxrs:server id="OAuthService" address="/oauth"> <jaxrs:server id="OAuthService" address="/token">
<jaxrs:serviceBeans> <jaxrs:serviceBeans>
<ref bean="OAuthServiceBean"/> <ref bean="OAuthServiceBean"/>
</jaxrs:serviceBeans> </jaxrs:serviceBeans>

@ -27,8 +27,6 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface RegistrationService { public interface RegistrationService {
enum ErrorCode { enum ErrorCode {
@ -52,6 +50,8 @@ public interface RegistrationService {
* @return Status 200 if success including consumerKey and consumerSecret. * @return Status 200 if success including consumerKey and consumerSecret.
*/ */
@POST @POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response register(RegistrationProfile profile); Response register(RegistrationProfile profile);
/** /**
@ -63,6 +63,8 @@ public interface RegistrationService {
* @return Status 200 if success. * @return Status 200 if success.
*/ */
@DELETE @DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response unregister(@QueryParam("applicationName") String applicationName, Response unregister(@QueryParam("applicationName") String applicationName,
@QueryParam("userId") String userId, @QueryParam("userId") String userId,
@QueryParam("consumerKey") String consumerKey); @QueryParam("consumerKey") String consumerKey);

@ -37,8 +37,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RegistrationServiceImpl implements RegistrationService { public class RegistrationServiceImpl implements RegistrationService {
private static final Log log = LogFactory.getLog(RegistrationServiceImpl.class); private static final Log log = LogFactory.getLog(RegistrationServiceImpl.class);

@ -60,26 +60,17 @@ public class RemoteDCRClient {
public static OAuthApplicationInfo createOAuthApplication(RegistrationProfile registrationProfile, String host) public static OAuthApplicationInfo createOAuthApplication(RegistrationProfile registrationProfile, String host)
throws DynamicClientRegistrationException { throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient(); if (log.isDebugEnabled()) {
log.debug("Invoking DCR service to create OAuth application for web app : " + registrationProfile.
getClientName());
}
DefaultHttpClient httpClient = getHTTPSClient();
String clientName = registrationProfile.getClientName(); String clientName = registrationProfile.getClientName();
try { 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. URI uri = new URIBuilder().setScheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath( DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT).build(); DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
.build();
Gson gson = new Gson(); Gson gson = new Gson();
StringEntity entity = new StringEntity(gson.toJson(registrationProfile), StringEntity entity = new StringEntity(gson.toJson(registrationProfile),
DynamicClientWebAppRegistrationConstants.ContentTypes.CONTENT_TYPE_APPLICATION_JSON, DynamicClientWebAppRegistrationConstants.ContentTypes.CONTENT_TYPE_APPLICATION_JSON,
@ -88,11 +79,12 @@ public class RemoteDCRClient {
httpPost.setEntity(entity); httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost); HttpResponse response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode(); int status = response.getStatusLine().getStatusCode();
HttpEntity responseData = response.getEntity(); HttpEntity responseData = response.getEntity();
String responseString = EntityUtils.toString(responseData, DynamicClientWebAppRegistrationConstants. String responseString = EntityUtils.toString(responseData, DynamicClientWebAppRegistrationConstants.
CharSets.CHARSET_UTF8); CharSets.CHARSET_UTF8);
if (status != 201) { if (status != 201) {
throw new DynamicClientRegistrationException("Backend server error occurred while invoking DCR endpoint for " + throw new DynamicClientRegistrationException(
"Backend server error occurred while invoking DCR endpoint for " +
"registering service-provider for web-app : " + clientName); "registering service-provider for web-app : " + clientName);
} }
return getOAuthApplicationInfo(gson.fromJson(responseString, JsonElement.class)); return getOAuthApplicationInfo(gson.fromJson(responseString, JsonElement.class));
@ -101,39 +93,32 @@ public class RemoteDCRClient {
"DCR endpoint for registering service-provider for web-app : " "DCR endpoint for registering service-provider for web-app : "
+ clientName, e); + clientName, e);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new DynamicClientRegistrationException("Exception occurred while constructing the payload for invoking " + throw new DynamicClientRegistrationException(
"DCR endpoint for registering service-provider for web-app : " "Exception occurred while constructing the payload for invoking " +
+ clientName, e); "DCR endpoint for registering service-provider for web-app : "
+ clientName, e);
} catch (IOException e) { } catch (IOException e) {
throw new DynamicClientRegistrationException("Connection error occurred while invoking DCR endpoint for" + throw new DynamicClientRegistrationException("Connection error occurred while invoking DCR endpoint for" +
" registering service-provider for web-app : " + clientName, e); " registering service-provider for web-app : " + clientName,
e);
} finally {
httpClient.close();
} }
} }
public static boolean deleteOAuthApplication(String user, String appName, String clientid, String host) public static boolean deleteOAuthApplication(String user, String appName, String clientid, String host)
throws DynamicClientRegistrationException { throws DynamicClientRegistrationException {
DefaultHttpClient httpClient = new DefaultHttpClient(); if (log.isDebugEnabled()) {
log.debug("Invoking DCR service to remove OAuth application created for web app : " + appName);
}
DefaultHttpClient httpClient = getHTTPSClient();
try { try {
// Setup the HTTPS settings to accept any certificate. URI uri = new URIBuilder().setScheme(DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; DYNAMIC_CLIENT_SERVICE_PROTOCOL).setHost(host).setPath(
DynamicClientWebAppRegistrationConstants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_ENDPOINT)
SchemeRegistry registry = new SchemeRegistry(); .setParameter("applicationName", appName)
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); .setParameter("userId", user)
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); .setParameter("consumerKey", clientid).build();
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); HttpDelete httpDelete = new HttpDelete(uri);
HttpResponse response = httpClient.execute(httpDelete); HttpResponse response = httpClient.execute(httpDelete);
int status = response.getStatusLine().getStatusCode(); int status = response.getStatusLine().getStatusCode();
@ -141,11 +126,14 @@ public class RemoteDCRClient {
return true; return true;
} }
} catch (IOException e) { } catch (IOException e) {
throw new DynamicClientRegistrationException("Connection error occurred while constructing the payload for " + throw new DynamicClientRegistrationException(
"invoking DCR endpoint for unregistering the web-app : " + appName, e); "Connection error occurred while constructing the payload for " +
"invoking DCR endpoint for unregistering the web-app : " + appName, e);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new DynamicClientRegistrationException("Exception occurred while constructing the URI for invoking " + throw new DynamicClientRegistrationException("Exception occurred while constructing the URI for invoking " +
"DCR endpoint for unregistering the web-app : " + appName, e); "DCR endpoint for unregistering the web-app : " + appName, e);
} finally {
httpClient.close();
} }
return false; return false;
} }
@ -162,7 +150,7 @@ public class RemoteDCRClient {
if (httpsProxyPort > 0) { if (httpsProxyPort > 0) {
port = httpsProxyPort; port = httpsProxyPort;
} }
return port; return port;
} }
private static OAuthApplicationInfo getOAuthApplicationInfo(JsonElement jsonData) { private static OAuthApplicationInfo getOAuthApplicationInfo(JsonElement jsonData) {
@ -182,4 +170,22 @@ public class RemoteDCRClient {
} }
return oAuthApplicationInfo; return oAuthApplicationInfo;
} }
private static DefaultHttpClient getHTTPSClient() {
DefaultHttpClient httpClient = new DefaultHttpClient();
// 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);
return httpClient;
}
} }

@ -106,6 +106,9 @@ public class MonitoringManagerImpl implements MonitoringManager {
complianceData.setPolicyId(policy.getId()); complianceData.setPolicyId(policy.getId());
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyComplianceException("Error occurred while opening a data source connection", e); throw new PolicyComplianceException("Error occurred while opening a data source connection", e);
} catch (MonitoringDAOException e) {
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -124,6 +127,10 @@ public class MonitoringManagerImpl implements MonitoringManager {
complianceFeatures); complianceFeatures);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -143,6 +150,10 @@ public class MonitoringManagerImpl implements MonitoringManager {
.getId()); .getId());
monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to remove the none compliance features from database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -153,17 +164,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable tor retrieve device data from DB for " + throw new PolicyComplianceException("Unable tor retrieve device data from DB for " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} catch (PolicyManagerDAOException | PolicyManagementException e) { } catch (PolicyManagerDAOException | PolicyManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " + throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} catch (MonitoringDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyComplianceException("Unable to add the none compliance features to database for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} }
return complianceFeatures; return complianceFeatures;
} }

@ -99,13 +99,14 @@ public class OAuthAuthenticator implements WebappAuthenticator {
if (oAuthValidationResponse.isValid()) { if (oAuthValidationResponse.isValid()) {
String username = oAuthValidationResponse.getUserName(); String username = oAuthValidationResponse.getUserName();
String tenantDomain = oAuthValidationResponse.getTenantDomain();
//Remove the userstore domain from username //Remove the userstore domain from username
/*if (username.contains("/")) { /*if (username.contains("/")) {
username = username.substring(username.indexOf('/') + 1); username = username.substring(username.indexOf('/') + 1);
}*/ }*/
authenticationInfo.setUsername(username); authenticationInfo.setUsername(username);
authenticationInfo.setTenantDomain(oAuthValidationResponse.getTenantDomain()); authenticationInfo.setTenantDomain(tenantDomain);
authenticationInfo.setTenantId(Utils.getTenantIdOFUser(username)); authenticationInfo.setTenantId(Utils.getTenantIdOFUser(username + "@" + tenantDomain));
if (oAuthValidationResponse.isValid()) { if (oAuthValidationResponse.isValid()) {
authenticationInfo.setStatus(Status.CONTINUE); authenticationInfo.setStatus(Status.CONTINUE);
} }

@ -105,15 +105,6 @@
<outputDirectory>${basedir}/src/main/resources/</outputDirectory> <outputDirectory>${basedir}/src/main/resources/</outputDirectory>
<destFileName>dynamic-client-web.war</destFileName> <destFileName>dynamic-client-web.war</destFileName>
</artifactItem> </artifactItem>
<artifactItem>
<groupId>org.wso2.mdm</groupId>
<artifactId>dynamic-client-web-proxy</artifactId>
<version>${carbon.device.mgt.version}</version>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources/</outputDirectory>
<destFileName>dynamic-client-web-proxy.war</destFileName>
</artifactItem>
</artifactItems> </artifactItems>
</configuration> </configuration>
</execution> </execution>

@ -1,3 +1,2 @@
instructions.configure = \ instructions.configure = \
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.dynamic.client.registration.server_${feature.version}/dynamic-client-web.war,target:${installFolder}/../../deployment/server/webapps/dynamic-client-web.war,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.dynamic.client.registration.server_${feature.version}/dynamic-client-web.war,target:${installFolder}/../../deployment/server/webapps/dynamic-client-web.war,overwrite:true);\
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.dynamic.client.registration.server_${feature.version}/dynamic-client-web-proxy.war,target:${installFolder}/../../deployment/server/webapps/dynamic-client-web-proxy.war,overwrite:true);\
Loading…
Cancel
Save