Search in sources :

Example 1 with JWTOptions

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

the class JWTAuthProviderTest method testExpiration.

@Test
public void testExpiration() {
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions().setExpiresInSeconds(1).setNoTimestamp(true));
    assertNotNull(token);
    vertx.setTimer(2000L, t -> {
        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) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 2 with JWTOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenImmutableClaims.

@Test
public void testGenerateNewTokenImmutableClaims() {
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    String token0 = authProvider.generateToken(payload, new JWTOptions().addPermission("user"));
    String token1 = authProvider.generateToken(payload, new JWTOptions().addPermission("admin"));
    assertNotEquals(token0, token1);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) Test(org.junit.Test)

Example 3 with JWTOptions

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

the class JWTAuthProviderTest method testLeeway2.

@Test
public void testLeeway2() {
    authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(0)));
    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);
    // fail because iat is > now (clock drifted 2 sec)
    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)

Example 4 with JWTOptions

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

the class JWTAuthProviderTest method testTokenWithoutTimestamp.

@Test
public void testTokenWithoutTimestamp() {
    JsonObject payload = new JsonObject().put("sub", "Paulo");
    final String token = authProvider.generateToken(payload, new JWTOptions().setExpiresInMinutes(5).setNoTimestamp(true));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        assertTrue(res.principal().containsKey("exp"));
        assertFalse(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 5 with JWTOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenES256.

@Test
public void testGenerateNewTokenES256() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("es256-keystore.jceks").setPassword("secret")));
    String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions().setAlgorithm("ES256"));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, res -> {
        if (res.failed()) {
            res.cause().printStackTrace();
            fail();
        }
        assertNotNull(res.result());
        testComplete();
    });
    await();
}
Also used : 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)

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