use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.
the class HttpClientProxyTest method testProxyAuthentication.
@Test
public void testProxyAuthentication() throws Exception {
final String user = "foo";
final String password = "bar";
final String credentials = B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
final String serverHost = "server";
final String realm = "test_realm";
final int status = HttpStatus.NO_CONTENT_204;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + realm + "\"");
} else {
String prefix = "Basic ";
if (authorization.startsWith(prefix)) {
String attempt = authorization.substring(prefix.length());
if (credentials.equals(attempt))
response.setStatus(status);
}
}
}
});
String proxyHost = "localhost";
int proxyPort = connector.getLocalPort();
// Any port will do for these tests - just not the same as the proxy
int serverPort = proxyPort + 1;
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
// No Authentication available => 407
Assert.assertEquals(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407, response1.getStatus());
// Add authentication...
URI uri = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, user, password));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.incrementAndGet();
}
});
// ...and perform the request again => 407 + 204
ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response2.getStatus());
Assert.assertEquals(2, requests.get());
// Now the authentication result is cached => 204
requests.set(0);
ContentResponse response3 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response3.getStatus());
Assert.assertEquals(1, requests.get());
}
use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.
the class HttpClientProxyTest method testProxyAuthenticationWithRedirect.
@Test
public void testProxyAuthenticationWithRedirect() throws Exception {
String user = "foo";
String password = "bar";
String credentials = B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
String proxyHost = "localhost";
String serverHost = "server";
int serverPort = HttpScheme.HTTP.is(scheme) ? 80 : 443;
String realm = "test_realm";
int status = HttpStatus.NO_CONTENT_204;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
if (target.startsWith("/proxy")) {
String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + realm + "\"");
} else {
String prefix = "Basic ";
if (authorization.startsWith(prefix)) {
String attempt = authorization.substring(prefix.length());
if (credentials.equals(attempt)) {
// Change also the host, to verify that proxy authentication works in this case too.
response.sendRedirect(scheme + "://127.0.0.1:" + serverPort + "/server");
}
}
}
} else if (target.startsWith("/server")) {
response.setStatus(status);
} else {
response.sendError(HttpStatus.INTERNAL_SERVER_ERROR_500);
}
}
});
int proxyPort = connector.getLocalPort();
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).path("/proxy").timeout(5, TimeUnit.SECONDS).send();
// No Authentication available => 407.
Assert.assertEquals(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407, response1.getStatus());
// Add authentication...
URI uri = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, user, password));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.incrementAndGet();
}
});
// ...and perform the request again => 407 + 302 + 204.
ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).path("/proxy").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response2.getStatus());
Assert.assertEquals(3, requests.get());
// Now the authentication result is cached => 204.
requests.set(0);
ContentResponse response3 = client.newRequest(serverHost, serverPort).scheme(scheme).path("/server").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response3.getStatus());
Assert.assertEquals(1, requests.get());
}
use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.
the class HttpClientProxyTest method testProxyAuthenticationWithExplicitAuthorizationHeader.
@Test
public void testProxyAuthenticationWithExplicitAuthorizationHeader() throws Exception {
String proxyRealm = "proxyRealm";
String serverRealm = "serverRealm";
int status = HttpStatus.NO_CONTENT_204;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + proxyRealm + "\"");
} else {
authorization = request.getHeader(HttpHeader.AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.UNAUTHORIZED_401);
response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"" + serverRealm + "\"");
} else {
response.setStatus(status);
}
}
}
});
String proxyHost = "localhost";
int proxyPort = connector.getLocalPort();
String serverHost = "server";
int serverPort = proxyPort + 1;
URI proxyURI = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.incrementAndGet();
}
});
// Make a request, expect 407 + 204.
ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).header(HttpHeader.AUTHORIZATION, "Basic foobar").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response1.getStatus());
Assert.assertEquals(2, requests.get());
// Make again the request, authentication is cached, expect 204.
requests.set(0);
ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).header(HttpHeader.AUTHORIZATION, "Basic foobar").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response2.getStatus());
Assert.assertEquals(1, requests.get());
}
use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.
the class HttpAuthenticationStoreTest method testFindAuthenticationWithDefaultHTTPPort.
@Test
public void testFindAuthenticationWithDefaultHTTPPort() throws Exception {
AuthenticationStore store = new HttpAuthenticationStore();
URI uri1 = URI.create("http://host:80");
URI uri2 = URI.create("http://host");
String realm = "realm";
store.addAuthentication(new BasicAuthentication(uri1, realm, "user", "password"));
Authentication result = store.findAuthentication("Basic", uri2, realm);
Assert.assertNotNull(result);
store.clearAuthentications();
// Flip the URIs.
uri1 = URI.create("https://server/");
uri2 = URI.create("https://server:443/path");
store.addAuthentication(new DigestAuthentication(uri1, realm, "user", "password"));
result = store.findAuthentication("Digest", uri2, realm);
Assert.assertNotNull(result);
}
use of org.eclipse.jetty.client.util.BasicAuthentication in project jersey by jersey.
the class AuthTest method testAuthPost.
@Test
public void testAuthPost() {
ClientConfig config = new ClientConfig();
config.property(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, new BasicAuthentication(getBaseUri(), "WallyWorld", "name", "password"));
config.connectorProvider(new JettyConnectorProvider());
Client client = ClientBuilder.newClient(config);
Response response = client.target(getBaseUri()).path(PATH).request().post(Entity.text("POST"));
assertEquals("POST", response.readEntity(String.class));
client.close();
}
Aggregations