Search in sources :

Example 1 with HonoUserAdapter

use of org.eclipse.hono.auth.HonoUserAdapter in project hono by eclipse.

the class AuthenticationServerClient method getToken.

private Future<HonoUser> getToken(final ProtonConnection openCon) {
    final Promise<HonoUser> result = Promise.promise();
    final ProtonMessageHandler messageHandler = (delivery, message) -> {
        final String type = MessageHelper.getApplicationProperty(message.getApplicationProperties(), AuthenticationConstants.APPLICATION_PROPERTY_TYPE, String.class);
        if (AuthenticationConstants.TYPE_AMQP_JWT.equals(type)) {
            final String payload = MessageHelper.getPayloadAsString(message);
            if (payload != null) {
                final HonoUser user = new HonoUserAdapter() {

                    @Override
                    public String getToken() {
                        return payload;
                    }
                };
                LOG.debug("successfully retrieved token from Authentication service");
                result.complete(user);
            } else {
                result.fail(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, "message from Authentication service contains no body"));
            }
        } else {
            result.fail(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, "Authentication service issued unsupported token [type: " + type + "]"));
        }
    };
    openReceiver(openCon, messageHandler).onComplete(attempt -> {
        if (attempt.succeeded()) {
            vertx.setTimer(5000, tid -> {
                result.tryFail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "time out reached while waiting for token from Authentication service"));
            });
            LOG.debug("opened receiver link to Authentication service, waiting for token ...");
        } else {
            result.fail(attempt.cause());
        }
    });
    return result.future();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) HonoUserAdapter(org.eclipse.hono.auth.HonoUserAdapter) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) MechanismMismatchException(io.vertx.proton.sasl.MechanismMismatchException) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) HonoUser(org.eclipse.hono.auth.HonoUser) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) AuthenticationConstants(org.eclipse.hono.util.AuthenticationConstants) Objects(java.util.Objects) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Optional(java.util.Optional) AuthenticationException(javax.security.sasl.AuthenticationException) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) HonoUser(org.eclipse.hono.auth.HonoUser) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) HonoUserAdapter(org.eclipse.hono.auth.HonoUserAdapter)

Example 2 with HonoUserAdapter

use of org.eclipse.hono.auth.HonoUserAdapter in project hono by eclipse.

the class AuthenticationServerClient method getToken.

private void getToken(final ProtonConnection openCon, final Future<HonoUser> authResult) {
    final ProtonMessageHandler messageHandler = (delivery, message) -> {
        String type = MessageHelper.getApplicationProperty(message.getApplicationProperties(), AuthenticationConstants.APPLICATION_PROPERTY_TYPE, String.class);
        if (AuthenticationConstants.TYPE_AMQP_JWT.equals(type)) {
            Section body = message.getBody();
            if (body instanceof AmqpValue) {
                final String token = ((AmqpValue) body).getValue().toString();
                HonoUser user = new HonoUserAdapter() {

                    @Override
                    public String getToken() {
                        return token;
                    }
                };
                LOG.debug("successfully retrieved token from Authentication service");
                authResult.complete(user);
            } else {
                authResult.fail("message from Authentication service contains no body");
            }
        } else {
            authResult.fail("Authentication service issued unsupported token [type: " + type + "]");
        }
    };
    openReceiver(openCon, messageHandler).compose(openReceiver -> {
        LOG.debug("opened receiver link to Authentication service, waiting for token ...");
    }, authResult);
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) HonoUserAdapter(org.eclipse.hono.auth.HonoUserAdapter) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) Autowired(org.springframework.beans.factory.annotation.Autowired) HonoUser(org.eclipse.hono.auth.HonoUser) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) AuthenticationConstants(org.eclipse.hono.service.auth.AuthenticationConstants) Objects(java.util.Objects) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) Section(org.apache.qpid.proton.amqp.messaging.Section) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Qualifier(org.springframework.beans.factory.annotation.Qualifier) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) HonoUser(org.eclipse.hono.auth.HonoUser) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) HonoUserAdapter(org.eclipse.hono.auth.HonoUserAdapter) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Vertx (io.vertx.core.Vertx)2 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)2 ProtonConnection (io.vertx.proton.ProtonConnection)2 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)2 ProtonReceiver (io.vertx.proton.ProtonReceiver)2 Objects (java.util.Objects)2 HonoUser (org.eclipse.hono.auth.HonoUser)2 HonoUserAdapter (org.eclipse.hono.auth.HonoUserAdapter)2 ConnectionFactory (org.eclipse.hono.connection.ConnectionFactory)2 MessageHelper (org.eclipse.hono.util.MessageHelper)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Promise (io.vertx.core.Promise)1 MechanismMismatchException (io.vertx.proton.sasl.MechanismMismatchException)1 HttpURLConnection (java.net.HttpURLConnection)1 Optional (java.util.Optional)1 AuthenticationException (javax.security.sasl.AuthenticationException)1