Search in sources :

Example 6 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStringContent.

@Test
public void testServerWithHttpClientStringContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new BytesContentProvider(__message.getBytes("UTF8")));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(__message, _received);
        Assert.assertEquals(200, response.getStatus());
    } finally {
        client.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 7 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_Authentication.

private void test_Authentication(Authentication authentication) throws Exception {
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    final AtomicReference<CountDownLatch> requests = new AtomicReference<>(new CountDownLatch(1));
    Request.Listener.Adapter requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    // Request without Authentication causes a 401
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
    ContentResponse response = request.timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(401, response.getStatus());
    Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS));
    client.getRequestListeners().remove(requestListener);
    authenticationStore.addAuthentication(authentication);
    requests.set(new CountDownLatch(2));
    requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    // Request with authentication causes a 401 (no previous successful authentication) + 200
    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));
    client.getRequestListeners().remove(requestListener);
    requests.set(new CountDownLatch(1));
    requestListener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            requests.get().countDown();
        }
    };
    client.getRequestListeners().add(requestListener);
    // Further requests do not trigger 401 because there is a previous successful authentication
    // Remove existing header to be sure it's added by the implementation
    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));
    client.getRequestListeners().remove(requestListener);
}
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) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Example 8 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_PreemptedAuthentication.

@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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 9 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method test_BasicAuthentication_WithWrongPassword.

@Test
public void test_BasicAuthentication_WithWrongPassword() throws Exception {
    startBasic(new EmptyServerHandler());
    AuthenticationStore authenticationStore = client.getAuthenticationStore();
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort());
    BasicAuthentication authentication = new BasicAuthentication(uri, realm, "basic", "wrong");
    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(401, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 10 with AuthenticationStore

use of org.eclipse.jetty.client.api.AuthenticationStore 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);
}
Also used : Authentication(org.eclipse.jetty.client.api.Authentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Aggregations

AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)11 URI (java.net.URI)8 Request (org.eclipse.jetty.client.api.Request)8 BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)8 Test (org.junit.Test)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)6 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 HttpClient (org.eclipse.jetty.client.HttpClient)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Authentication (org.eclipse.jetty.client.api.Authentication)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)2 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)1 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)1