Search in sources :

Example 1 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.

the class ForwardProxyTLSServerTest method testProxyAuthentication.

private void testProxyAuthentication(String realm, ConnectHandler connectHandler, boolean includeAddress) throws Exception {
    startTLSServer(new ServerHandler());
    startProxy(connectHandler);
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    HttpProxy httpProxy = newHttpProxy();
    if (includeAddress)
        httpProxy.getIncludedAddresses().add("localhost:" + serverConnector.getLocalPort());
    httpClient.getProxyConfiguration().getProxies().add(httpProxy);
    URI uri = URI.create((proxySslContextFactory == null ? "http" : "https") + "://localhost:" + proxyConnector.getLocalPort());
    httpClient.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "proxyUser", "proxyPassword"));
    httpClient.start();
    try {
        String host = "localhost";
        String body = "BODY";
        ContentResponse response = httpClient.newRequest(host, serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(body, "UTF-8")).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = response.getContentAsString();
        Assert.assertEquals(body, content);
    } finally {
        httpClient.stop();
    }
}
Also used : HttpProxy(org.eclipse.jetty.client.HttpProxy) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) URI(java.net.URI)

Example 2 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.

the class DataSourceLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Example 3 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.

the class JdbcLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Example 4 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_BasicAuthentication_WithAuthenticationRemoved.

@Test
public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception {
    startBasic(new EmptyServerHandler());
    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(2));
    Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
    authenticationStore.addAuthentication(authentication);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    authenticationStore.removeAuthentication(authentication);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    Authentication.Result result = authenticationStore.findAuthenticationResult(request.getURI());
    Assert.assertNotNull(result);
    authenticationStore.removeAuthenticationResult(result);
    requests.set(new CountDownLatch(1));
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(401, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 5 with BasicAuthentication

use of org.eclipse.jetty.client.util.BasicAuthentication in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_RequestFailsAfterResponse.

@Test
public void test_RequestFailsAfterResponse() throws Exception {
    startBasic(new EmptyServerHandler());
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "basic");
    authenticationStore.addAuthentication(authentication);
    CountDownLatch successLatch = new CountDownLatch(1);
    CountDownLatch resultLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").content(content).onResponseSuccess(response -> successLatch.countDown());
    request.send(result -> {
        if (result.isFailed() && result.getResponseFailure() == null)
            resultLatch.countDown();
    });
    // Send some content to make sure the request is dispatched on the server.
    content.offer(ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)));
    // Wait for the response to arrive to
    // the authentication protocol handler.
    Thread.sleep(1000);
    // Trigger request failure.
    request.abort(new Exception());
    // Verify that the response was successful, it's the request that failed.
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
Also used : DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Aggregations

BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)25 URI (java.net.URI)18 Test (org.junit.Test)17 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)10 Request (org.eclipse.jetty.client.api.Request)10 IOException (java.io.IOException)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)8 ServletException (javax.servlet.ServletException)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HttpProxy (org.eclipse.jetty.client.HttpProxy)5 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpClient (org.eclipse.jetty.client.HttpClient)4 Authentication (org.eclipse.jetty.client.api.Authentication)4 Client (javax.ws.rs.client.Client)3 Response (javax.ws.rs.core.Response)3 Origin (org.eclipse.jetty.client.Origin)3