Search in sources :

Example 16 with JWTOptions

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

the class JWTAuthProviderTest method testBadIssuer.

@Test
public void testBadIssuer() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setIssuer("https://vertx.io")));
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions().setIssuer("https://auth0.io"));
    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 17 with JWTOptions

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

the class JWTAuthProviderTest method testLeeway4.

@Test
public void testLeeway4() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(5)));
    long now = (System.currentTimeMillis() / 1000) + 2;
    JsonObject payload = new JsonObject().put("sub", "Paulo").put("iat", now);
    String token = authProvider.generateToken(payload);
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    // pass because iat is > now (clock drifted 2 sec) and we have a leeway of 5sec
    authProvider.authenticate(authInfo, onSuccess(t -> 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 18 with JWTOptions

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

the class JWTAuthProviderTest method testTokenWithTimestamp.

@Test
public void testTokenWithTimestamp() {
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions());
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        assertTrue(res.principal().containsKey("iat"));
        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) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 19 with JWTOptions

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

the class JWTAuthProviderTest method testGoodIssuer.

@Test
public void testGoodIssuer() {
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions().setIssuer("https://vertx.io"));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token).put("options", new JsonObject().put("issuer", "https://vertx.io"));
    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) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 20 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")));
    // protect the API
    router.route("/api/*").handler(JWTAuthHandler.create(jwt, "/api/newToken"));
    // this route is excluded from the auth handler
    router.get("/api/newToken").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60)));
    });
    // this is the secret API
    router.get("/api/protected").handler(ctx -> {
        ctx.response().putHeader("Content-Type", "text/plain");
        ctx.response().end("a secret you should keep for yourself...");
    });
    // Serve the non private static pages
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : 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)

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