Search in sources :

Example 1 with SessionStore

use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.

the class RedirectAuthHandlerTest method testRedirectWithParams.

@Test
public void testRedirectWithParams() throws Exception {
    router.route().handler(BodyHandler.create());
    router.route().handler(CookieHandler.create());
    SessionStore store = LocalSessionStore.create(vertx);
    router.route().handler(SessionHandler.create(store));
    router.route().handler(UserSessionHandler.create(authProvider));
    AuthHandler authHandler = RedirectAuthHandler.create(authProvider);
    router.route("/protected/*").handler(authHandler);
    router.route("/protected/somepage").handler(ctx -> {
        assertEquals("1", ctx.request().getParam("param"));
        ctx.response().end("Welcome to the protected resource!");
    });
    router.route("/loginpage").handler(rc -> rc.response().putHeader("content-type", "text/html").end(createloginHTML()));
    router.route("/login").handler(FormLoginHandler.create(authProvider));
    // request protected resource, expect redirect to login
    testRequest(HttpMethod.GET, "/protected/somepage?param=1", null, resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertEquals("/loginpage", location);
        String setCookie = resp.headers().get("set-cookie");
        assertNotNull(setCookie);
        sessionCookie.set(setCookie);
    }, 302, "Found", null);
    // get login
    testRequest(HttpMethod.GET, "/loginpage", req -> req.putHeader("cookie", sessionCookie.get()), resp -> {
    }, 200, "OK", createloginHTML());
    // do post with credentials
    testRequest(HttpMethod.POST, "/login", sendLoginRequestConsumer(), resp -> {
        // session will be upgraded
        String setCookie = resp.headers().get("set-cookie");
        assertNotNull(setCookie);
        sessionCookie.set(setCookie);
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertEquals("/protected/somepage?param=1", location);
    }, 302, "Found", null);
    // fetch the resource
    testRequest(HttpMethod.GET, "/protected/somepage?param=1", req -> req.putHeader("cookie", sessionCookie.get()), resp -> {
    }, 200, "OK", "Welcome to the protected resource!");
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) Test(org.junit.Test)

Example 2 with SessionStore

use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.

the class RedirectAuthHandlerTest method doLoginCommon.

private void doLoginCommon(Handler<RoutingContext> handler, Set<String> authorities) throws Exception {
    router.route().handler(BodyHandler.create());
    router.route().handler(CookieHandler.create());
    SessionStore store = LocalSessionStore.create(vertx);
    router.route().handler(SessionHandler.create(store));
    router.route().handler(UserSessionHandler.create(authProvider));
    AuthHandler authHandler = RedirectAuthHandler.create(authProvider);
    if (authorities != null) {
        authHandler.addAuthorities(authorities);
    }
    router.route("/protected/*").handler(authHandler);
    router.route("/protected/somepage").handler(handler);
    String loginHTML = createloginHTML();
    router.route("/loginpage").handler(rc -> rc.response().putHeader("content-type", "text/html").end(loginHTML));
    if (formLoginHandler == null) {
        formLoginHandler = FormLoginHandler.create(authProvider);
    }
    router.route("/login").handler(formLoginHandler);
    testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertEquals("/loginpage", location);
        String setCookie = resp.headers().get("set-cookie");
        assertNotNull(setCookie);
        sessionCookie.set(setCookie);
    }, 302, "Found", null);
    testRequest(HttpMethod.GET, "/loginpage", req -> req.putHeader("cookie", sessionCookie.get()), resp -> {
    }, 200, "OK", loginHTML);
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore)

Example 3 with SessionStore

use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.

the class AuthHandlerTestBase method testAuthorisation.

protected void testAuthorisation(String username, boolean fail, Set<String> authorities) throws Exception {
    if (requiresSession()) {
        router.route().handler(BodyHandler.create());
        router.route().handler(CookieHandler.create());
        SessionStore store = getSessionStore();
        router.route().handler(SessionHandler.create(store));
    }
    JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
    AuthHandler authHandler = createAuthHandler(authProvider);
    if (authorities != null) {
        authHandler.addAuthorities(authorities);
    }
    router.route().handler(rc -> {
        // we need to be logged in
        if (rc.user() == null) {
            JsonObject authInfo = new JsonObject().put("username", username).put("password", "delicious:sausages");
            authProvider.authenticate(authInfo, res -> {
                if (res.succeeded()) {
                    rc.setUser(res.result());
                    rc.next();
                } else {
                    rc.fail(res.cause());
                }
            });
        }
    });
    router.route().handler(authHandler);
    router.route().handler(rc -> rc.response().end());
    testRequest(HttpMethod.GET, "/", fail ? 403 : 200, fail ? "Forbidden" : "OK");
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider)

Example 4 with SessionStore

use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.

the class BasicAuthHandlerTest method testWithSessions.

