Search in sources :

Example 6 with BasicAuthentication

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

the class HttpClientProxyTest method testProxyAuthenticationWithServerAuthentication.

@Test
public void testProxyAuthenticationWithServerAuthentication() 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"));
    URI serverURI = URI.create(scheme + "://" + serverHost + ":" + serverPort);
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(serverURI, serverRealm, "serverUser", "serverPassword"));
    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 + 401 + 204.
    ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response1.getStatus());
    Assert.assertEquals(3, requests.get());
    // Make again the request, authentication is cached, expect 204.
    requests.set(0);
    ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(status, response2.getStatus());
    Assert.assertEquals(1, requests.get());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 7 with BasicAuthentication

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

the class Usage method testBasicAuthentication.

@Test
public void testBasicAuthentication() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    URI uri = URI.create("http://localhost:8080/secure");
    // Setup Basic authentication credentials for TestRealm
    client.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, "TestRealm", "username", "password"));
    // One liner to send the request
    ContentResponse response = client.newRequest(uri).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) URI(java.net.URI) Test(org.junit.Test)

Example 8 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 9 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 10 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)

Aggregations

BasicAuthentication (org.eclipse.jetty.client.util.BasicAuthentication)23 URI (java.net.URI)17 Test (org.junit.Test)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)9 Request (org.eclipse.jetty.client.api.Request)9 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)6 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpClient (org.eclipse.jetty.client.HttpClient)4 HttpProxy (org.eclipse.jetty.client.HttpProxy)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