Search in sources :

Example 6 with JWTOptions

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

the class JWTAuthProviderTest method testGenerateNewToken.

@Test
public void testGenerateNewToken() {
    JsonObject payload = new JsonObject().put("sub", "Paulo").put("exp", 1747055313).put("iat", 1431695313).put("permissions", new JsonArray().add("read").add("write").add("execute")).put("roles", new JsonArray().add("admin").add("developer").add("user"));
    String token = authProvider.generateToken(payload, new JWTOptions().setSubject("Paulo"));
    assertNotNull(token);
    assertEquals(JWT_VALID, token);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 7 with JWTOptions

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

the class JWTAuthProviderTest method testGoodAudience.

@Test
public void testGoodAudience() {
    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).put("options", new JsonObject().put("audience", new JsonArray().add("b").add("d")));
    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) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 8 with JWTOptions

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

the class JWTAuthProviderTest method testLeeway3.

@Test
public void testLeeway3() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(5)));
    long now = System.currentTimeMillis() / 1000;
    JsonObject payload = new JsonObject().put("sub", "Paulo").put("exp", now).put("iat", now);
    String token = authProvider.generateToken(payload);
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    // fail because exp is <= to now
    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 9 with JWTOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenWithMacSecret.

@Test
public void testGenerateNewTokenWithMacSecret() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addSecret(new SecretOptions().setType("HS256").setSecret("notasecret")));
    String token = authProvider.generateToken(new JsonObject(), new JWTOptions().setAlgorithm("HS256"));
    assertNotNull(token);
    // reverse
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : SecretOptions(io.vertx.ext.auth.SecretOptions) 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) Test(org.junit.Test)

Example 10 with JWTOptions

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

the class JWTAuthProviderTest method testLeeway.

@Test
public void testLeeway() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(0)));
    long now = System.currentTimeMillis() / 1000;
    JsonObject payload = new JsonObject().put("sub", "Paulo").put("exp", now);
    String token = authProvider.generateToken(payload);
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    // fail because exp is <= to now
    authProvider.authenticate(authInfo, onFailure(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)

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