public boolean authenticate(HttpRequest request,
HttpResponse response,
LoginConfig config) throws IOException {
HttpServletRequest hreq =
(HttpServletRequest) request.getRequest();
// Have we already authenticated someone?
// Note: SingleSignOn does not call setUserPrincipal(),
// so whoever did it would have to be some custom valve
// in the pipeline
Principal principal = hreq.getUserPrincipal();
String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
if (principal != null)
{
if (debug >= 1)
log("Found principal '" + principal.getName() + "'");
// Associate the session with any existing SSO session
if (ssoId != null)
associate(ssoId, getSession(request, true));
return true;
}
// Is there an SSO session against which we can try to reauthenticate?
if (ssoId != null)
{
if (debug >= 1)
log("SSO Id set");
// Try to reauthenticate using data cached by SSO. If this fails,
// either the original SSO logon was of DIGEST or SSL (which
// we can't reauthenticate ourselves because there is no
// cached username and password), or the realm denied
// the user's reauthentication for some reason.
// In either case we have to prompt the user for a logon */
if (reauthenticateFromSSO(ssoId, request))
return true;
}
// Validate any credentials already included with this request
String authorization = request.getAuthorization();
String username = parseUsername(authorization);
String password = parsePassword(authorization);
principal = context.getRealm().authenticate(username, password);
if (principal != null)
{
register(request, response, principal, Constants.BASIC_METHOD,
username, password);
return (true);
}
// Send an "unauthorized" response and an appropriate challenge
String realmName = config.getRealmName();
if (realmName == null)
realmName = hreq.getServerName() + ":" + hreq.getServerPort();
// if (debug >= 1)
// log("Challenging for realm '" + realmName + "'");
HttpServletResponse hres =
(HttpServletResponse) response.getResponse();
hres.setHeader("WWW-Authenticate",
"Basic realm=\"" + realmName + "\"");
hres.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// hres.flushBuffer();
return (false);
}
Authenticate the user making this request, based on the specified
login configuration. Return true if any specified
constraint has been satisfied, or false if we have
created a response challenge already.
Differs from the standard Tomcat version in that it associates the
session of any request with any single sign-on session that may exist. |