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:examples.AuthJWTExamples.java

License:Open Source License

public void example6(Vertx vertx) {

    JsonObject config = new JsonObject().put("keyStore",
            new JsonObject().put("path", "keystore.jceks").put("type", "jceks").put("password", "secret"));

    AuthProvider provider = JWTAuth.create(vertx, config);
}

From source file:examples.AuthJWTExamples.java

License:Open Source License

public void example7(Vertx vertx, String username, String password) {

    JsonObject config = new JsonObject().put("keyStore",
            new JsonObject().put("path", "keystore.jceks").put("type", "jceks").put("password", "secret"));

    JWTAuth provider = JWTAuth.create(vertx, config);

    // on the verify endpoint once you verify the identity of the user by its username/password
    if ("paulo".equals(username) && "super_secret".equals(password)) {
        String token = provider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
        // now for any request to protected resources you should pass this string in the HTTP header Authorization as:
        // Authorization: Bearer <token>
    }/*from  ww  w . java 2 s  .c o  m*/
}

From source file:examples.AuthJWTExamples.java

License:Open Source License

public void example8(Vertx vertx) {

    JsonObject config = new JsonObject().put("public-key", "BASE64-ENCODED-PUBLIC_KEY");
    AuthProvider provider = JWTAuth.create(vertx, config);
}

From source file:examples.AuthMongoExamples.java

License:Open Source License

public void example1(Vertx vertx, JsonObject mongoClientConfig) {
    MongoClient client = MongoClient.createShared(vertx, mongoClientConfig);
    JsonObject authProperties = new JsonObject();
    MongoAuth authProvider = MongoAuth.create(client, authProperties);
}

From source file:examples.AuthMongoExamples.java

License:Open Source License

public void example2(MongoAuth authProvider) {
    JsonObject authInfo = new JsonObject().put("username", "tim").put("password", "sausages");
    authProvider.authenticate(authInfo, res -> {
        if (res.succeeded()) {
            User user = res.result();/*from ww  w .  j  av  a 2s  .  c  om*/
        } else {
            // Failed!
        }
    });
}

From source file:examples.AuthOAuth2Examples.java

License:Open Source License

public void example1(Vertx vertx) {

    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE,
            new OAuth2ClientOptions().setClientID("YOUR_CLIENT_ID").setClientSecret("YOUR_CLIENT_SECRET")
                    .setSite("https://github.com/login").setTokenPath("/oauth/access_token")
                    .setAuthorizationPath("/oauth/authorize"));

    // when there is a need to access a protected resource or call a protected method,
    // call the authZ url for a challenge

    String authorization_uri = oauth2
            .authorizeURL(new JsonObject().put("redirect_uri", "http://localhost:8080/callback")
                    .put("scope", "notifications").put("state", "3(#0/!~"));

    // when working with web application use the above string as a redirect url

    // in this case GitHub will call you back in the callback uri one should now complete the handshake as:

    String code = "xxxxxxxxxxxxxxxxxxxxxxxx"; // the code is provided as a url parameter by github callback call

    oauth2.getToken(new JsonObject().put("code", code).put("redirect_uri", "http://localhost:8080/callback"),
            res -> {//from   w ww . j ava 2 s .  c  om
                if (res.failed()) {
                    // error, the code provided is not valid
                } else {
                    // save the token and continue...
                }
            });
}

From source file:examples.AuthOAuth2Examples.java

License:Open Source License

public void example2(Vertx vertx, HttpServerResponse response) {

    // Set the client credentials and the OAuth2 server
    OAuth2ClientOptions credentials = new OAuth2ClientOptions().setClientID("<client-id>")
            .setClientSecret("<client-secret>").setSite("https://api.oauth.com");

    // Initialize the OAuth2 Library
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, credentials);

    // Authorization oauth2 URI
    String authorization_uri = oauth2
            .authorizeURL(new JsonObject().put("redirect_uri", "http://localhost:8080/callback")
                    .put("scope", "<scope>").put("state", "<state>"));

    // Redirect example using Vert.x
    response.putHeader("Location", authorization_uri).setStatusCode(302).end();

    JsonObject tokenConfig = new JsonObject().put("code", "<code>").put("redirect_uri",
            "http://localhost:3000/callback");

    // Callbacks//from   ww w .j ava 2  s .c o  m
    // Save the access token
    oauth2.getToken(tokenConfig, res -> {
        if (res.failed()) {
            System.err.println("Access Token Error: " + res.cause().getMessage());
        } else {
            // Get the access token object (the authorization code is given from the previous step).
            AccessToken token = res.result();
        }
    });
}

From source file:examples.AuthOAuth2Examples.java

License:Open Source License

public void example3(Vertx vertx) {

    // Initialize the OAuth2 Library
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD);

    JsonObject tokenConfig = new JsonObject().put("username", "username").put("password", "password");

    // Callbacks//from   w w  w . j ava  2 s .c o  m
    // Save the access token
    oauth2.getToken(tokenConfig, res -> {
        if (res.failed()) {
            System.err.println("Access Token Error: " + res.cause().getMessage());
        } else {
            // Get the access token object (the authorization code is given from the previous step).
            AccessToken token = res.result();

            oauth2.api(HttpMethod.GET, "/users",
                    new JsonObject().put("access_token", token.principal().getString("access_token")), res2 -> {
                        // the user object should be returned here...
                    });
        }
    });
}

From source file:examples.AuthOAuth2Examples.java

License:Open Source License

public void example4(Vertx vertx) {

    // Set the client credentials and the OAuth2 server
    OAuth2ClientOptions credentials = new OAuth2ClientOptions().setClientID("<client-id>")
            .setClientSecret("<client-secret>").setSite("https://api.oauth.com");

    // Initialize the OAuth2 Library
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.CLIENT, credentials);

    JsonObject tokenConfig = new JsonObject();

    // Callbacks//from   w  w w  .  j ava 2s. co m
    // Save the access token
    oauth2.getToken(tokenConfig, res -> {
        if (res.failed()) {
            System.err.println("Access Token Error: " + res.cause().getMessage());
        } else {
            // Get the access token object (the authorization code is given from the previous step).
            AccessToken token = res.result();
        }
    });
}

From source file:examples.AuthOAuth2Examples.java

License:Open Source License

public void example13(Vertx vertx) {
    // you would get this config from the keycloak admin console
    JsonObject keycloakJson = new JsonObject().put("realm", "master")
            .put("realm-public-key", "MIIBIjANBgkqhk...wIDAQAB")
            .put("auth-server-url", "http://localhost:9000/auth").put("ssl-required", "external")
            .put("resource", "frontend")
            .put("credentials", new JsonObject().put("secret", "2fbf5e18-b923-4a83-9657-b4ebd5317f60"));

    // Initialize the OAuth2 Library
    OAuth2Auth oauth2 = KeycloakAuth.create(vertx, OAuth2FlowType.PASSWORD, keycloakJson);

    // first get a token (authenticate)
    oauth2.getToken(new JsonObject().put("username", "user").put("password", "secret"), res -> {
        if (res.failed()) {
            // error handling...
        } else {//from ww  w.  ja  va 2  s  . co  m
            AccessToken token = res.result();

            // now check for permissions
            token.isAuthorised("account:manage-account", r -> {
                if (r.result()) {
                    // this user is authorized to manage its account
                }
            });
        }
    });
}