Search in sources :

Example 11 with JWTOptions

use of io.vertx.ext.jwt.JWTOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Create a JWT Auth Provider
    JWTAuth jwt = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret")));
    // this route is excluded from the auth handler (it represents your login endpoint)
    router.get("/api/newToken").handler(ctx -> {
        List<String> authorities = new ArrayList<>();
        for (String authority : ctx.request().params().getAll("authority")) {
            authorities.add(authority);
        }
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions(authorities)));
    });
    // protect the API (any authority is allowed)
    router.route("/api/protected").handler(JWTAuthHandler.create(jwt));
    router.get("/api/protected").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("this secret is not defcon!");
    });
    // protect the API (defcon1 authority is required)
    router.route("/api/protected/defcon1").handler(JWTAuthHandler.create(jwt).addAuthority("defcon1"));
    router.get("/api/protected/defcon1").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("this secret is defcon1!");
    });
    // protect the API (defcon2 authority is required)
    router.route("/api/protected/defcon2").handler(JWTAuthHandler.create(jwt).addAuthority("defcon2"));
    router.get("/api/protected/defcon2").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("this secret is defcon2!");
    });
    // protect the API (defcon3 authority is required)
    router.route("/api/protected/defcon3").handler(JWTAuthHandler.create(jwt).addAuthority("defcon3"));
    router.get("/api/protected/defcon3").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("this secret is defcon3!");
    });
    // Serve the non private static pages
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : ArrayList(java.util.ArrayList) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 12 with JWTOptions

use of io.vertx.ext.jwt.JWTOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Create a JWT Auth Provider
    JWTAuth jwt = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret")));
    // this route is excluded from the auth handler (it represents your login endpoint)
    router.get("/api/newToken").handler(ctx -> {
        List<String> authorities = new ArrayList<>();
        for (String authority : ctx.request().params().getAll("authority")) {
            authorities.add(authority);
        }
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions(authorities)));
    });
    router.route("/api/protected*").handler(JWTAuthHandler.create(jwt));
    router.get("/api/protected").handler(ctx -> {
        // protected the API (any authority is allowed)
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("this secret is not defcon!");
    });
    router.get("/api/protected/defcon1").handler(ctx -> {
        // protect the API (defcon1 authority is required)
        ctx.user().isAuthorised("defcon1", allowed -> {
            if (allowed.failed()) {
                ctx.fail(allowed.cause());
                return;
            }
            // user does not have the required authority
            if (!allowed.result()) {
                ctx.response().setStatusCode(403).end();
                return;
            }
            ctx.response().putHeader("Content-Type", "text/plain");
            ctx.response().end("this secret is defcon1!");
        });
    });
    router.get("/api/protected/defcon2").handler(ctx -> {
        // protect the API (defcon2 authority is required)
        ctx.user().isAuthorised("defcon2", allowed -> {
            if (allowed.failed()) {
                ctx.fail(allowed.cause());
                return;
            }
            // user does not have the required authority
            if (!allowed.result()) {
                ctx.response().setStatusCode(403).end();
                return;
            }
            ctx.response().putHeader("Content-Type", "text/plain");
            ctx.response().end("this secret is defcon2!");
        });
    });
    router.get("/api/protected/defcon3").handler(ctx -> {
        // protect the API (defcon3 authority is required)
        ctx.user().isAuthorised("defcon3", allowed -> {
            if (allowed.failed()) {
                ctx.fail(allowed.cause());
                return;
            }
            // user does not have the required authority
            if (!allowed.result()) {
                ctx.response().setStatusCode(403).end();
                return;
            }
            ctx.response().putHeader("Content-Type", "text/plain");
            ctx.response().end("this secret is defcon3!");
        });
    });
    // Serve the non private static pages
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : ArrayList(java.util.ArrayList) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 13 with JWTOptions

use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.

the class JWTAuthProviderTest method testBadAudience.

@Test
public void testBadAudience() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().addAudience("e").addAudience("d")));
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions().addAudience("a").addAudience("b").addAudience("c"));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onFailure(thr -> {
        assertNotNull(thr);
        testComplete();
    }));
    await();
}
Also used : KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JsonArray(io.vertx.core.json.JsonArray) Base64(java.util.Base64) JWTOptions(io.vertx.ext.jwt.JWTOptions) SecretOptions(io.vertx.ext.auth.SecretOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) VertxTestBase(io.vertx.test.core.VertxTestBase) StandardCharsets(java.nio.charset.StandardCharsets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) JWTOptions(io.vertx.ext.jwt.JWTOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 14 with JWTOptions

use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.

the class JWTAuthProviderTest method testGenerateNewTokenForceAlgorithm.

@Test
public void testGenerateNewTokenForceAlgorithm() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("gce.jks").setType("jks").setPassword("notasecret")));
    String token = authProvider.generateToken(new JsonObject(), new JWTOptions().setAlgorithm("RS256"));
    assertNotNull(token);
    // reverse
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JsonArray(io.vertx.core.json.JsonArray) Base64(java.util.Base64) JWTOptions(io.vertx.ext.jwt.JWTOptions) SecretOptions(io.vertx.ext.auth.SecretOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) VertxTestBase(io.vertx.test.core.VertxTestBase) StandardCharsets(java.nio.charset.StandardCharsets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) Test(org.junit.Test)

Example 15 with JWTOptions

use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.

the class JWTAuthProviderTest method testAlgNone.

@Test
public void testAlgNone() {
    JWTAuth authProvider = JWTAuth.create(vertx, new JWTAuthOptions());
    JsonObject payload = new JsonObject().put("sub", "UserUnderTest").put("aud", "OrganizationUnderTest").put("iat", 1431695313).put("exp", 1747055313).put("roles", new JsonArray().add("admin").add("developer").add("user")).put("permissions", new JsonArray().add("read").add("write").add("execute"));
    final String token = authProvider.generateToken(payload, new JWTOptions().setSubject("UserUnderTest").setAlgorithm("none"));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JsonArray(io.vertx.core.json.JsonArray) Base64(java.util.Base64) JWTOptions(io.vertx.ext.jwt.JWTOptions) SecretOptions(io.vertx.ext.auth.SecretOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) VertxTestBase(io.vertx.test.core.VertxTestBase) StandardCharsets(java.nio.charset.StandardCharsets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)20 JWTOptions (io.vertx.ext.jwt.JWTOptions)20 JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)17 Test (org.junit.Test)17 JsonArray (io.vertx.core.json.JsonArray)15 KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)15 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)15 SecretOptions (io.vertx.ext.auth.SecretOptions)14 VertxTestBase (io.vertx.test.core.VertxTestBase)14 StandardCharsets (java.nio.charset.StandardCharsets)14 Base64 (java.util.Base64)14 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)14 Router (io.vertx.ext.web.Router)3 ArrayList (java.util.ArrayList)2