Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic auth support to proxy chain #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions src/main/java/org/littleshoot/proxy/ChainedProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,23 @@ public interface ChainedProxy extends SslEngineSource {
* Called to let us know that we were disconnected.
*/
void disconnected();

/**
* (Optional) user name which is send to the Chain Proxy using basic authentication. The user name
* is only send if the {@link #getBasicAuthPassword()} method provides a non <code>null</code>
* string.
*
* @return The user name or <code>null</code> if no authentication to the Chain Proxy is
* necessary.
*/
String getBasicAuthUser();

/**
* (Optional) user password which is send to the Chain Proxy using basic authentication. The
* password is only send if the {@link #getBasicAuthUser()} method provides a non
* <code>null</code> string.
*
* @return The password or <code>null</code> if no authentication to the Chain Proxy is necessary.
*/
String getBasicAuthPassword();
}
10 changes: 10 additions & 0 deletions src/main/java/org/littleshoot/proxy/ChainedProxyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ public void disconnected() {
public SSLEngine newSslEngine(String peerHost, int peerPort) {
return null;
}

@Override
public String getBasicAuthUser() {
return null;
}

@Override
public String getBasicAuthPassword() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ private void modifyRequestHeadersToReflectProxying(HttpRequest httpRequest) {
switchProxyConnectionHeader(headers);
stripConnectionTokens(headers);
stripHopByHopHeaders(headers);
addRemoteBasicAuthHeaders(headers);
ProxyUtils.addVia(httpRequest, proxyServer.getProxyAlias());
}
}
Expand Down Expand Up @@ -1186,6 +1187,25 @@ private void stripHopByHopHeaders(HttpHeaders headers) {
}
}
}

/**
* Add basic authentication header for the case that a chained proxy with basic authentication
* is available.
*
* @param headers
* The headers to modify
*/
private void addRemoteBasicAuthHeaders(HttpHeaders headers) {
if (currentServerConnection.getChainedProxy() != null) {
String basicAuthUser = currentServerConnection.getChainedProxy().getBasicAuthUser();
String basicAuthPassword = currentServerConnection.getChainedProxy().getBasicAuthPassword();
if (basicAuthUser != null && basicAuthPassword != null) {
String basicAuthString = "Basic "
+ new String(Base64.encodeBase64((basicAuthUser + ":" + basicAuthPassword).getBytes()));
headers.add(HttpHeaders.Names.PROXY_AUTHORIZATION.toLowerCase(Locale.US), basicAuthString);
}
}
}

/***************************************************************************
* Miscellaneous
Expand Down