Example usage for java.util.concurrent CompletableFuture completedFuture

List of usage examples for java.util.concurrent CompletableFuture completedFuture

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture completedFuture.

Prototype

public static <U> CompletableFuture<U> completedFuture(U value) 

Source Link

Document

Returns a new CompletableFuture that is already completed with the given value.

Usage

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<EvalNode> getValueFromParentContext(Context context, String valueName) {
    while (context != null) {
        if (context.contains(valueName)) {
            return context.getValue(valueName);
        }/*  w ww.  j a  v  a 2 s .  co  m*/
        context = context.getParent();
    }
    return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
}

From source file:ru.histone.v2.evaluator.EvalUtils.java

public static CompletableFuture<EvalNode> getValue(Object v) {
    return CompletableFuture.completedFuture(createEvalNode(v));
}

From source file:ru.histone.v2.rtti.RunTimeTypeInfo.java

public CompletableFuture<EvalNode> callFunction(String baseUri, HistoneType type, String funcName,
        List<EvalNode> args) {
    final Function f = getFunc(type, funcName);
    if (f.isAsync()) {
        return CompletableFuture.completedFuture(null).thenComposeAsync((x) -> f.execute(baseUri, args),
                executor);/*from   www .ja v  a 2 s. co  m*/
    }
    return f.execute(baseUri, args);
}

From source file:tech.beshu.ror.acl.blocks.rules.impl.AuthKeyUnixAsyncRule.java

@Override
protected CompletableFuture<Boolean> authenticate(String username, String password) {
    return CompletableFuture.completedFuture(authenticateSync(username, password));
}

From source file:tech.beshu.ror.acl.blocks.rules.impl.JwtAuthSyncRule.java

@Override
public CompletableFuture<RuleExitResult> match(__old_RequestContext rc) {
    Optional<String> token = Optional.of(rc.getHeaders()).map(m -> m.get(settings.getHeaderName()))
            .flatMap(JwtAuthSyncRule::extractToken);

    /*//  ww w .  j a v a2 s.com
      JWT ALGO    FAMILY
      =======================
      NONE        None
            
      HS256       HMAC
      HS384       HMAC
      HS512       HMAC
            
      RS256       RSA
      RS384       RSA
      RS512       RSA
      PS256       RSA
      PS384       RSA
      PS512       RSA
            
      ES256       EC
      ES384       EC
      ES512       EC
    */

    if (!token.isPresent()) {
        logger.debug("Authorization header is missing or does not contain a bearer token");
        return CompletableFuture.completedFuture(NO_MATCH);
    }

    try {

        JwtParser parser = Jwts.parser();

        // Defaulting to HMAC for backward compatibility
        String algoFamily = settings.getAlgo().map(String::toUpperCase).orElse("HMAC");
        if (settings.getKey() == null) {
            algoFamily = "NONE";
        }

        if (!"NONE".equals(algoFamily)) {

            if ("RSA".equals(algoFamily)) {
                try {
                    byte[] keyBytes = Base64.decodeBase64(settings.getKey());
                    KeyFactory kf = KeyFactory.getInstance("RSA");
                    PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(keyBytes));
                    parser.setSigningKey(pubKey);
                } catch (GeneralSecurityException gso) {
                    throw new RuntimeException(gso);
                }
            }

            else if ("EC".equals(algoFamily)) {
                try {
                    byte[] keyBytes = Base64.decodeBase64(settings.getKey());
                    KeyFactory kf = KeyFactory.getInstance("EC");
                    PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(keyBytes));
                    parser.setSigningKey(pubKey);
                } catch (GeneralSecurityException gso) {
                    throw new RuntimeException(gso);
                }
            }

            else if ("HMAC".equals(algoFamily)) {
                parser.setSigningKey(settings.getKey());
            } else {
                throw new RuntimeException("unrecognised algorithm family " + algoFamily
                        + ". Should be either of: HMAC, EC, RSA, NONE");
            }
        }

        Claims jws;
        if (settings.getKey() != null) {
            jws = parser.parseClaimsJws(token.get()).getBody();
        } else {
            String[] ar = token.get().split("\\.");
            if (ar.length < 2) {
                // token is not a valid JWT
                return CompletableFuture.completedFuture(NO_MATCH);
            }
            String tokenNoSig = ar[0] + "." + ar[1] + ".";
            jws = parser.parseClaimsJwt(tokenNoSig).getBody();
        }

        Claims finalJws = jws;
        Optional<String> user = settings.getUserClaim().map(claim -> finalJws.get(claim, String.class));
        if (settings.getUserClaim().isPresent())
            if (!user.isPresent()) {
                return CompletableFuture.completedFuture(NO_MATCH);
            } else {
                rc.setLoggedInUser(new LoggedUser(user.get()));
            }

        Optional<Set<String>> roles = this.extractRoles(jws);
        if (settings.getRolesClaim().isPresent() && !roles.isPresent()) {
            return CompletableFuture.completedFuture(NO_MATCH);
        }
        if (!settings.getRoles().isEmpty()) {
            if (!roles.isPresent()) {
                return CompletableFuture.completedFuture(NO_MATCH);
            } else {
                Set<String> r = roles.get();
                if (r.isEmpty() || Sets.intersection(r, settings.getRoles()).isEmpty())
                    return CompletableFuture.completedFuture(NO_MATCH);
            }
        }

        if (settings.getExternalValidator().isPresent()) {
            return httpClient.authenticate("x", token.get()).thenApply(resp -> resp ? MATCH : NO_MATCH);
        }
        return CompletableFuture.completedFuture(MATCH);

    } catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | SignatureException e) {
        return CompletableFuture.completedFuture(NO_MATCH);
    }
}