Search in sources :

Example 16 with BasicAuthentication

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());
}
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 17 with BasicAuthentication

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());
}
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 18 with BasicAuthentication

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());
}
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 19 with BasicAuthentication

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);
}
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)

Example 20 with BasicAuthentication

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();
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

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