Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ You can send a request with http proxy authenticate

[source,groovy]
----
def response = httpRequest httpProxy-authenticate: Basic, 'http://proxy.local',
def response = httpRequest httpProxy: 'http://proxy.local', proxyAuthentication: 'my-jenkins-credential-id',
responseHandle: 'NONE', url: 'https://api.github.com/orgs/${orgName}'
----

Expand Down Expand Up @@ -250,7 +250,7 @@ You can use a Jenkins credential to authenticate the request

[source,groovy]
----
def response = httpRequest authenticate: 'my-jenkins-credential-id',
def response = httpRequest authentication: 'my-jenkins-credential-id',
url: 'https://api.github.com/user/jenkinsci'
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private CloseableHttpClient auth(
((CredentialBasicAuthentication) authenticator).addCredentials(httpProxy, proxyCredentials);
} else {
new CredentialBasicAuthentication(proxyCredentials)
.prepare(clientBuilder, context, httpProxy);
.prepare(clientBuilder, context, httpProxy, httpProxy);
}
}

Expand All @@ -433,7 +433,7 @@ private CloseableHttpClient auth(
}

logger().println("Using authentication: " + authenticator.getKeyName());
return authenticator.authenticate(clientBuilder, context, httpRequestBase, logger());
return authenticator.authenticate(clientBuilder, context, httpRequestBase, httpProxy, logger());
}

private ResponseContentSupplier executeRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.PrintStream;
import java.io.Serializable;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -17,5 +18,5 @@ public interface Authenticator extends Serializable {
String getKeyName();

CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase,
PrintStream logger) throws IOException, InterruptedException;
HttpHost proxyHost, PrintStream logger) throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.PrintStream;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -53,8 +54,8 @@ public String getPassword() {

@Override
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context,
HttpRequestBase requestBase, PrintStream logger) {
CredentialBasicAuthentication.auth(clientBuilder, context, URIUtils.extractHost(requestBase.getURI()), userName, password);
HttpRequestBase requestBase, HttpHost proxyHost, PrintStream logger) {
CredentialBasicAuthentication.auth(clientBuilder, context, URIUtils.extractHost(requestBase.getURI()), proxyHost, userName, password);
return clientBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.PrintStream;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -28,6 +29,7 @@ public String getKeyName() {
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder,
HttpContext context,
HttpRequestBase requestBase,
HttpHost proxyHost,
PrintStream logger) throws IOException {
try {
clientBuilder.setSSLContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void addCredentials(HttpHost host, StandardUsernamePasswordCredentials cr
if (host == null || credentials == null) {
throw new IllegalArgumentException("Null target host or credentials");
}
extraCredentials.put(host, credential);
extraCredentials.put(host, credentials);
}

@Override
Expand All @@ -46,33 +46,42 @@ public String getKeyName() {
}

@Override
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase, PrintStream logger) {
prepare(clientBuilder, context, requestBase);
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase, HttpHost proxyHost, PrintStream logger) {
prepare(clientBuilder, context, requestBase, proxyHost);
return clientBuilder.build();
}

public void prepare(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase) {
prepare(clientBuilder, context, URIUtils.extractHost(requestBase.getURI()));
public void prepare(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase, HttpHost proxyHost) {
prepare(clientBuilder, context, URIUtils.extractHost(requestBase.getURI()), proxyHost);
}

public void prepare(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost) {
auth(clientBuilder, context, targetHost,
public void prepare(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost, HttpHost proxyHost) {
auth(clientBuilder, context, targetHost, proxyHost,
credential.getUsername(), credential.getPassword().getPlainText(), extraCredentials);
}

static void auth(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost,
static void auth(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost, HttpHost proxyHost,
String username, String password) {
auth(clientBuilder, context, targetHost, username, password, null);
auth(clientBuilder, context, targetHost, proxyHost, username, password, null);
}

static void auth(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost,
static void auth(HttpClientBuilder clientBuilder, HttpContext context, HttpHost targetHost, HttpHost proxyHost,
String username, String password, Map<HttpHost, StandardUsernamePasswordCredentials> extraCreds) {
CredentialsProvider provider = new BasicCredentialsProvider();
AuthCache authCache = new BasicAuthCache();

provider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new org.apache.http.auth.UsernamePasswordCredentials(username, password));
authCache.put(targetHost, new BasicScheme());

if (proxyHost != null) {
// Do not preempt authentication if target is the proxy
if (!targetHost.getHostName().equals(proxyHost.getHostName()) ||
(targetHost.getPort() != (proxyHost.getPort()))) {
authCache.put(targetHost, new BasicScheme());
}
} else {
authCache.put(targetHost, new BasicScheme());
}

if (extraCreds != null && !extraCreds.isEmpty()) {
for (Map.Entry<HttpHost, StandardUsernamePasswordCredentials> creds : extraCreds.entrySet()) {
Expand All @@ -83,7 +92,16 @@ static void auth(HttpClientBuilder clientBuilder, HttpContext context, HttpHost
creds.getValue().getPassword().getPlainText()
)
);
authCache.put(creds.getKey(), new BasicScheme());

if (proxyHost != null) {
// Do not preempt authentication if target is the proxy
if (!creds.getKey().getHostName().equals(proxyHost.getHostName()) ||
(creds.getKey().getPort() != (proxyHost.getPort()))) {
authCache.put(targetHost, new BasicScheme());
}
} else {
authCache.put(targetHost, new BasicScheme());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.PrintStream;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
Expand Down Expand Up @@ -50,7 +51,7 @@ public String getKeyName() {
}

@Override
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase, PrintStream logger) {
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context, HttpRequestBase requestBase, HttpHost proxyHost, PrintStream logger) {
return auth(clientBuilder, context, requestBase,
username, credential.getPassword().getPlainText(), domain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -50,7 +51,7 @@ public List<RequestAction> getActions() {

@Override
public CloseableHttpClient authenticate(HttpClientBuilder clientBuilder, HttpContext context,
HttpRequestBase requestBase, PrintStream logger) throws IOException {
HttpRequestBase requestBase, HttpHost proxyHost, PrintStream logger) throws IOException {
CloseableHttpClient client = clientBuilder.build();
final HttpClientUtil clientUtil = new HttpClientUtil();
for (RequestAction requestAction : actions) {
Expand Down