Fixes #409 (Http client authentication with proxy server)

Made BasicResult a public static class so that it can be used by
applications via AuthenticationStore.addAuthenticationResult().
This commit is contained in:
Simone Bordet 2016-03-16 15:52:56 +01:00
parent 3c0b654141
commit be7c50fa51
2 changed files with 51 additions and 6 deletions

View File

@ -63,25 +63,41 @@ public class BasicAuthentication extends AbstractAuthentication
@Override
public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context)
{
String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
return new BasicResult(headerInfo.getHeader(), value);
return new BasicResult(getURI(), headerInfo.getHeader(), user, password);
}
private class BasicResult implements Result
/**
* Basic authentication result.
* <p>
* Application may utilize this class directly via
* {@link AuthenticationStore#addAuthenticationResult(Result)}
* to perform preemptive authentication, that is immediately
* sending the authorization header based on the fact that the
* URI is known to require authentication and that username
* and password are known a priori.
*/
public static class BasicResult implements Result
{
private final URI uri;
private final HttpHeader header;
private final String value;
public BasicResult(HttpHeader header, String value)
public BasicResult(URI uri, String user, String password)
{
this(uri, HttpHeader.AUTHORIZATION, user, password);
}
public BasicResult(URI uri, HttpHeader header, String user, String password)
{
this.uri = uri;
this.header = header;
this.value = value;
this.value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
}
@Override
public URI getURI()
{
return BasicAuthentication.this.getURI();
return uri;
}
@Override

View File

@ -366,4 +366,33 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
@Test
public void test_PreemptedAuthentication() throws Exception
{
startBasic(new EmptyServerHandler());
AuthenticationStore authenticationStore = client.getAuthenticationStore();
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
authenticationStore.addAuthenticationResult(new BasicAuthentication.BasicResult(uri, "basic", "basic"));
AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter()
{
@Override
public void onSuccess(Request request)
{
requests.incrementAndGet();
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/secure")
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(1, requests.get());
}
}