Search in sources :

Example 1 with AbstractDeviceCredentials

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

the class HonoBasicAuthHandlerTest method testPreCredentialsValidationHandlerGetsInvoked.

/**
 * Verifies that the PreCredentialsValidationHandler given for the AuthHandler is invoked
 * when authenticating.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testPreCredentialsValidationHandlerGetsInvoked() {
    final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class);
    final DeviceUser deviceUser = new DeviceUser("tenant", "device");
    // prepare authProvider
    final DeviceCredentialsAuthProvider<AbstractDeviceCredentials> authProvider = mock(DeviceCredentialsAuthProvider.class);
    doReturn(deviceCredentials).when(authProvider).getCredentials(any(JsonObject.class));
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(2);
        handler.handle(Future.succeededFuture(deviceUser));
        return null;
    }).when(authProvider).authenticate(any(AbstractDeviceCredentials.class), any(), VertxMockSupport.anyHandler());
    // prepare PreCredentialsValidationHandler
    final PreCredentialsValidationHandler<HttpContext> preCredValidationHandler = mock(PreCredentialsValidationHandler.class);
    when(preCredValidationHandler.handle(eq(deviceCredentials), any(HttpContext.class))).thenReturn(Future.succeededFuture());
    // GIVEN an auth handler with a PreCredentialsValidationHandler
    final HonoBasicAuthHandler authHandler = new HonoBasicAuthHandler(authProvider, "test", preCredValidationHandler);
    // WHEN the auth handler handles a request
    final String authorization = "BASIC " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8));
    final MultiMap headers = mock(MultiMap.class);
    when(headers.get(eq(HttpHeaders.AUTHORIZATION))).thenReturn(authorization);
    final HttpServerRequest req = mock(HttpServerRequest.class);
    when(req.headers()).thenReturn(headers);
    final HttpServerResponse resp = mock(HttpServerResponse.class);
    final RoutingContext routingContext = mock(RoutingContext.class);
    final Map<String, Object> routingContextMap = new HashMap<>();
    when(routingContext.put(any(), any())).thenAnswer(invocation -> {
        routingContextMap.put(invocation.getArgument(0), invocation.getArgument(1));
        return routingContext;
    });
    when(routingContext.get(any())).thenAnswer(invocation -> routingContextMap.get(invocation.getArgument(0)));
    when(routingContext.request()).thenReturn(req);
    when(routingContext.response()).thenReturn(resp);
    when(routingContext.currentRoute()).thenReturn(mock(Route.class));
    authHandler.handle(routingContext);
    // THEN authentication succeeds and the PreCredentialsValidationHandler has been invoked
    verify(routingContext).setUser(eq(deviceUser));
    verify(preCredValidationHandler).handle(eq(deviceCredentials), any(HttpContext.class));
}
Also used : DeviceUser(org.eclipse.hono.service.auth.DeviceUser) HashMap(java.util.HashMap) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpContext(org.eclipse.hono.service.http.HttpContext) JsonObject(io.vertx.core.json.JsonObject) PreCredentialsValidationHandler(org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler) Handler(io.vertx.core.Handler) AbstractDeviceCredentials(org.eclipse.hono.adapter.auth.device.AbstractDeviceCredentials) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) 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 Route (io.vertx.ext.web.Route)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 HashMap (java.util.HashMap)1 AbstractDeviceCredentials (org.eclipse.hono.adapter.auth.device.AbstractDeviceCredentials)1 PreCredentialsValidationHandler (org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler)1 DeviceUser (org.eclipse.hono.service.auth.DeviceUser)1 HttpContext (org.eclipse.hono.service.http.HttpContext)1 Test (org.junit.jupiter.api.Test)1