Search in sources :

Example 1 with UsernamePasswordCredentials

use of org.eclipse.hono.adapter.auth.device.UsernamePasswordCredentials in project hono by eclipse.

the class HonoBasicAuthHandlerTest method testHandleFailsWithStatusCodeFromAuthProvider.

/**
 * Verifies that the handler returns the status code conveyed in a
 * failed {@code AuthenticationProvider} invocation in the response.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleFailsWithStatusCodeFromAuthProvider() {
    // GIVEN an auth handler configured with an auth provider that
    // fails with a 503 error code during authentication
    final DeviceCredentialsAuthProvider<UsernamePasswordCredentials> authProvider = mock(DeviceCredentialsAuthProvider.class);
    final ServiceInvocationException error = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE);
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(2);
        handler.handle(Future.failedFuture(error));
        return null;
    }).when(authProvider).authenticate(any(UsernamePasswordCredentials.class), any(), VertxMockSupport.anyHandler());
    when(authProvider.getCredentials(any(JsonObject.class))).thenReturn(UsernamePasswordCredentials.create("device1@DEFAULT_TENANT", "secret"));
    final HonoBasicAuthHandler authHandler = new HonoBasicAuthHandler(authProvider, "test");
    // WHEN trying to authenticate a request using the HTTP BASIC scheme
    final String authorization = "BASIC " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8));
    final MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.add(HttpHeaders.AUTHORIZATION, authorization);
    final HttpServerRequest req = mock(HttpServerRequest.class);
    when(req.headers()).thenReturn(headers);
    final HttpServerResponse resp = mock(HttpServerResponse.class);
    final RoutingContext ctx = mock(RoutingContext.class);
    when(ctx.request()).thenReturn(req);
    when(ctx.response()).thenReturn(resp);
    authHandler.handle(ctx);
    // THEN the request context is failed with the 503 error code
    verify(ctx).fail(error);
}
Also used : MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PreCredentialsValidationHandler(org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) UsernamePasswordCredentials(org.eclipse.hono.adapter.auth.device.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Aggregations

Handler (io.vertx.core.Handler)1 MultiMap (io.vertx.core.MultiMap)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 PreCredentialsValidationHandler (org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler)1 UsernamePasswordCredentials (org.eclipse.hono.adapter.auth.device.UsernamePasswordCredentials)1 ServerErrorException (org.eclipse.hono.client.ServerErrorException)1 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)1 Test (org.junit.jupiter.api.Test)1