{
resp.setContentType("text/html; charset=UTF-8");
HttpSession session = req.getSession();
// Looking for credentials stored in the session
Credentials credentials = (Credentials)session.getAttribute(InitiateLoginServlet.CREDENTIALS);
//
if (credentials == null)
{
PortalContainer pContainer = PortalContainer.getInstance();
ServletContext context = pContainer.getPortalContext();
//
String token = getRememberMeTokenCookie(req);
if (token != null)
{
AbstractTokenService tokenService = AbstractTokenService.getInstance(CookieTokenService.class);
credentials = tokenService.validateToken(token, false);
if (credentials == null)
{
log.debug("Login initiated with no credentials in session but found token an invalid " + token + " " +
"that will be cleared in next response");
// We clear the cookie in the next response as it was not valid
Cookie cookie = new Cookie(InitiateLoginServlet.COOKIE_NAME, "");
cookie.setPath(req.getContextPath());
cookie.setMaxAge(0);
resp.addCookie(cookie);
// This allows the customer to define another login page without
// changing the portal
showLoginForm(req, resp);
}
else
{
// Send authentication request
log.debug("Login initiated with no credentials in session but found token " + token + " with existing credentials, " +
"performing authentication");
sendAuth(resp, credentials.getUsername(), token);
}
}
else
{
// This allows the customer to define another login page without
// changing the portal
log.debug("Login initiated with no credentials in session and no token cookie, redirecting to login page");
showLoginForm(req, resp);
}
}
else
{
// We create a temporary token just for the login time
TransientTokenService tokenService = AbstractTokenService.getInstance(TransientTokenService.class);
String token = tokenService.createToken(credentials);
req.getSession().removeAttribute(InitiateLoginServlet.CREDENTIALS);
// Send authentication request
log.debug("Login initiated with credentials in session, performing authentication");
sendAuth(resp, credentials.getUsername(), token);
}
}