//No original person in session so check for swap request
if (originalUid == null) {
targetUid = (String)s.getAttribute(SWAP_TARGET_UID);
if (targetUid != null) {
final IPerson person = personManager.getPerson(request);
originalUid = person.getName();
swap = true;
}
}
else {
final IPerson person = personManager.getPerson(request);
targetUid = person.getName();
}
s.invalidate();
} catch (IllegalStateException ise) {
// ISE indicates session was already invalidated.
// This is fine. This servlet trying to guarantee that the session has been invalidated;
// it doesn't have to insist that it is the one that invalidated it.
if (log.isTraceEnabled()) {
log.trace("LoginServlet attempted to invalidate an already invalid session.", ise);
}
}
}
// Create the user's session
HttpSession s = request.getSession(true);
IPerson person = null;
try {
final HashMap principals;
final HashMap credentials;
// Get the person object associated with the request
person = personManager.getPerson(request);
//If doing an identity swap
if (targetUid != null && originalUid != null) {
if (swap) {
swapperLog.warn("Swapping identity for '" + originalUid + "' to '" + targetUid + "'");
//Track the originating user
s.setAttribute(SWAP_ORIGINAL_UID, originalUid);
//Setup the swapped person
person.setUserName(targetUid);
}
else {
swapperLog.warn("Reverting swapped identity from '" + targetUid + "' to '" + originalUid + "'");
person.setUserName(originalUid);
}
//Setup the custom security context
final IdentitySwapperPrincipal identitySwapperPrincipal = new IdentitySwapperPrincipal(person);
final IdentitySwapperSecurityContext identitySwapperSecurityContext = new IdentitySwapperSecurityContext(identitySwapperPrincipal);
person.setSecurityContext(identitySwapperSecurityContext);
principals = new HashMap();
credentials = new HashMap();
}
//Norm authN path
else {
// WE grab all of the principals and credentials from the request and load
// them into their respective HashMaps.
principals = getPropertyFromRequest (principalTokens, request);
credentials = getPropertyFromRequest (credentialTokens, request);
}
// Attempt to authenticate using the incoming request
m_authenticationService.authenticate(principals, credentials, person);
} catch (Exception e) {
// Log the exception
log.error("Exception authenticating the request", e);
// Reset everything
request.getSession(false).invalidate();
// Add the authentication failure
request.getSession(true).setAttribute("up_authenticationError", "true");
person = null;
}
// create the redirect URL, adding fname and args parameters if necessary
String redirectTarget = null;
final String refUrl = request.getParameter("refUrl");
if (refUrl != null) {
if (refUrl.startsWith("/")) {
redirectTarget = refUrl;
}
else {
log.warn("Refernce URL passed in does not start with a / and will be ignored: " + refUrl);
}
}
if (redirectTarget == null) {
/* Grab the target functional name, if any, off the login request.
* Also any arguments for the target
* We will pass them along after authentication.
*/
String targetFname = request.getParameter("uP_fname");
if (targetFname == null){
redirectTarget = request.getContextPath() + "/" + redirectString;
} else {
StringBuilder sb = new StringBuilder();
sb.append(request.getContextPath());
sb.append("/tag.idempotent.");
sb.append(redirectString);
sb.append("?uP_fname=");
sb.append(URLEncoder.encode(targetFname, "UTF-8"));
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String paramName = e.nextElement();
if(!paramName.equals("uP_fname")){
sb.append('&');
sb.append(paramName);
sb.append('=');
sb.append(URLEncoder.encode(request.getParameter(paramName),"UTF-8"));
}
}
redirectTarget = sb.toString();
}
}
if (person == null || !person.getSecurityContext().isAuthenticated()) {
if ( request.getMethod().equals("POST") )
request.getSession(false).setAttribute("up_authenticationAttempted", "true");
// Preserve the attempted username so it can be redisplayed to the user by CLogin
String attemptedUserName = request.getParameter("userName");
if (attemptedUserName != null)