Search in sources :

Example 1 with JWTOptions

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

the class JWTAuthHandlerTest method testLoginFail.

@Test
public void testLoginFail() throws Exception {
    Handler<RoutingContext> handler = rc -> {
        fail("should not get here");
        rc.response().end("Welcome to the protected resource!");
    };
    router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
    router.route("/protected/somepage").handler(handler);
    testRequest(HttpMethod.GET, "/protected/somepage", null, 401, "Unauthorized", null);
    // Now try again with bad token
    final String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer x" + token), 401, "Unauthorized", null);
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic " + token), 401, "Unauthorized", null);
}
Also used : HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) RoutingContext(io.vertx.ext.web.RoutingContext) Handler(io.vertx.core.Handler) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) WebTestBase(io.vertx.ext.web.WebTestBase) Before(org.junit.Before) RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) Test(org.junit.Test)

Example 2 with JWTOptions

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

the class JWTAuthHandlerTest method testLogin.

@Test
public void testLogin() throws Exception {
    Handler<RoutingContext> handler = rc -> {
        assertNotNull(rc.user());
        assertEquals("paulo", rc.user().principal().getString("sub"));
        rc.response().end("Welcome to the protected resource!");
    };
    router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
    router.route("/protected/somepage").handler(handler);
    testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
    }, 401, "Unauthorized", null);
    // Now try again with credentials
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer " + authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions())), 200, "OK", "Welcome to the protected resource!");
}
Also used : HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) RoutingContext(io.vertx.ext.web.RoutingContext) Handler(io.vertx.core.Handler) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) WebTestBase(io.vertx.ext.web.WebTestBase) Before(org.junit.Before) RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) Test(org.junit.Test)

Example 3 with JWTOptions

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

the class WebExamples method example52.

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

Example 4 with JWTOptions

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

the class WebExamples method example50.

public void example50(Vertx vertx) {
    Router router = Router.router(vertx);
    JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret"));
    JWTAuth authProvider = 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(authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions()));
        } else {
            ctx.fail(401);
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 5 with JWTOptions

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

the class AuthJWTExamples method example7.

public void example7(Vertx vertx, String username, String password) {
    JWTAuthOptions config = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("keystore.jceks").setPassword("secret"));
    JWTAuth provider = JWTAuth.create(vertx, config);
    // on the verify endpoint once you verify the identity of the user by its username/password
    if ("paulo".equals(username) && "super_secret".equals(password)) {
        String token = provider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
    // now for any request to protected resources you should pass this string in the HTTP header Authorization as:
    // Authorization: Bearer <token>
    }
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)5 JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)5 JWTOptions (io.vertx.ext.auth.jwt.JWTOptions)5 Handler (io.vertx.core.Handler)2 HttpMethod (io.vertx.core.http.HttpMethod)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 WebTestBase (io.vertx.ext.web.WebTestBase)2 Before (org.junit.Before)2 Test (org.junit.Test)2 KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)1 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)1