@Test
public void testWithSessions() throws Exception {
    router.route().handler(BodyHandler.create());
    router.route().handler(CookieHandler.create());
    SessionStore store = new SerializingSessionStore();
    router.route().handler(SessionHandler.create(store));
    JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
    router.route().handler(UserSessionHandler.create(authProvider));
    router.route("/protected/*").handler(BasicAuthHandler.create(authProvider));
    AtomicReference<String> sessionID = new AtomicReference<>();
    AtomicInteger count = new AtomicInteger();
    Handler<RoutingContext> handler = rc -> {
        int c = count.incrementAndGet();
        assertNotNull(rc.session());
        String sessID = sessionID.get();
        if (sessID != null) {
            assertEquals(sessID, rc.session().id());
        }
        assertNotNull(rc.user());
        assertEquals("tim", rc.user().principal().getString("username"));
        if (c == 7) {
            rc.clearUser();
        }
        rc.response().end("Welcome to the protected resource!");
    };
    router.route("/protected/somepage").handler(handler);
    AtomicReference<String> sessionCookie = new AtomicReference<>();
    testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
        String wwwAuth = resp.headers().get("WWW-Authenticate");
        assertNotNull(wwwAuth);
        assertEquals("Basic realm=\"" + BasicAuthHandler.DEFAULT_REALM + "\"", wwwAuth);
        String setCookie = resp.headers().get("set-cookie");
        // auth failed you should not get a session cookie!!!
        assertNull(setCookie);
    }, 401, "Unauthorized", null);
    // Now try again with credentials
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic dGltOmRlbGljaW91czpzYXVzYWdlcw=="), resp -> {
        String wwwAuth = resp.headers().get("WWW-Authenticate");
        assertNull(wwwAuth);
        // auth is success, we should get a cookie!!!
        String setCookie = resp.headers().get("set-cookie");
        assertNotNull(setCookie);
        sessionCookie.set(setCookie);
    }, 200, "OK", "Welcome to the protected resource!");
    // And try again a few times we should be logged in with user stored in the session
    for (int i = 0; i < 5; i++) {
        testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("cookie", sessionCookie.get()), resp -> {
            String wwwAuth = resp.headers().get("WWW-Authenticate");
            assertNull(wwwAuth);
        }, 200, "OK", "Welcome to the protected resource!");
    }
    // Now set the user to null, this effectively logs him out
    testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
        String wwwAuth = resp.headers().get("WWW-Authenticate");
        assertNotNull(wwwAuth);
        assertEquals("Basic realm=\"" + BasicAuthHandler.DEFAULT_REALM + "\"", wwwAuth);
    }, 401, "Unauthorized", null);
    // And login again
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic dGltOmRlbGljaW91czpzYXVzYWdlcw=="), resp -> {
        String wwwAuth = resp.headers().get("WWW-Authenticate");
        assertNull(wwwAuth);
    }, 200, "OK", "Welcome to the protected resource!");
}
Also used : SessionStore(io.vertx.ext.web.sstore.SessionStore) Session(io.vertx.ext.web.Session) PRNG(io.vertx.ext.auth.PRNG) ClusterSerializable(io.vertx.core.shareddata.impl.ClusterSerializable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SessionImpl(io.vertx.ext.web.sstore.impl.SessionImpl) Test(org.junit.Test) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) AuthProvider(io.vertx.ext.auth.AuthProvider) Buffer(io.vertx.core.buffer.Buffer) SessionStore(io.vertx.ext.web.sstore.SessionStore) ShiroAuth(io.vertx.ext.auth.shiro.ShiroAuth) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ShiroAuthRealmType(io.vertx.ext.auth.shiro.ShiroAuthRealmType) RoutingContext(io.vertx.ext.web.RoutingContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with SessionStore

use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.

the class WebExamples method example31.

public void example31(Vertx vertx) {
    // Create a local session store using defaults
    SessionStore store1 = LocalSessionStore.create(vertx);
    // Create a local session store specifying the local shared map name to use
    // This might be useful if you have more than one application in the same
    // Vert.x instance and want to use different maps for different applications
    SessionStore store2 = LocalSessionStore.create(vertx, "myapp3.sessionmap");
    // Create a local session store specifying the local shared map name to use and
    // setting the reaper interval for expired sessions to 10 seconds
    SessionStore store3 = LocalSessionStore.create(vertx, "myapp3.sessionmap", 10000);
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore)

Aggregations

SessionStore (io.vertx.ext.web.sstore.SessionStore)12 LocalSessionStore (io.vertx.ext.web.sstore.LocalSessionStore)10 ClusteredSessionStore (io.vertx.ext.web.sstore.ClusteredSessionStore)6 JsonObject (io.vertx.core.json.JsonObject)4 AuthProvider (io.vertx.ext.auth.AuthProvider)4 Test (org.junit.Test)4 SessionHandler (io.vertx.ext.web.handler.SessionHandler)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)2 CorsHandler (io.vertx.ext.web.handler.CorsHandler)2 VxApiCertOptions (com.szmirren.vxApi.core.options.VxApiCertOptions)1 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 VertxOptions (io.vertx.core.VertxOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpMethod (io.vertx.core.http.HttpMethod)1 PemKeyCertOptions (io.vertx.core.net.PemKeyCertOptions)1 PfxOptions (io.vertx.core.net.PfxOptions)1