Search in sources :

Example 6 with JWTAuth

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

the class WebExamples method example50.

public void example50(Vertx vertx) {
    Router router = Router.router(vertx);
    JWTAuthOptions authConfig = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setType("jceks").setPath("keystore.jceks").setPassword("secret"));
    JWTAuth jwt = JWTAuth.create(vertx, authConfig);
    router.route("/login").handler(ctx -> {
        // this is an example, authentication should be done with another provider...
        if ("paulo".equals(ctx.request().getParam("username")) && "secret".equals(ctx.request().getParam("password"))) {
            ctx.response().end(jwt.generateToken(new JsonObject().put("sub", "paulo")));
        } else {
            ctx.fail(401);
        }
    });
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 7 with JWTAuth

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

the class WebExamples method example52.

public void example52(Vertx vertx) {
    JWTAuthOptions authConfig = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setType("jceks").setPath("keystore.jceks").setPassword("secret"));
    JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
    authProvider.generateToken(new JsonObject().put("sub", "paulo").put("someKey", "some value"), new JWTOptions());
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 8 with JWTAuth

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

the class ChainAuthMixHandlerTest method test2JWTIssuers.

@Test
public void test2JWTIssuers() throws Exception {
    JsonObject key = new JsonObject().put("kty", "oct").put("use", "sig").put("k", "wuSPxS64NYh4ohDpZWOMNtawhBLHVn8dhKuIxnsLLd-dfKzIb5FL7r-vXTJ3MjtqnBlh_piKjn6qvb8os00MXNEyJWhgbPsnZEfqj6wMsJiH3uDcEgDuBMVbsuMlVbyX3x0Cd6qn0qvF8JZaLxSR6JNEEOGnbkUXqF9ghcI2y8rooN6ivQJ0-SiCqtQSkVrSO4H65lHagUus0XjTErL4GypbcO6PBIZMtHBW4UZHVcl86IhDxj5v0xf3WSuDGxkrbw5rpM_eVUR1eu71XPoTXD4WgDRtq4CoQcIFeSpqJuKZvzDJ47zV3wgnqKZ6G-RkiSKLBUj5_4Ur_YWHw2h-CQ").put("alg", "HS256");
    JWTAuth me = JWTAuth.create(vertx, new JWTAuthOptions().addJwk(key).setJWTOptions(new JWTOptions().setIssuer("me")));
    JWTAuth you = JWTAuth.create(vertx, new JWTAuthOptions().addJwk(key).setJWTOptions(new JWTOptions().setIssuer("you")));
    ChainAuthHandler chain = ChainAuthHandler.any().add(JWTAuthHandler.create(me)).add(JWTAuthHandler.create(you));
    router.route().handler(chain);
    router.route().handler(ctx -> ctx.response().end());
    // Payload with right issuer
    final JsonObject payloadA = new JsonObject().put("sub", "Paulo").put("iss", "me");
    testRequest(HttpMethod.GET, "/", req -> req.putHeader("Authorization", "Bearer " + me.generateToken(payloadA)), 200, "OK", null);
    // Payload with right issuer
    final JsonObject payloadB = new JsonObject().put("sub", "Paulo").put("iss", "you");
    testRequest(HttpMethod.GET, "/", req -> req.putHeader("Authorization", "Bearer " + you.generateToken(payloadB)), 200, "OK", null);
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) Test(org.junit.Test)

Example 9 with JWTAuth

use of io.vertx.ext.auth.jwt.JWTAuth 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)

Example 10 with JWTAuth

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

the class AuthJWTExamples method example17.

public void example17(Vertx vertx) {
    JWTAuth provider = JWTAuth.create(vertx, new JWTAuthOptions().addPubSecKey(new PubSecKeyOptions().setAlgorithm("ES256").setSecretKey("MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgeRyEfU1NSHPTCuC9\n" + "rwLZMukaWCH2Fk6q5w+XBYrKtLihRANCAAStpUnwKmSvBM9EI+W5QN3ALpvz6bh0\n" + "SPCXyz5KfQZQuSj4f3l+xNERDUDaygIUdLjBXf/bc15ur2iZjcq4r0Mr")));
    String token = provider.generateToken(new JsonObject(), new io.vertx.ext.jwt.JWTOptions().setAlgorithm("ES256"));
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Aggregations

JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)13 JsonObject (io.vertx.core.json.JsonObject)12 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)10 KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)5 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)4 JWTOptions (io.vertx.ext.auth.jwt.JWTOptions)4 JWTOptions (io.vertx.ext.jwt.JWTOptions)4 Router (io.vertx.ext.web.Router)3 JWTOptions (io.vertx.ext.auth.JWTOptions)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 JsonArray (io.vertx.core.json.JsonArray)1 SecretOptions (io.vertx.ext.auth.SecretOptions)1 VertxTestBase (io.vertx.test.core.VertxTestBase)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Base64 (java.util.Base64)1 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)1