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
42 changes: 42 additions & 0 deletions src/main/java/org/littleshoot/proxy/BasicProxyAuthenticator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.littleshoot.proxy;

import com.google.common.io.BaseEncoding;

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;

/**
*
*/
public abstract class BasicProxyAuthenticator implements ProxyAuthenticator {

@Override
public boolean authenticate(String proxyAuthorizationHeaderValue) {
String value = StringUtils.substringAfter(proxyAuthorizationHeaderValue, "Basic ").trim();

byte[] decodedValue = BaseEncoding.base64().decode(value);

String decodedString = new String(decodedValue, Charset.forName("UTF-8"));

String userName = StringUtils.substringBefore(decodedString, ":");
String password = StringUtils.substringAfter(decodedString, ":");

return authenticate(userName, password);
}

/**
* Authenticates the user using the specified userName and password.
*
* @param username
* The user name.
* @param password
* The password.
* @return <code>true</code> if the credentials are acceptable, otherwise
* <code>false</code>.
* requests.
*/
abstract boolean authenticate(String username, String password);

abstract public String getRealm();
}
17 changes: 8 additions & 9 deletions src/main/java/org/littleshoot/proxy/ProxyAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
* the basis of a username and password.
*/
public interface ProxyAuthenticator {

/**
* Authenticates the user using the specified userName and password.
*
* @param userName
* The user name.
* @param password
* The password.
* @return <code>true</code> if the credentials are acceptable, otherwise
* Authenticates the user using the specified proxy authorization header.
*
* @param proxyAuthorizationHeaderValue
* The proxy authorization header value.
* @return <code>true</code> if the credential is acceptable, otherwise
* <code>false</code>.
*/
boolean authenticate(String userName, String password);
boolean authenticate(String proxyAuthorizationHeaderValue);

/**
* The realm value to be used in the request for proxy authentication
* ("Proxy-Authenticate" header). Returning null will cause the string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,15 +982,7 @@ private boolean authenticationRequired(HttpRequest request) {
List<String> values = request.headers().getAll(
HttpHeaders.Names.PROXY_AUTHORIZATION);
String fullValue = values.iterator().next();
String value = StringUtils.substringAfter(fullValue, "Basic ").trim();

byte[] decodedValue = BaseEncoding.base64().decode(value);

String decodedString = new String(decodedValue, Charset.forName("UTF-8"));

String userName = StringUtils.substringBefore(decodedString, ":");
String password = StringUtils.substringAfter(decodedString, ":");
if (!authenticator.authenticate(userName, password)) {
if (!authenticator.authenticate(fullValue)) {
writeAuthenticationRequired(authenticator.getRealm());
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ protected void setUp() {
protected boolean isMITM() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ protected String getPassword() {
}

@Override
public boolean authenticate(String userName, String password) {
return getUsername().equals(userName) && getPassword().equals(password);
public boolean authenticate(String proxyAuthorizationHeaderValue) {
return new TestBasicProxyAuthenticator(getUsername(), getPassword()).authenticate(proxyAuthorizationHeaderValue);
}

@Override
Expand All @@ -37,4 +37,25 @@ protected boolean isAuthenticating() {
public String getRealm() {
return null;
}

static class TestBasicProxyAuthenticator extends BasicProxyAuthenticator{

private final String username;
private final String password;

TestBasicProxyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

@Override
boolean authenticate(String username, String password) {
return this.username.equals(username) && this.password.equals(password);
}

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