Search in sources :

Example 1 with Authenticator

use of com.sun.net.httpserver.Authenticator in project jetty.project by eclipse.

the class HttpSpiContextHandler method handleAuthentication.

private void handleAuthentication(HttpServletResponse resp, HttpExchange httpExchange, Authenticator auth) throws IOException {
    Result result = auth.authenticate(httpExchange);
    if (result instanceof Authenticator.Failure) {
        int rc = ((Authenticator.Failure) result).getResponseCode();
        for (Map.Entry<String, List<String>> header : httpExchange.getResponseHeaders().entrySet()) {
            for (String value : header.getValue()) resp.addHeader(header.getKey(), value);
        }
        resp.sendError(rc);
    } else if (result instanceof Authenticator.Retry) {
        int rc = ((Authenticator.Retry) result).getResponseCode();
        for (Map.Entry<String, List<String>> header : httpExchange.getResponseHeaders().entrySet()) {
            for (String value : header.getValue()) resp.addHeader(header.getKey(), value);
        }
        resp.setStatus(rc);
        resp.flushBuffer();
    } else if (result instanceof Authenticator.Success) {
        HttpPrincipal principal = ((Authenticator.Success) result).getPrincipal();
        ((JettyExchange) httpExchange).setPrincipal(principal);
        _httpHandler.handle(httpExchange);
    }
}
Also used : HttpPrincipal(com.sun.net.httpserver.HttpPrincipal) Result(com.sun.net.httpserver.Authenticator.Result) List(java.util.List) Map(java.util.Map) Authenticator(com.sun.net.httpserver.Authenticator)

Example 2 with Authenticator

use of com.sun.net.httpserver.Authenticator in project data-prepper by opensearch-project.

the class HttpBasicDataPrepperCoreAuthenticationProviderTest method getAuthenticator_should_return_Authenticator_which_returns_true_for_matching_username_and_password.

@Test
void getAuthenticator_should_return_Authenticator_which_returns_true_for_matching_username_and_password() {
    final Authenticator authenticator = createObjectUnderTest().getAuthenticator();
    assertThat(authenticator, notNullValue());
    assertThat(authenticator, instanceOf(BasicAuthenticator.class));
    final BasicAuthenticator basicAuthenticator = (BasicAuthenticator) authenticator;
    assertThat(basicAuthenticator.checkCredentials(username, password), equalTo(true));
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) Authenticator(com.sun.net.httpserver.Authenticator) Test(org.junit.jupiter.api.Test)

Example 3 with Authenticator

use of com.sun.net.httpserver.Authenticator in project data-prepper by opensearch-project.

the class HttpBasicDataPrepperCoreAuthenticationProviderTest method getAuthenticator_should_return_Authenticator_which_returns_false_for_null_password.

@Test
void getAuthenticator_should_return_Authenticator_which_returns_false_for_null_password() {
    final Authenticator authenticator = createObjectUnderTest().getAuthenticator();
    assertThat(authenticator, notNullValue());
    assertThat(authenticator, instanceOf(BasicAuthenticator.class));
    final BasicAuthenticator basicAuthenticator = (BasicAuthenticator) authenticator;
    assertThat(basicAuthenticator.checkCredentials(username, null), equalTo(false));
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) Authenticator(com.sun.net.httpserver.Authenticator) Test(org.junit.jupiter.api.Test)

Example 4 with Authenticator

use of com.sun.net.httpserver.Authenticator in project data-prepper by opensearch-project.

the class HttpBasicDataPrepperCoreAuthenticationProviderTest method getAuthenticator_should_return_Authenticator_which_returns_false_for_null_username.

@Test
void getAuthenticator_should_return_Authenticator_which_returns_false_for_null_username() {
    final Authenticator authenticator = createObjectUnderTest().getAuthenticator();
    assertThat(authenticator, notNullValue());
    assertThat(authenticator, instanceOf(BasicAuthenticator.class));
    final BasicAuthenticator basicAuthenticator = (BasicAuthenticator) authenticator;
    assertThat(basicAuthenticator.checkCredentials(null, password), equalTo(false));
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) Authenticator(com.sun.net.httpserver.Authenticator) Test(org.junit.jupiter.api.Test)

Example 5 with Authenticator

use of com.sun.net.httpserver.Authenticator in project data-prepper by opensearch-project.

the class HttpBasicDataPrepperCoreAuthenticationProviderTest method getAuthenticator_should_return_Authenticator_which_returns_false_for_incorrect_username.

@Test
void getAuthenticator_should_return_Authenticator_which_returns_false_for_incorrect_username() {
    final Authenticator authenticator = createObjectUnderTest().getAuthenticator();
    assertThat(authenticator, notNullValue());
    assertThat(authenticator, instanceOf(BasicAuthenticator.class));
    final BasicAuthenticator basicAuthenticator = (BasicAuthenticator) authenticator;
    assertThat(basicAuthenticator.checkCredentials(UUID.randomUUID().toString(), password), equalTo(false));
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) Authenticator(com.sun.net.httpserver.Authenticator) Test(org.junit.jupiter.api.Test)

Aggregations

Authenticator (com.sun.net.httpserver.Authenticator)10 Test (org.junit.jupiter.api.Test)8 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)6 DataPrepperCoreAuthenticationProvider (com.amazon.dataprepper.pipeline.server.DataPrepperCoreAuthenticationProvider)1 PrometheusMetricsHandler (com.amazon.dataprepper.pipeline.server.PrometheusMetricsHandler)1 Result (com.sun.net.httpserver.Authenticator.Result)1 HttpHandler (com.sun.net.httpserver.HttpHandler)1 HttpPrincipal (com.sun.net.httpserver.HttpPrincipal)1 HttpServer (com.sun.net.httpserver.HttpServer)1 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)1 List (java.util.List)1 Map (java.util.Map)1