Example usage for io.vertx.core.json JsonObject JsonObject

List of usage examples for io.vertx.core.json JsonObject JsonObject

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject JsonObject.

Prototype

public JsonObject() 

Source Link

Document

Create a new, empty instance

Usage

From source file:io.apiman.gateway.engine.vertxebinmemory.apis.Head.java

License:Apache License

default JsonObject asJson() {
    return new JsonObject().put("type", type()).put("action", action()).put("body", body()).put("uuid", uuid());
}

From source file:io.apiman.gateway.platforms.vertx3.api.auth.AuthFactory.java

License:Apache License

public static AuthHandler getAuth(Vertx vertx, Router router, VertxEngineConfig apimanConfig, JsonObject auth) {
    String type = auth.getString("type", "NONE");
    JsonObject authConfig = auth.getJsonObject("config", new JsonObject());

    switch (AuthType.getType(type)) {
    case BASIC:/*from www  .j  av  a  2s .  c  o  m*/
        return BasicAuth.create(authConfig);
    case NONE:
        return NoneAuth.create();
    case KEYCLOAK:
        return KeycloakOAuthFactory.create(vertx, router, apimanConfig, authConfig);
    default:
        return NoneAuth.create();
    }
}

From source file:io.apiman.gateway.platforms.vertx3.api.auth.KeycloakOAuthFactory.java

License:Apache License

private static AuthHandler directGrant(Vertx vertx, VertxEngineConfig apimanConfig, JsonObject authConfig,
        OAuth2FlowType flowType, String role) {
    return new AuthHandler() {

        @Override/*  ww  w . ja  v  a2 s. com*/
        public void handle(RoutingContext context) {
            try {
                String[] auth = Basic.decodeWithScheme(context.request().getHeader("Authorization"));
                doBasic2Oauth(context, role, auth[0], auth[1]);
            } catch (RuntimeException e) {
                handle400(context, e.getMessage());
            }
        }

        private void doBasic2Oauth(RoutingContext context, String role, String username, String password) {
            JsonObject params = new JsonObject().put("username", username).put("password", password);

            OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, authConfig);
            oauth2.getToken(params, tokenResult -> {
                if (tokenResult.succeeded()) {
                    log.debug("OAuth2 Keycloak exchange succeeded.");
                    AccessToken token = tokenResult.result();
                    token.isAuthorised(role, res -> {
                        if (res.result()) {
                            context.next();
                        } else {
                            String message = MessageFormat.format("User {0} does not have required role: {1}.",
                                    username, role);
                            log.error(message);
                            handle403(context, "insufficient_scope", message);
                        }
                    });
                } else {
                    String message = tokenResult.cause().getMessage();
                    log.error("Access Token Error: {0}.", message);
                    handle401(context, "invalid_token", message);
                }
            });
        }

        private void handle400(RoutingContext context, String message) {
            context.response().setStatusMessage(message);
            context.fail(400);
        }

        private void handle401(RoutingContext context, String error, String message) {
            String value = MessageFormat.format("Basic realm=\"{0}\" error=\"{1}\" error_message=\"{2}\"",
                    "apiman-gw", error, message);
            context.response().putHeader("WWW-Authenticate", value);
            context.fail(401);
        }

        private void handle403(RoutingContext context, String error, String message) {
            String value = MessageFormat.format("Basic realm=\"{0}\" error=\"{1}\" error_message=\"{2}\"",
                    "apiman-gw", error, message);
            context.response().putHeader("WWW-Authenticate", value);
            context.fail(403);
        }

        @Override
        public AuthHandler addAuthority(String authority) {
            return this;
        }

        @Override
        public AuthHandler addAuthorities(Set<String> authorities) {
            return this;
        }
    };
}

From source file:io.apiman.gateway.platforms.vertx3.api.IRouteBuilder.java

License:Apache License

default <T extends Throwable> void error(RoutingContext context, HttpResponseStatus code, String message,
        T object) {//from  w w  w  .ja va2  s .com
    HttpServerResponse response = context.response().setStatusCode(code.code());
    response.putHeader("X-API-Gateway-Error", "true");

    if (message == null) {
        response.setStatusMessage(code.reasonPhrase());
    } else {
        response.setStatusMessage(message);
    }

    if (object != null) {
        JsonObject errorResponse = new JsonObject().put("errorType", object.getClass().getSimpleName())
                .put("message", object.getMessage());

        response.setChunked(true).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
                .end(errorResponse.toString(), "UTF-8");
    } else {
        response.end();
    }
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

public String getKeyStore() {
    return config.getJsonObject(SSL, new JsonObject()).getJsonObject(SSL_KEYSTORE, new JsonObject())
            .getString(SSL_PATH);//from  w  ww  .j a  v a 2  s  .com
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

public String getKeyStorePassword() {
    return config.getJsonObject(SSL, new JsonObject()).getJsonObject(SSL_KEYSTORE, new JsonObject())
            .getString(API_PASSWORD);/*from  w  w  w.j a v  a2  s  . com*/
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

public String getTrustStore() {
    return config.getJsonObject(SSL, new JsonObject()).getJsonObject(SSL_TRUSTSTORE, new JsonObject())
            .getString(SSL_PATH);/*from   w w w.j a  va2  s.c  om*/
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

public String getTrustStorePassword() {
    return config.getJsonObject(SSL, new JsonObject()).getJsonObject(SSL_TRUSTSTORE, new JsonObject())
            .getString(API_PASSWORD);//from w w w. j av  a 2  s.  c o  m
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

public JsonObject getAuth() {
    return config.getJsonObject(API_AUTH, new JsonObject());
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

License:Apache License

protected String getClassname(JsonObject obj, String prefix) {
    String clazzName = System.getProperty(prefix);
    // TODO Something of a hack because the constants may assume apiman-gateway prefix, which isn't in the vert.x JSON.
    String strippedPrefix = StringUtils.substringAfter(prefix, "apiman-gateway.");
    String filteredPrefix = strippedPrefix.isEmpty() ? prefix : strippedPrefix;

    if (clazzName == null)
        return obj.getJsonObject(filteredPrefix, new JsonObject()).getString(GATEWAY_CLASS);

    return clazzName;
}