Skip to content

Commit

Permalink
CAMEL-20019: camel-platform-http-vertx implement session handler (#12530
Browse files Browse the repository at this point in the history
)

* CAMEL-20019: demo session handler implementation

* CAMEL-20019: updates in response to review comments
  • Loading branch information
jonomorris committed Dec 27, 2023
1 parent 6959b2e commit f79496b
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ protected void initializeServer() {
subRouter.route().handler(createCorsHandler(configuration));
}

if (configuration.getSessionConfig().isEnabled()) {
subRouter.route().handler(
configuration.getSessionConfig().createSessionHandler(vertx));
}

router.route(configuration.getPath() + "*").subRouter(subRouter);

context.getRegistry().bind(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
import java.time.Duration;
import java.util.List;

import io.vertx.core.Vertx;
import io.vertx.core.http.CookieSameSite;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.ClusteredSessionStore;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;
import org.apache.camel.support.jsse.SSLContextParameters;

/**
Expand All @@ -39,6 +45,7 @@ public class VertxPlatformHttpServerConfiguration {

private BodyHandler bodyHandler = new BodyHandler();
private Cors cors = new Cors();
private SessionConfig sessionConfig = new SessionConfig();

public int getPort() {
return getBindPort();
Expand Down Expand Up @@ -112,6 +119,14 @@ public void setCors(Cors corsConfiguration) {
this.cors = corsConfiguration;
}

public SessionConfig getSessionConfig() {
return sessionConfig;
}

public void setSessionConfig(SessionConfig sessionConfig) {
this.sessionConfig = sessionConfig;
}

public BodyHandler getBodyHandler() {
return bodyHandler;
}
Expand All @@ -120,6 +135,128 @@ public void setBodyHandler(BodyHandler bodyHandler) {
this.bodyHandler = bodyHandler;
}

public static class SessionConfig {
private boolean enabled;
private SessionStoreType storeType = SessionStoreType.LOCAL;
private String sessionCookieName = SessionHandler.DEFAULT_SESSION_COOKIE_NAME;
private String sessionCookiePath = SessionHandler.DEFAULT_SESSION_COOKIE_PATH;
private long sessionTimeOut = SessionHandler.DEFAULT_SESSION_TIMEOUT;
private boolean cookieSecure = SessionHandler.DEFAULT_COOKIE_SECURE_FLAG;
private boolean cookieHttpOnly = SessionHandler.DEFAULT_COOKIE_HTTP_ONLY_FLAG;
private int sessionIdMinLength = SessionHandler.DEFAULT_SESSIONID_MIN_LENGTH;
private CookieSameSite cookieSameSite = CookieSameSite.STRICT;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public SessionStoreType getStoreType() {
return this.storeType;
}

public void setStoreType(SessionStoreType storeType) {
this.storeType = storeType;
}

public String getSessionCookieName() {
return this.sessionCookieName;
}

public void setSessionCookieName(String sessionCookieName) {
this.sessionCookieName = sessionCookieName;
}

public String getSessionCookiePath() {
return this.sessionCookiePath;
}

public void setSessionCookiePath(String sessionCookiePath) {
this.sessionCookiePath = sessionCookiePath;
}

public long getSessionTimeOut() {
return this.sessionTimeOut;
}

public void setSessionTimeout(long timeout) {
this.sessionTimeOut = timeout;
}

public boolean isCookieSecure() {
return this.cookieSecure;
}

// Instructs browsers to only send the cookie over HTTPS when set.
public void setCookieSecure(boolean cookieSecure) {
this.cookieSecure = cookieSecure;
}

public boolean isCookieHttpOnly() {
return this.cookieHttpOnly;
}

// Instructs browsers to prevent Javascript access to the cookie.
// Defends against XSS attacks.
public void setCookieHttpOnly(boolean cookieHttpOnly) {
this.cookieHttpOnly = cookieHttpOnly;
}

public int getSessionIdMinLength() {
return this.sessionIdMinLength;
}

public void setSessionIdMinLength(int sessionIdMinLength) {
this.sessionIdMinLength = sessionIdMinLength;
}

public CookieSameSite getCookieSameSite() {
return this.cookieSameSite;
}

public void setCookieSameSite(CookieSameSite cookieSameSite) {
this.cookieSameSite = cookieSameSite;
}

public SessionHandler createSessionHandler(Vertx vertx) {
SessionStore sessionStore = storeType.create(vertx);
SessionHandler handler = SessionHandler.create(sessionStore);
configure(handler);
return handler;
}

private void configure(SessionHandler handler) {
handler.setSessionTimeout(this.sessionTimeOut)
.setSessionCookieName(this.sessionCookieName)
.setSessionCookiePath(this.sessionCookiePath)
.setSessionTimeout(this.sessionTimeOut)
.setCookieHttpOnlyFlag(this.cookieHttpOnly)
.setCookieSecureFlag(this.cookieSecure)
.setMinLength(this.sessionIdMinLength)
.setCookieSameSite(this.cookieSameSite);
}
}

public enum SessionStoreType {
LOCAL {
@Override
public SessionStore create(Vertx vertx) {
return LocalSessionStore.create(vertx);
}
},
CLUSTERED {
@Override
public SessionStore create(Vertx vertx) {
return ClusteredSessionStore.create(vertx);
}
};

public abstract SessionStore create(Vertx vertx);
}

public static class Cors {
private boolean enabled;
private List<String> origins;
Expand Down

0 comments on commit f79496b

Please sign in to comment.