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);
});
}
Aggregations