Example usage for io.vertx.core Future succeededFuture

List of usage examples for io.vertx.core Future succeededFuture

Introduction

In this page you can find the example usage for io.vertx.core Future succeededFuture.

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Connects to the Credentials service using the configured client.
 *
 * @param connectHandler The handler to invoke with the outcome of the connection attempt.
 *                       If {@code null} and the connection attempt failed, this method
 *                       tries to re-connect until a connection is established.
 *///from   w  w  w.ja  v  a 2 s  .co m
protected final void connectToCredentialsService(final Handler<AsyncResult<HonoClient>> connectHandler) {

    if (credentials == null) {
        if (connectHandler != null) {
            if (registration != null) {
                // give back registration client if credentials client is not configured
                connectHandler.handle(Future.succeededFuture(registration));
            } else {
                connectHandler.handle(Future
                        .failedFuture("Neither Credentials client nor Device Registration client is set"));
            }
        }
    } else if (credentials.isConnected()) {
        LOG.debug("already connected to Credentials service");
        if (connectHandler != null) {
            connectHandler.handle(Future.succeededFuture(credentials));
        }
    } else {
        credentials.connect(createClientOptions(), connectAttempt -> {
            if (connectHandler != null) {
                connectHandler.handle(connectAttempt);
            } else {
                LOG.debug("connected to Credentials service");
            }
        }, this::onDisconnectCredentialsService);
    }
}

From source file:org.eclipse.hono.service.auth.ClaimsBasedAuthorizationService.java

License:Open Source License

@Override
public Future<Boolean> isAuthorized(final HonoUser user, final ResourceIdentifier resource,
        final Activity intent) {

    Objects.requireNonNull(user);
    Objects.requireNonNull(resource);
    Objects.requireNonNull(intent);

    if (user.isExpired()) {
        return Future.failedFuture("user information expired");
    } else {/*from   ww w  . j  a v a2 s  .c  o m*/
        return Future.succeededFuture(user.getAuthorities().isAuthorized(resource, intent));
    }
}

From source file:org.eclipse.hono.service.auth.ClaimsBasedAuthorizationService.java

License:Open Source License

@Override
public Future<Boolean> isAuthorized(final HonoUser user, final ResourceIdentifier resource,
        final String operation) {

    Objects.requireNonNull(user);
    Objects.requireNonNull(resource);
    Objects.requireNonNull(operation);

    if (user.isExpired()) {
        return Future.failedFuture("user information expired");
    } else {//  w  ww .  j  ava  2 s.c om
        return Future.succeededFuture(user.getAuthorities().isAuthorized(resource, operation));
    }
}

From source file:org.eclipse.hono.service.auth.delegating.AuthenticationServerClient.java

License:Open Source License

/**
 * Verifies username/password credentials with a remote authentication server using SASL PLAIN.
 * /*  w  w  w  . j a  v a2s  . co m*/
 * @param authzid The identity to act as.
 * @param authcid The username.
 * @param password The password.
 * @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication,
 *                                    the result contains a JWT with the the authenticated user's claims.
 */
public void verifyPlain(final String authzid, final String authcid, final String password,
        final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {

    ProtonClientOptions options = new ProtonClientOptions();
    options.setReconnectAttempts(3).setReconnectInterval(50);
    options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN);
    factory.connect(options, authcid, password, null, null, conAttempt -> {
        if (conAttempt.failed()) {
            authenticationResultHandler.handle(Future.failedFuture("cannot connect to authentication server"));
        } else {
            final ProtonConnection openCon = conAttempt.result();

            final Future<HonoUser> userTracker = Future.future();
            userTracker.setHandler(s -> {
                if (s.succeeded()) {
                    authenticationResultHandler.handle(Future.succeededFuture(s.result()));
                } else {
                    authenticationResultHandler.handle(Future.failedFuture(s.cause()));
                }
                ProtonConnection con = conAttempt.result();
                if (con != null) {
                    LOG.debug("closing connection to authentication server");
                    con.close();
                }
            });

            vertx.setTimer(5000, tid -> {
                if (!userTracker.isComplete()) {
                    userTracker.fail("time out reached while waiting for token from authentication server");
                }
            });

            getToken(openCon, userTracker);
        }
    });
}

From source file:org.eclipse.hono.service.auth.device.CredentialsApiAuthProvider.java

License:Open Source License

@Override
public final void authenticate(final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) {

    final DeviceCredentials credentials = getCredentials(Objects.requireNonNull(authInfo));
    if (credentials == null) {
        resultHandler.handle(Future.failedFuture(
                new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "malformed credentials")));
    } else {/*from www .  j  a v a  2 s .c o m*/
        authenticate(credentials, s -> {
            if (s.succeeded()) {
                resultHandler.handle(Future.succeededFuture(s.result()));
            } else {
                resultHandler.handle(Future.failedFuture(s.cause()));
            }
        });
    }
}

