trivial: allow disabling preemtive auth in sling client
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 @@
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 @@
*/
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 @@
protected AuthCache authCache;
+ protected boolean preeemptiveAuth;
+
protected Builder() {
}
@@ -193,6 +197,11 @@
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 @@
}
}
- // 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;