Search in sources :

Example 11 with JWTOptions

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

the class MultiAuthorizationHandlerTest method testJWTAuthenticationWithAuthorization4.

@Test
public void testJWTAuthenticationWithAuthorization4() throws Exception {
    // we are testing the following:
    // authentication via jwt
    // 3 authorization providers are registered
    // an authorization is required on the path
    // => the test should fail since no authorization providers provide the correct authorization
    router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
    router.route("/protected/*").handler(AuthorizationHandler.create(RoleBasedAuthorization.create("role4")).addAuthorizationProvider(createProvider("authzProvider1", RoleBasedAuthorization.create("role1"))).addAuthorizationProvider(createProvider("authzProvider2", RoleBasedAuthorization.create("role2"))).addAuthorizationProvider(createProvider("authzProvider3", RoleBasedAuthorization.create("role3"))));
    router.route("/protected/page1").handler(rc -> {
        assertNotNull(rc.user());
        assertEquals("paulo", rc.user().attributes().getJsonObject("accessToken").getString("sub"));
        rc.response().end("Welcome");
    });
    // login with correct credentials
    testRequest(HttpMethod.GET, "/protected/page1", req -> req.putHeader("Authorization", "Bearer " + authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions())), 403, "Forbidden", "Forbidden");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.JWTOptions) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)11 JWTOptions (io.vertx.ext.auth.JWTOptions)11 Test (org.junit.Test)10 JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)4 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)4 KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)3 RoutingContext (io.vertx.ext.web.RoutingContext)3 Handler (io.vertx.core.Handler)2 HttpMethod (io.vertx.core.http.HttpMethod)2 JsonArray (io.vertx.core.json.JsonArray)2 WebTestBase (io.vertx.ext.web.WebTestBase)2 Arrays (java.util.Arrays)2 Before (org.junit.Before)2 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)1 JWK (io.vertx.ext.auth.impl.jose.JWK)1 JWT (io.vertx.ext.auth.impl.jose.JWT)1 OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)1 OAuth2Options (io.vertx.ext.auth.oauth2.OAuth2Options)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1