package io.undertow.servlet.handlers.security;
import io.undertow.security.api.AuthenticatedSessionManager;
import io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession;
import io.undertow.security.api.NotificationReceiver;
import io.undertow.security.api.SecurityContext;
import io.undertow.security.api.SecurityNotification;
import io.undertow.security.api.SecurityNotification.EventType;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.session.Session;
import io.undertow.servlet.handlers.ServletRequestContext;
import io.undertow.servlet.spec.HttpSessionImpl;
import io.undertow.servlet.spec.ServletContextImpl;
import io.undertow.servlet.util.SavedRequest;
import java.security.AccessController;
import javax.servlet.http.HttpSession;
public class CachedAuthenticatedSessionHandler implements HttpHandler {
public static final String ATTRIBUTE_NAME = CachedAuthenticatedSessionHandler.class.getName() + ".AuthenticatedSession";
public static final String NO_ID_CHANGE_REQUIRED = CachedAuthenticatedSessionHandler.class.getName() + ".NoIdChangeRequired";
private final NotificationReceiver NOTIFICATION_RECEIVER = new SecurityNotificationReceiver();
private final AuthenticatedSessionManager SESSION_MANAGER = new ServletAuthenticatedSessionManager();
private final HttpHandler next;
private final ServletContextImpl servletContext;
public CachedAuthenticatedSessionHandler(final HttpHandler next, final ServletContextImpl servletContext) {
this.next = next;
this.servletContext = servletContext;
}
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
SecurityContext securityContext = exchange.getSecurityContext();
securityContext.registerNotificationReceiver(NOTIFICATION_RECEIVER);
HttpSession session = servletContext.getSession(exchange, false);
if (session != null) {
exchange.putAttachment(AuthenticatedSessionManager.ATTACHMENT_KEY, SESSION_MANAGER);
SavedRequest.tryRestoreRequest(exchange, session);
}
next.handleRequest(exchange);
}
private class SecurityNotificationReceiver implements NotificationReceiver {
@Override
public void handleNotification(SecurityNotification notification) {
EventType eventType = notification.getEventType();
HttpSessionImpl httpSession = servletContext.getSession(notification.getExchange(), false);
switch (eventType) {
case AUTHENTICATED:
if (isCacheable(notification)) {
if(servletContext.getDeployment().getDeploymentInfo().isChangeSessionIdOnLogin()) {
if (httpSession != null) {
Session session = underlyingSession(httpSession);
if (!httpSession.isNew() &&
!httpSession.isInvalid() &&
session.getAttribute(NO_ID_CHANGE_REQUIRED) == null) {
ServletRequestContext src = notification.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY);
src.getOriginalRequest().changeSessionId();
}
if(session.getAttribute(NO_ID_CHANGE_REQUIRED) == null) {
session.setAttribute(NO_ID_CHANGE_REQUIRED, true);
}
}
}
if(httpSession == null) {
httpSession = servletContext.getSession(notification.getExchange(), true);
}
Session session = underlyingSession(httpSession);
session.setAttribute(ATTRIBUTE_NAME,
new AuthenticatedSession(notification.getAccount(), notification.getMechanism()));
}
break;
case LOGGED_OUT:
if (httpSession != null) {
Session session = underlyingSession(httpSession);
session.removeAttribute(ATTRIBUTE_NAME);
session.removeAttribute(NO_ID_CHANGE_REQUIRED);
}
break;
}
}
}
protected Session underlyingSession(HttpSessionImpl httpSession) {
Session session;
if (System.getSecurityManager() == null) {
session = httpSession.getSession();
} else {
session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
}
return session;
}
private class ServletAuthenticatedSessionManager implements AuthenticatedSessionManager {
@Override
public AuthenticatedSession lookupSession(HttpServerExchange exchange) {
HttpSessionImpl httpSession = servletContext.getSession(exchange, false);
if (httpSession != null) {
Session session = underlyingSession(httpSession);
return (AuthenticatedSession) session.getAttribute(ATTRIBUTE_NAME);
}
return null;
}
@Override
public void clearSession(HttpServerExchange exchange) {
HttpSessionImpl httpSession = servletContext.getSession(exchange, false);
if (httpSession != null) {
Session session = underlyingSession(httpSession);
session.removeAttribute(ATTRIBUTE_NAME);
}
}
}
private boolean isCacheable(final SecurityNotification notification) {
return notification.isProgramatic() || notification.isCachingRequired();
}
}