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

concord-server: invalidate session on failed login #859

Merged
merged 4 commits into from
Jan 15, 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 @@ -42,6 +42,7 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
Expand Down Expand Up @@ -139,14 +140,23 @@ protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, Ser

@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
log.warn("onLoginFailure ['{}'] -> login failed ({}): {}", token, request.getRemoteAddr(), e.getMessage());
log.debug("onLoginFailure ['{}'] -> login failed ({}): {}", token, request.getRemoteAddr(), e.getMessage());
failedAuths.mark();

Subject s = ThreadContext.getSubject();
if (s != null) {
s.logout();
}

HttpSession session = ((HttpServletRequest) request).getSession(false);
if (session != null) {
session.invalidate();

// remove JSESSIONID cookie
HttpServletResponse resp = WebUtils.toHttp(response);
resp.addHeader("Set-Cookie", "JSESSIONID=; Path=/; HttpOnly");
}

return super.onLoginFailure(token, e, request, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
* =====
*/

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.web.util.WebUtils;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.util.Pac4jConstants;

import javax.inject.Inject;
Expand All @@ -51,7 +53,7 @@ public OidcCallbackFilter(PluginConfiguration cfg,

@Override
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
HttpServletRequest req = WebUtils.toHttp(request);
HttpServletResponse resp = WebUtils.toHttp(response);

Expand All @@ -67,8 +69,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
postLoginUrl = cfg.getAfterLoginUrl();
}

CallbackLogic<?, JEEContext> callback = pac4jConfig.getCallbackLogic();
callback.perform(context, pac4jConfig, pac4jConfig.getHttpActionAdapter(), postLoginUrl, true, false, true, OidcPluginModule.CLIENT_NAME);
try {
CallbackLogic<?, JEEContext> callback = pac4jConfig.getCallbackLogic();
callback.perform(context, pac4jConfig, pac4jConfig.getHttpActionAdapter(), postLoginUrl, true, false, true, OidcPluginModule.CLIENT_NAME);
} catch (TechnicalException e) {
throw new AuthorizationException("OIDC callback error: " + e.getMessage());
}
}

@Override
Expand Down