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!");
}
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);
}
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");
}
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!");
}
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);
}
Aggregations