This is an automated email from the ASF dual-hosted git repository.
dulvac pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git
The following commit(s) were added to refs/heads/master by this push:
new bddbc14 trivial: allow disabling preemtive auth in sling client
bddbc14 is described below
commit bddbc147d620f0230fba2517605d7f70e9a4d151
Author: Andrei Dulvac <[email protected]>
AuthorDate: Wed Nov 29 18:09:12 2017 +0100
trivial: allow disabling preemtive auth in sling client
---
.../apache/sling/testing/clients/SlingClient.java | 5 ++
.../sling/testing/clients/SlingClientConfig.java | 16 +++-
.../interceptors/FormBasedAuthInterceptor.java | 94 ++++++++++++++++++++++
.../testing/clients/interceptors/package-info.java | 2 +-
.../apache/sling/testing/clients/package-info.java | 2 +-
5 files changed, 116 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/clients/SlingClient.java
b/src/main/java/org/apache/sling/testing/clients/SlingClient.java
index bee5b40..8359bf2 100644
--- a/src/main/java/org/apache/sling/testing/clients/SlingClient.java
+++ b/src/main/java/org/apache/sling/testing/clients/SlingClient.java
@@ -589,6 +589,11 @@ public class SlingClient extends AbstractSlingClient {
return this;
}
+ public InternalBuilder<T> setPreemptiveAuth(boolean isPreemptiveAuth) {
+ this.configBuilder.setPreemptiveAuth(isPreemptiveAuth);
+ return this;
+ }
+
public InternalBuilder<T> setCookieStore(CookieStore cs) {
this.configBuilder.setCookieStore(cs);
return this;
diff --git
a/src/main/java/org/apache/sling/testing/clients/SlingClientConfig.java
b/src/main/java/org/apache/sling/testing/clients/SlingClientConfig.java
index 58d380c..531d7e1 100644
--- a/src/main/java/org/apache/sling/testing/clients/SlingClientConfig.java
+++ b/src/main/java/org/apache/sling/testing/clients/SlingClientConfig.java
@@ -69,11 +69,13 @@ public class SlingClientConfig {
*/
protected final AuthCache authCache;
+
/**
* Extra values to be used in interceptors, custom auth mechanisms, etc.
*/
protected final Map<String, String> values;
+
protected SlingClientConfig(URI url, String user, String password,
CookieStore cookieStore,
CredentialsProvider credentialsProvider,
AuthCache authCache) {
@@ -153,6 +155,8 @@ public class SlingClientConfig {
protected AuthCache authCache;
+ protected boolean preeemptiveAuth;
+
protected Builder() {
}
@@ -193,6 +197,11 @@ public class SlingClientConfig {
return this;
}
+ public Builder setPreemptiveAuth(boolean preemptiveAuth) {
+ this.preeemptiveAuth = preemptiveAuth;
+ return this;
+ }
+
public Builder setCookieStore(CookieStore cookieStore) {
this.cookieStore = cookieStore;
return this;
@@ -209,13 +218,18 @@ public class SlingClientConfig {
}
}
- // Create default AuthCache if not set
+ // Create default AuthCache for basic if not set
if (authCache == null) {
BasicScheme basicScheme = new BasicScheme();
authCache = new BasicAuthCache();
authCache.put(URIUtils.extractHost(url), basicScheme);
}
+ // if preemptive auth is disabled, force auth cache to be null
+ if (!this.preeemptiveAuth) {
+ authCache = null;
+ }
+
// Create default CookieStore if not set
if (cookieStore == null) {
cookieStore = new BasicCookieStore();
diff --git
a/src/main/java/org/apache/sling/testing/clients/interceptors/FormBasedAuthInterceptor.java
b/src/main/java/org/apache/sling/testing/clients/interceptors/FormBasedAuthInterceptor.java
new file mode 100644
index 0000000..0f78425
--- /dev/null
+++
b/src/main/java/org/apache/sling/testing/clients/interceptors/FormBasedAuthInterceptor.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * 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.apache.sling.testing.clients.interceptors;
+
+import org.apache.http.*;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.cookie.Cookie;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.protocol.HttpContext;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.SlingClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.LinkedList;
+import java.util.List;
+
+public class FormBasedAuthInterceptor implements HttpRequestInterceptor {
+ static final Logger LOG =
LoggerFactory.getLogger(FormBasedAuthInterceptor.class);
+
+ private final String loginPath = "j_security_check";
+ private final String loginTokenName;
+
+ public FormBasedAuthInterceptor(String loginTokenName) {
+ this.loginTokenName = loginTokenName;
+ }
+
+ public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
+ final URI uri = URI.create(request.getRequestLine().getUri());
+ if (uri.getPath().endsWith(loginPath)) {
+ LOG.debug("Request ends with {} so I'm not intercepting the
request", loginPath);
+ return;
+ }
+
+ Cookie loginCookie = getLoginCookie(context, loginTokenName);
+ if (loginCookie != null) {
+ LOG.debug("Request has cookie {}={} so I'm not intercepting the
request", loginCookie.getName(), loginCookie.getValue());
+ return;
+ }
+
+ // get host
+ final HttpHost host = HttpClientContext.adapt(context).getTargetHost();
+
+ // get the username and password from the credentials provider
+ final CredentialsProvider credsProvider =
HttpClientContext.adapt(context).getCredentialsProvider();
+ final AuthScope scope = new AuthScope(host.getHostName(),
host.getPort());
+ final String username =
credsProvider.getCredentials(scope).getUserPrincipal().getName();
+ final String password =
credsProvider.getCredentials(scope).getPassword();
+
+ List<NameValuePair> parameters = new LinkedList<>();
+ parameters.add(new BasicNameValuePair("j_username", username));
+ parameters.add(new BasicNameValuePair("j_password", password));
+ HttpEntity httpEntity = new UrlEncodedFormEntity(parameters, "utf-8");
+
+ HttpPost loginPost = new
HttpPost(URI.create(request.getRequestLine().getUri()).resolve(loginPath));
+ loginPost.setEntity(httpEntity);
+ final CloseableHttpClient client =
HttpClientBuilder.create().disableRedirectHandling().build();
+
+ client.execute(host, loginPost, context);
+
+ }
+
+ /** Get login token cookie or null if not found */
+ private Cookie getLoginCookie(HttpContext context, String loginTokenName) {
+ for (Cookie cookie :
HttpClientContext.adapt(context).getCookieStore().getCookies()) {
+ if (cookie.getName().equalsIgnoreCase(loginTokenName)) {
+ return cookie;
+ }
+ }
+ return null;
+ }
+}
diff --git
a/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
b/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
index 0f097b5..25c711d 100644
---
a/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
+++
b/src/main/java/org/apache/sling/testing/clients/interceptors/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("1.0.1")
+@Version("1.1.0")
package org.apache.sling.testing.clients.interceptors;
import org.osgi.annotation.versioning.Version;
diff --git a/src/main/java/org/apache/sling/testing/clients/package-info.java
b/src/main/java/org/apache/sling/testing/clients/package-info.java
index 991d7e5..9970e37 100644
--- a/src/main/java/org/apache/sling/testing/clients/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("1.3.0")
+@Version("1.4.0")
package org.apache.sling.testing.clients;
import org.osgi.annotation.versioning.Version;
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].