Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNDERTOW-2418] Adjust properly session timeout also in case when FORM is combined with other mechanisms #1635

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange excha
protected void handleRedirectBack(final HttpServerExchange exchange) {
final Session session = Sessions.getSession(exchange);
if (session != null) {
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
if (originalSessionTimeout != null) {
session.setMaxInactiveInterval(originalSessionTimeout);
}
restoreOriginalSessionTimeout(session);
final String location = (String) session.removeAttribute(LOCATION_ATTRIBUTE);
if(location != null) {
exchange.addDefaultResponseListener(new DefaultResponseListener() {
Expand All @@ -192,6 +189,20 @@ public boolean handleDefaultResponse(final HttpServerExchange exchange) {
}
}

protected void restoreOriginalSessionTimeout(final HttpServerExchange exchange) {
final Session session = Sessions.getSession(exchange);
restoreOriginalSessionTimeout(session);
}

protected void restoreOriginalSessionTimeout(final Session session) {
if (session != null) {
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
if (originalSessionTimeout != null) {
session.setMaxInactiveInterval(originalSessionTimeout);
}
}
}

public ChallengeResult sendChallenge(final HttpServerExchange exchange, final SecurityContext securityContext) {

// make sure a request to root context is handled with trailing slash. Otherwise call to j_security_check will not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
*/
package io.undertow.security.impl;

import static io.undertow.security.api.SecurityNotification.EventType.AUTHENTICATED;

import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.security.api.AuthenticationMechanism;
import io.undertow.security.api.AuthenticationMechanism.AuthenticationMechanismOutcome;
import io.undertow.security.api.AuthenticationMechanism.ChallengeResult;
import io.undertow.security.api.AuthenticationMechanismContext;
import io.undertow.security.api.AuthenticationMode;
import io.undertow.security.api.NotificationReceiver;
import io.undertow.security.api.SecurityNotification;
import io.undertow.security.idm.Account;
import io.undertow.security.idm.IdentityManager;
import io.undertow.security.idm.PasswordCredential;
Expand Down Expand Up @@ -168,6 +172,16 @@ public void addAuthenticationMechanism(final AuthenticationMechanism handler) {
}
cur.next = new Node<>(handler);
}
if (handler instanceof FormAuthenticationMechanism) {
registerNotificationReceiver(new NotificationReceiver() {
@Override
public void handleNotification(final SecurityNotification notification) {
if (notification.getEventType() == AUTHENTICATED) {
((FormAuthenticationMechanism) handler).restoreOriginalSessionTimeout(exchange);
}
}
});
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@

package io.undertow.servlet.handlers.security;

import static io.undertow.security.api.SecurityNotification.EventType.AUTHENTICATED;
import static io.undertow.util.StatusCodes.OK;

import io.undertow.security.api.AuthenticationMechanism;
import io.undertow.security.api.AuthenticationMechanismFactory;
import io.undertow.security.api.SecurityContext;
import io.undertow.security.api.NotificationReceiver;
import io.undertow.security.api.SecurityNotification;
import io.undertow.security.idm.IdentityManager;
import io.undertow.security.impl.FormAuthenticationMechanism;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -157,19 +153,6 @@ public ServletFormAuthenticationMechanism(FormParserFactory formParserFactory, S
this.overrideInitial = overrideInitial;
}

@Override
public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) {
securityContext.registerNotificationReceiver(new NotificationReceiver() {
@Override
public void handleNotification(final SecurityNotification notification) {
if (notification.getEventType() == AUTHENTICATED) {
getAndInitializeSession(exchange, false);
}
}
});
return super.authenticate(exchange, securityContext);
}

@Override
protected Integer servePage(final HttpServerExchange exchange, final String location) {
final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
Expand Down Expand Up @@ -271,15 +254,17 @@ private Session getAndInitializeSession(final HttpServerExchange exchange, final
session.setMaxInactiveInterval(authenticationSessionTimeout);
}
} else {
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
if (originalSessionTimeout != null) {
session.setMaxInactiveInterval(originalSessionTimeout);
}
restoreOriginalSessionTimeout(session);
}

return session;
}

@Override
protected void restoreOriginalSessionTimeout(final HttpServerExchange exchange) {
getAndInitializeSession(exchange, false);
}

private static class FormResponseWrapper extends HttpServletResponseWrapper {

private int status = OK;
Expand Down
Loading