Search in sources :

Example 1 with ExecutionContextAuthHandler

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

AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 JsonObject (io.vertx.core.json.JsonObject)1 User (io.vertx.ext.auth.User)1 AuthenticationProvider (io.vertx.ext.auth.authentication.AuthenticationProvider)1 RoutingContext (io.vertx.ext.web.RoutingContext)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 Objects (java.util.Objects)1 DeviceCredentialsAuthProvider (org.eclipse.hono.adapter.auth.device.DeviceCredentialsAuthProvider)1 ExecutionContextAuthHandler (org.eclipse.hono.adapter.auth.device.ExecutionContextAuthHandler)1 PreCredentialsValidationHandler (org.eclipse.hono.adapter.auth.device.PreCredentialsValidationHandler)1 HttpContext (org.eclipse.hono.service.http.HttpContext)1