Search in sources :

Example 1 with PreCredentialsValidationHandler

use of org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler 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)

Example 2 with PreCredentialsValidationHandler

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

the class HonoBasicAuthHandler method authenticate.

@Override
public void authenticate(final RoutingContext context, final Handler<AsyncResult<User>> handler) {
    parseAuthorization(context, parseAuthorization -> {
        if (parseAuthorization.failed()) {
            handler.handle(Future.failedFuture(parseAuthorization.cause()));
            return;
        }
        final String suser;
        final String spass;
        try {
            // decode the payload
            final String decoded = new String(Base64.getDecoder().decode(parseAuthorization.result()));
            final int colonIdx = decoded.indexOf(":");
            if (colonIdx != -1) {
                suser = decoded.substring(0, colonIdx);
                spass = decoded.substring(colonIdx + 1);
            } else {
                suser = decoded;
                spass = null;
            }
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(new HttpException(400, e)));
            return;
        }
        final var credentials = new JsonObject().put("username", suser).put("password", spass);
        final ExecutionContextAuthHandler<HttpContext> authHandler = new ExecutionContextAuthHandler<>((DeviceCredentialsAuthProvider<?>) authProvider, preCredentialsValidationHandler) {

            @Override
            public Future<JsonObject> parseCredentials(final HttpContext context) {
                return Future.succeededFuture(credentials);
            }
        };
        authHandler.authenticateDevice(HttpContext.from(context)).map(deviceUser -> (User) deviceUser).onComplete(handler);
    });
}
Also used : HttpContext(org.eclipse.hono.service.http.HttpContext) AuthenticationProvider(io.vertx.ext.auth.authentication.AuthenticationProvider) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) BasicAuthHandler(io.vertx.ext.web.handler.BasicAuthHandler) Objects(java.util.Objects) Base64(java.util.Base64) User(io.vertx.ext.auth.User) DeviceCredentialsAuthProvider(org.eclipse.hono.adapter.auth.device.DeviceCredentialsAuthProvider) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) PreCredentialsValidationHandler(org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler) Handler(io.vertx.core.Handler) HttpException(io.vertx.ext.web.handler.HttpException) HTTPAuthorizationHandler(io.vertx.ext.web.handler.impl.HTTPAuthorizationHandler) ExecutionContextAuthHandler(org.eclipse.hono.adapter.auth.device.ExecutionContextAuthHandler) User(io.vertx.ext.auth.User) ExecutionContextAuthHandler(org.eclipse.hono.adapter.auth.device.ExecutionContextAuthHandler) HttpContext(org.eclipse.hono.service.http.HttpContext) JsonObject(io.vertx.core.json.JsonObject) HttpException(io.vertx.ext.web.handler.HttpException)

Aggregations

Handler (io.vertx.core.Handler)2 JsonObject (io.vertx.core.json.JsonObject)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 PreCredentialsValidationHandler (org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler)2 HttpContext (org.eclipse.hono.service.http.HttpContext)2 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 MultiMap (io.vertx.core.MultiMap)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 User (io.vertx.ext.auth.User)1 AuthenticationProvider (io.vertx.ext.auth.authentication.AuthenticationProvider)1 Route (io.vertx.ext.web.Route)1 BasicAuthHandler (io.vertx.ext.web.handler.BasicAuthHandler)1 HttpException (io.vertx.ext.web.handler.HttpException)1 HTTPAuthorizationHandler (io.vertx.ext.web.handler.impl.HTTPAuthorizationHandler)1 Base64 (java.util.Base64)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 AbstractDeviceCredentials (org.eclipse.hono.adapter.auth.device.AbstractDeviceCredentials)1