Skip to content

Commit 66f2c4f

Browse files
committed
[UNDERTOW-2418] Adjust properly session timeout also in case when FORM is combined with other mechanisms
1 parent 124c3bc commit 66f2c4f

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

core/src/main/java/io/undertow/security/impl/FormAuthenticationMechanism.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange excha
172172
protected void handleRedirectBack(final HttpServerExchange exchange) {
173173
final Session session = Sessions.getSession(exchange);
174174
if (session != null) {
175-
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
176-
if (originalSessionTimeout != null) {
177-
session.setMaxInactiveInterval(originalSessionTimeout);
178-
}
175+
restoreOriginalSessionTimeout(session);
179176
final String location = (String) session.removeAttribute(LOCATION_ATTRIBUTE);
180177
if(location != null) {
181178
exchange.addDefaultResponseListener(new DefaultResponseListener() {
@@ -192,6 +189,20 @@ public boolean handleDefaultResponse(final HttpServerExchange exchange) {
192189
}
193190
}
194191

192+
protected void restoreOriginalSessionTimeout(final HttpServerExchange exchange) {
193+
final Session session = Sessions.getSession(exchange);
194+
restoreOriginalSessionTimeout(session);
195+
}
196+
197+
protected void restoreOriginalSessionTimeout(final Session session) {
198+
if (session != null) {
199+
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
200+
if (originalSessionTimeout != null) {
201+
session.setMaxInactiveInterval(originalSessionTimeout);
202+
}
203+
}
204+
}
205+
195206
public ChallengeResult sendChallenge(final HttpServerExchange exchange, final SecurityContext securityContext) {
196207

197208
// make sure a request to root context is handled with trailing slash. Otherwise call to j_security_check will not

core/src/main/java/io/undertow/security/impl/SecurityContextImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
*/
1818
package io.undertow.security.impl;
1919

20+
import static io.undertow.security.api.SecurityNotification.EventType.AUTHENTICATED;
21+
2022
import io.undertow.UndertowLogger;
2123
import io.undertow.UndertowMessages;
2224
import io.undertow.security.api.AuthenticationMechanism;
2325
import io.undertow.security.api.AuthenticationMechanism.AuthenticationMechanismOutcome;
2426
import io.undertow.security.api.AuthenticationMechanism.ChallengeResult;
2527
import io.undertow.security.api.AuthenticationMechanismContext;
2628
import io.undertow.security.api.AuthenticationMode;
29+
import io.undertow.security.api.NotificationReceiver;
30+
import io.undertow.security.api.SecurityNotification;
2731
import io.undertow.security.idm.Account;
2832
import io.undertow.security.idm.IdentityManager;
2933
import io.undertow.security.idm.PasswordCredential;
@@ -168,6 +172,16 @@ public void addAuthenticationMechanism(final AuthenticationMechanism handler) {
168172
}
169173
cur.next = new Node<>(handler);
170174
}
175+
if (handler instanceof FormAuthenticationMechanism) {
176+
registerNotificationReceiver(new NotificationReceiver() {
177+
@Override
178+
public void handleNotification(final SecurityNotification notification) {
179+
if (notification.getEventType() == AUTHENTICATED) {
180+
((FormAuthenticationMechanism) handler).restoreOriginalSessionTimeout(exchange);
181+
}
182+
}
183+
});
184+
}
171185
}
172186

173187
@Override

servlet/src/main/java/io/undertow/servlet/handlers/security/ServletFormAuthenticationMechanism.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818

1919
package io.undertow.servlet.handlers.security;
2020

21-
import static io.undertow.security.api.SecurityNotification.EventType.AUTHENTICATED;
2221
import static io.undertow.util.StatusCodes.OK;
2322

2423
import io.undertow.security.api.AuthenticationMechanism;
2524
import io.undertow.security.api.AuthenticationMechanismFactory;
26-
import io.undertow.security.api.SecurityContext;
27-
import io.undertow.security.api.NotificationReceiver;
28-
import io.undertow.security.api.SecurityNotification;
2925
import io.undertow.security.idm.IdentityManager;
3026
import io.undertow.security.impl.FormAuthenticationMechanism;
3127
import io.undertow.server.HttpServerExchange;
@@ -157,19 +153,6 @@ public ServletFormAuthenticationMechanism(FormParserFactory formParserFactory, S
157153
this.overrideInitial = overrideInitial;
158154
}
159155

160-
@Override
161-
public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) {
162-
securityContext.registerNotificationReceiver(new NotificationReceiver() {
163-
@Override
164-
public void handleNotification(final SecurityNotification notification) {
165-
if (notification.getEventType() == AUTHENTICATED) {
166-
getAndInitializeSession(exchange, false);
167-
}
168-
}
169-
});
170-
return super.authenticate(exchange, securityContext);
171-
}
172-
173156
@Override
174157
protected Integer servePage(final HttpServerExchange exchange, final String location) {
175158
final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
@@ -271,15 +254,17 @@ private Session getAndInitializeSession(final HttpServerExchange exchange, final
271254
session.setMaxInactiveInterval(authenticationSessionTimeout);
272255
}
273256
} else {
274-
final Integer originalSessionTimeout = (Integer) session.removeAttribute(ORIGINAL_SESSION_TIMEOUT);
275-
if (originalSessionTimeout != null) {
276-
session.setMaxInactiveInterval(originalSessionTimeout);
277-
}
257+
restoreOriginalSessionTimeout(session);
278258
}
279259

280260
return session;
281261
}
282262

263+
@Override
264+
protected void restoreOriginalSessionTimeout(final HttpServerExchange exchange) {
265+
getAndInitializeSession(exchange, false);
266+
}
267+
283268
private static class FormResponseWrapper extends HttpServletResponseWrapper {
284269

285270
private int status = OK;

0 commit comments

Comments
 (0)