Skip to content

Commit

Permalink
Refactor so Principal is never cached in session with cache==false
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 7, 2019
1 parent c31917d commit ab72a10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
5 changes: 3 additions & 2 deletions java/org/apache/catalina/authenticator/AuthenticatorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,11 @@ public void register(Request request, HttpServletResponse response, Principal pr
}

// Cache the authentication information in our session, if any
if (cache) {
if (session != null) {
if (session != null) {
if (cache) {
session.setAuthType(authType);
session.setPrincipal(principal);
} else {
if (username != null) {
session.setNote(Constants.SESS_USERNAME_NOTE, username);
} else {
Expand Down
3 changes: 3 additions & 0 deletions java/org/apache/catalina/authenticator/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public class Constants {

/**
* The previously authenticated principal (if caching is disabled).
*
* @deprecated Unused. Will be removed in Tomcat 10.
*/
@Deprecated
public static final String FORM_PRINCIPAL_NOTE = "org.apache.catalina.authenticator.PRINCIPAL";

/**
Expand Down
33 changes: 9 additions & 24 deletions java/org/apache/catalina/authenticator/FormAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ public boolean authenticate(Request request,
LoginConfig config)
throws IOException {

if (checkForCachedAuthentication(request, response, true)) {
return true;
}

// References to objects we will need later
Session session = null;
Principal principal = null;
Expand All @@ -175,9 +171,8 @@ public boolean authenticate(Request request,
}
principal = context.getRealm().authenticate(username, password);
if (principal != null) {
session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);
if (!matchRequest(request)) {
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);
return true;
}
}
Expand All @@ -194,16 +189,6 @@ public boolean authenticate(Request request,
if (log.isDebugEnabled()) {
log.debug("Restore request from session '" + session.getIdInternal() + "'");
}
principal = (Principal) session.getNote(Constants.FORM_PRINCIPAL_NOTE);
register(request, response, principal, HttpServletRequest.FORM_AUTH,
(String) session.getNote(Constants.SESS_USERNAME_NOTE),
(String) session.getNote(Constants.SESS_PASSWORD_NOTE));
// If we're caching principals we no longer need the user name
// and password in the session, so remove them
if (cache) {
session.removeNote(Constants.SESS_USERNAME_NOTE);
session.removeNote(Constants.SESS_PASSWORD_NOTE);
}
if (restoreRequest(request, session)) {
if (log.isDebugEnabled()) {
log.debug("Proceed to restored request");
Expand All @@ -218,6 +203,12 @@ public boolean authenticate(Request request,
}
}

// This check has to be after the previous check for a matching request
// because that matching request may also include a cached Principal.
if (checkForCachedAuthentication(request, response, true)) {
return true;
}

// Acquire references to objects we will need to evaluate
String contextPath = request.getContextPath();
String requestURI = request.getDecodedRequestURI();
Expand Down Expand Up @@ -302,12 +293,7 @@ public boolean authenticate(Request request,
return false;
}

// Save the authenticated Principal in our session
session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);

// Save the username and password as well
session.setNote(Constants.SESS_USERNAME_NOTE, username);
session.setNote(Constants.SESS_PASSWORD_NOTE, password);
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);

// Redirect the user to the original request URI (which will cause
// the original request to be restored)
Expand Down Expand Up @@ -502,7 +488,7 @@ protected boolean matchRequest(Request request) {
}

// Is there a saved principal?
if (session.getNote(Constants.FORM_PRINCIPAL_NOTE) == null) {
if (cache && session.getPrincipal() == null || !cache && request.getPrincipal() == null) {
return false;
}

Expand Down Expand Up @@ -532,7 +518,6 @@ protected boolean restoreRequest(Request request, Session session)
// Retrieve and remove the SavedRequest object from our session
SavedRequest saved = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
session.removeNote(Constants.FORM_REQUEST_NOTE);
session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
if (saved == null) {
return false;
}
Expand Down

0 comments on commit ab72a10

Please sign in to comment.