From source file:org.eclipse.hono.service.auth.device.Device.java

License:Open Source License

/**
 * Checks if this device has a particular authority.
 * <p>//from w w  w  . j  a va2 s.c  o m
 * In order for the check to succeed, the JWT must
 * <ul>
 * <li>not be expired</li>
 * <li>contain the given authorities in its <em>aut</em> claim</li>
 * </ul>
 * 
 * @param authority The authority to check for.
 * @param resultHandler The handler to notify about the outcome of the check.
 */
@Override
public User isAuthorized(final String authority, final Handler<AsyncResult<Boolean>> resultHandler) {
    for (final Object item : authorities) {
        if (authority.equals(item)) {
            resultHandler.handle(Future.succeededFuture(Boolean.TRUE));
            return this;
        }
    }
    resultHandler.handle(Future.succeededFuture(Boolean.FALSE));
    return this;
}

From source file:org.eclipse.hono.service.auth.device.HonoChainAuthHandler.java

License:Open Source License

private void iterate(final int idx, final RoutingContext ctx, final HttpStatusException lastException,
        final Handler<AsyncResult<JsonObject>> handler) {

    // stop condition
    if (idx >= handlers.size()) {
        // no more providers, means that we failed to find a provider capable of performing this operation
        handler.handle(Future.failedFuture(lastException));
        return;//ww w  . java 2  s.  com
    }

    // parse the request in order to extract the credentials object
    final AuthHandler authHandler = handlers.get(idx);

    authHandler.parseCredentials(ctx, res -> {
        if (res.failed()) {
            if (res.cause() instanceof HttpStatusException) {
                final HttpStatusException exception = (HttpStatusException) res.cause();
                switch (exception.getStatusCode()) {
                case 302:
                case 400:
                case 401:
                case 403:
                    // try again with next provider since we know what kind of error it is
                    iterate(idx + 1, ctx, exception, handler);
                    return;
                }
            }
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }

        // setup the desired auth provider if we can
        if (authHandler instanceof HonoAuthHandler) {
            ctx.put(AUTH_PROVIDER_CONTEXT_KEY, ((HonoAuthHandler) authHandler).authProvider);
        }
        handler.handle(Future.succeededFuture(res.result()));
    });
}

From source file:org.eclipse.hono.service.auth.EventBusBasedAuthorizationService.java

License:Open Source License

@Override
public Future<Boolean> isAuthorized(final HonoUser user, final ResourceIdentifier resource,
        final String operation) {
    return Future.succeededFuture(Boolean.FALSE);
}

From source file:org.eclipse.hono.service.auth.impl.FileBasedAuthenticationService.java

License:Open Source License

private void verify(final String authenticationId, final JsonObject user, final String authorizationId,
        final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {

    JsonObject effectiveUser = user;/*from  w w  w  .  j av  a2s  . c  o  m*/
    String effectiveAuthorizationId = authenticationId;
    if (authorizationId != null && !authorizationId.isEmpty() && isAuthorizedToImpersonate(user)) {
        JsonObject impersonatedUser = users.get(authorizationId);
        if (impersonatedUser != null) {
            effectiveUser = impersonatedUser;
            effectiveAuthorizationId = authorizationId;
            log.debug("granting authorization id specified by client");
        } else {
            log.debug(
                    "no user found for authorization id provided by client, granting authentication id instead");
        }
    }
    final Authorities grantedAuthorities = getAuthorities(effectiveUser);
    final String grantedAuthorizationId = effectiveAuthorizationId;
    final Instant tokenExpirationTime = Instant.now().plus(tokenFactory.getTokenLifetime());
    final String token = tokenFactory.createToken(grantedAuthorizationId, grantedAuthorities);
    HonoUser honoUser = new HonoUser() {

        @Override
        public String getName() {
            return grantedAuthorizationId;
        }

        @Override
        public String getToken() {
            return token;
        }

        @Override
        public Authorities getAuthorities() {
            return grantedAuthorities;
        }

        @Override
        public boolean isExpired() {
            return !Instant.now().isBefore(tokenExpirationTime);
        }
    };
    authenticationResultHandler.handle(Future.succeededFuture(honoUser));
}

From source file:org.eclipse.hono.service.credentials.BaseCredentialsService.java

License:Open Source License

private void handleUnimplementedOperation(
        final Handler<AsyncResult<CredentialsResult<JsonObject>>> resultHandler) {
    resultHandler//from   w  w w .  j a  va2  s . c o  m
            .handle(Future.succeededFuture(CredentialsResult.from(HTTP_NOT_IMPLEMENTED, (JsonObject) null)));
}