aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Terry2013-07-30 16:39:32 (EDT)
committerDavid Terry2013-07-31 11:41:31 (EDT)
commit2335083be55b66ff87771dfa9fbca3625c1bd819 (patch)
tree0d6c635aab4c8f52ddaf265e754ddf6c35ea8459
parent53ff68bd61de97fe395bf56f4aca7800978a3df5 (diff)
downloadorg.eclipse.lyo.rio-2335083be55b66ff87771dfa9fbca3625c1bd819.zip
org.eclipse.lyo.rio-2335083be55b66ff87771dfa9fbca3625c1bd819.tar.gz
org.eclipse.lyo.rio-2335083be55b66ff87771dfa9fbca3625c1bd819.tar.bz2
[414069] Add OAuth 2 legged support to TRS RIO Testsrefs/changes/94/14994/2
Change-Id: I00f89a24a08d2fd63b772fa505bfb481a214f349 Signed-off-by: David Terry <dgterry@us.ibm.com>
-rw-r--r--org.eclipse.lyo.rio.trs.tests/pom.xml13
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogCreationTest.java9
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogUpdationTest.java7
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/EasySSLClient.java104
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/FetchUtil.java44
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/messages.properties1
-rw-r--r--org.eclipse.lyo.rio.trs.tests/src/main/resources/config.properties14
7 files changed, 183 insertions, 9 deletions
diff --git a/org.eclipse.lyo.rio.trs.tests/pom.xml b/org.eclipse.lyo.rio.trs.tests/pom.xml
index a8ab2bc..7779aa6 100644
--- a/org.eclipse.lyo.rio.trs.tests/pom.xml
+++ b/org.eclipse.lyo.rio.trs.tests/pom.xml
@@ -7,12 +7,25 @@
<artifactId>org.eclipse.lyo.rio.trs.tests</artifactId>
<description>A set of junit tests to validate that various aspects of the reference application's change log are functioning properly.</description>
+ <repositories>
+ <repository>
+ <id>oauth</id>
+ <url>http://oauth.googlecode.com/svn/code/maven</url>
+ </repository>
+ </repositories>
+
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
+ <dependency>
+ <groupId>net.oauth.core</groupId>
+ <artifactId>oauth-consumer</artifactId>
+ <version>20090617</version>
+ <scope>compile</scope>
+ </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogCreationTest.java b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogCreationTest.java
index 8821b74..6fd1132 100644
--- a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogCreationTest.java
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogCreationTest.java
@@ -30,6 +30,7 @@ import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.DefaultedHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
+import org.eclipse.lyo.rio.trs.tests.utils.EasySSLClient;
import org.eclipse.lyo.rio.trs.tests.utils.FetchException;
import org.eclipse.lyo.rio.trs.tests.utils.ITRSVocabulary;
import org.eclipse.lyo.rio.trs.tests.utils.InvalidTRSException;
@@ -72,7 +73,7 @@ public class ChangeLogCreationTest extends TestCore{
String trsEndpoint = prop.getProperty("configTrsEndpoint");
String acceptType = prop.getProperty("acceptType");
- httpClient = new DefaultHttpClient();
+ httpClient = new EasySSLClient().getClient();
httpContext =
new DefaultedHttpContext(new BasicHttpContext(), new SyncBasicHttpContext(null));
@@ -83,15 +84,15 @@ public class ChangeLogCreationTest extends TestCore{
trsResource = getResource(trsEndpoint, httpClient, httpContext, acceptType);
} catch (FileNotFoundException e) {
terminateTest(Messages.getServerString("tests.general.config.properties.missing"), e);
- } catch (InterruptedException e) {
- terminateTest(null, e);
} catch (IOException e) {
terminateTest(Messages.getServerString("tests.general.config.properties.unreadable"), e);
} catch (FetchException e) {
terminateTest(Messages.getServerString("tests.general.trs.fetch.error"), e);
} catch (SendException e) {
terminateTest(Messages.getServerString("tests.general.trs.send.error"), e);
- }
+ } catch (Exception e) {
+ terminateTest(null, e);
+ }
}
@Test
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogUpdationTest.java b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogUpdationTest.java
index fee4c60..0965299 100644
--- a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogUpdationTest.java
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/ChangeLogUpdationTest.java
@@ -31,6 +31,7 @@ import org.apache.http.protocol.DefaultedHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
import org.eclipse.lyo.core.trs.HttpConstants;
+import org.eclipse.lyo.rio.trs.tests.utils.EasySSLClient;
import org.eclipse.lyo.rio.trs.tests.utils.FetchException;
import org.eclipse.lyo.rio.trs.tests.utils.ITRSVocabulary;
import org.eclipse.lyo.rio.trs.tests.utils.InvalidTRSException;
@@ -77,7 +78,7 @@ public class ChangeLogUpdationTest extends TestCore {
String trsEndpoint = prop.getProperty("configTrsEndpoint");
String acceptType = prop.getProperty("acceptType");
- httpClient = new DefaultHttpClient();
+ httpClient = new EasySSLClient().getClient();
httpContext =
new DefaultedHttpContext(new BasicHttpContext(), new SyncBasicHttpContext(null));
@@ -97,14 +98,14 @@ public class ChangeLogUpdationTest extends TestCore {
} catch (FileNotFoundException e) {
terminateTest(Messages.getServerString("tests.general.config.properties.missing"), e);
- } catch (InterruptedException e) {
- terminateTest(null, e);
} catch (IOException e) {
terminateTest(Messages.getServerString("tests.general.config.properties.unreadable"), e);
} catch (FetchException e) {
terminateTest(Messages.getServerString("tests.general.trs.fetch.error"), e);
} catch (SendException e) {
terminateTest(Messages.getServerString("tests.general.trs.send.error"), e);
+ } catch (Exception e) {
+ terminateTest(null, e);
}
}
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/EasySSLClient.java b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/EasySSLClient.java
new file mode 100644
index 0000000..a4416a2
--- /dev/null
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/EasySSLClient.java
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Joseph Leong - Initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.rio.trs.tests.utils;
+
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import org.apache.http.conn.scheme.PlainSocketFactory;
+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.TrustStrategy;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+public class EasySSLClient {
+ private DefaultHttpClient httpClient;
+
+ public EasySSLClient() throws Exception
+ {
+ httpClient = new DefaultHttpClient();
+ initCookiePolicy();
+ initSSLPolicy();
+ }
+
+ void initCookiePolicy() {
+ httpClient.setCookieStore(new BasicCookieStore());
+ }
+
+ void initSSLPolicy() throws Exception {
+ Scheme http1 = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
+ Scheme http2 = new Scheme("http", 8080, PlainSocketFactory.getSocketFactory());
+ Scheme http3 = new Scheme("http", 8082, PlainSocketFactory.getSocketFactory());
+ Scheme http4 = new Scheme("http", 9080, PlainSocketFactory.getSocketFactory());
+ Scheme http5 = new Scheme("http", 9081, PlainSocketFactory.getSocketFactory());
+
+ SSLSocketFactory sf = null;
+
+ try {
+ sf = getEasySSLSocketFactory();
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new Exception("Unable to initialize SSL HttpClient");
+ }
+
+ Scheme https1 = new Scheme("https", 443, sf);
+ Scheme https2 = new Scheme("https", 8443, sf);
+ Scheme https3 = new Scheme("https", 9443, sf);
+ Scheme https4 = new Scheme("https", 9444, sf);
+
+ SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
+
+ schemeRegistry.register(http1);
+ schemeRegistry.register(http2);
+ schemeRegistry.register(http3);
+ schemeRegistry.register(http4);
+ schemeRegistry.register(http5);
+ schemeRegistry.register(https1);
+ schemeRegistry.register(https2);
+ schemeRegistry.register(https3);
+ schemeRegistry.register(https4);
+ }
+
+ private SSLSocketFactory getEasySSLSocketFactory()
+ throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException
+ {
+ TrustStrategy trustStrategy = new TrustStrategy() {
+ @Override
+ public boolean isTrusted(X509Certificate[] x509Certificates,
+ String s) throws CertificateException {
+ return true; // Accept Self-Signed Certs
+ }
+ };
+
+ SSLSocketFactory sslSocketFactory = null;
+
+ //Bypass check for hostname verification
+ sslSocketFactory =
+ new SSLSocketFactory(trustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+
+ return sslSocketFactory;
+ }
+
+ public DefaultHttpClient getClient() {
+ return this.httpClient;
+ }
+}
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/FetchUtil.java b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/FetchUtil.java
index e96d2a0..2294f7a 100644
--- a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/FetchUtil.java
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/FetchUtil.java
@@ -19,8 +19,19 @@ package org.eclipse.lyo.rio.trs.tests.utils;
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;
+import java.util.Properties;
+import javax.ws.rs.HttpMethod;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthException;
+import net.oauth.OAuthMessage;
+import net.oauth.OAuthServiceProvider;
+
+import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.HttpContext;
import org.eclipse.lyo.core.trs.HttpConstants;
@@ -78,7 +89,38 @@ public class FetchUtil {
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
get.addHeader(HttpConstants.CACHE_CONTROL, "max-age=0"); //$NON-NLS-1$
- model = httpClient.execute(get, new RDFModelResponseHandler(uri), httpContext);
+ try {
+ model = httpClient.execute(get, new RDFModelResponseHandler(uri), httpContext);
+ } catch (HttpResponseException e1) {
+ if (e1.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
+ // If authentication is required attempt OAuth authentication.
+ // This uses two legged OAuth which requires a functional
+ // user be already configured on the resource server.
+ Properties prop = TestCore.getConfigPropertiesInstance();
+ String consumerKey = prop.getProperty("consumerKey");
+ String consumerSecret = prop.getProperty("consumerSecret");
+ String authorizationTokenURL = prop.getProperty("OAuthURL");
+ String oAuthRealm = prop.getProperty("OAuthRealm");
+
+ // Using the information from the config.properties file
+ // construct the authentication header to use in our GET request
+ OAuthServiceProvider provider = new OAuthServiceProvider(null, authorizationTokenURL, null);
+ OAuthConsumer consumer = new OAuthConsumer("",consumerKey,consumerSecret,provider);
+ OAuthAccessor accessor = new OAuthAccessor(consumer);
+ accessor.accessToken = "";
+
+ try {
+ OAuthMessage message = accessor.newRequestMessage(HttpMethod.GET, prop.getProperty("configTrsEndpoint"), null);
+ String authHeader = message.getAuthorizationHeader(oAuthRealm);
+ get.setHeader("Authorization",authHeader);
+ get.setHeader("OSLC-Core-Version", "2.0");
+
+ model = httpClient.execute(get, new RDFModelResponseHandler(uri), httpContext);
+ } catch (OAuthException e) {
+ TestCore.terminateTest(Messages.getServerString("fetch.util.authentication.failure"), e);
+ }
+ }
+ }
} catch (Exception e) {
String uriLocation = Messages.getServerString("fetch.util.uri.unidentifiable"); //$NON-NLS-1$
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/messages.properties b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/messages.properties
index 46c3185..5236838 100644
--- a/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/messages.properties
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/java/org/eclipse/lyo/rio/trs/tests/utils/messages.properties
@@ -18,6 +18,7 @@ model.util.model.null = model must not be null
model.util.inputstream.null = InputStream must not be null
model.util.outputstream.null = OutputStream must not be null
+fetch.util.authentication.failure = Tests were not authorized to access TRS resource and fallback OAuth authentication failed.
fetch.util.uri.null = Argument uri must not be null
fetch.util.httpclient.null = Argument httpClient must not be null
fetch.util.uri.unidentifiable = Unable to identify URI Location
diff --git a/org.eclipse.lyo.rio.trs.tests/src/main/resources/config.properties b/org.eclipse.lyo.rio.trs.tests/src/main/resources/config.properties
index 61c8342..1d16c4d 100644
--- a/org.eclipse.lyo.rio.trs.tests/src/main/resources/config.properties
+++ b/org.eclipse.lyo.rio.trs.tests/src/main/resources/config.properties
@@ -20,4 +20,16 @@ configResContentFile =
#Media/Content type to be used for the Update
configUpdateContentType = application/rdf+xml
#Content to be posted to the resource for Update
-configResUpdateFile = resourceUpdateContent.xml \ No newline at end of file
+configResUpdateFile = resourceUpdateContent.xml
+
+#OAuth consumer key. Often generated by and shared with the resource server
+consumerKey = key
+
+#OAuth consumer secret shared with the resource server
+consumerSecret = secret
+
+#Name of the OAuth Realm
+OAuthRealm = Jazz
+
+#OAuth Authentication URL
+OAuthURL = https://localhost:9443/ccm/oauth-authorize \ No newline at end of file