String loginServerName = (String)httpSession.getAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME);
if (loginServerName != null)
{
httpSession.removeAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME);
}
OpenIDRegistrationConfiguration portalRegistrationConfiguration = (OpenIDRegistrationConfiguration)httpSession.getAttribute(OpenIDConstants.OPEN_ID_REGISTRATION_CONFIGURATION);
if (portalRegistrationConfiguration != null)
{
httpSession.removeAttribute(OpenIDConstants.OPEN_ID_REGISTRATION_CONFIGURATION);
}
boolean authenticatedByProvider = false;
boolean portalUserExists = false;
try
{
// request parameters
ParameterList authParams = new ParameterList(request.getParameterMap());
// retrieve OpenID provider from session
String provider = (String)httpSession.getAttribute(OPEN_ID_PROVIDER_ATTR_NAME);
DiscoveryInformation discovered = (DiscoveryInformation)httpSession.getAttribute(OPEN_ID_DISCOVERY_INFO_ATTR_NAME);
// reconstruct the authenticated request URL
StringBuffer authRequestURLBuffer = request.getRequestURL();
String authRequestQueryString = request.getQueryString();
if ((authRequestQueryString != null) && (authRequestQueryString.length() > 0))
{
authRequestURLBuffer.append('?').append(authRequestQueryString);
}
String authRequestURL = authRequestURLBuffer.toString();
// select consumer implementation based on provider
String providerConsumer = OPEN_ID_CONSUMER_INIT_PARAM_NAME_VALUE;
if (provider != null)
{
String consumer = getInitParameter(OPEN_ID_CONSUMER_INIT_PARAM_NAME_PREFIX+provider);
if ((consumer != null) && consumer.equals(STEP2_CONSUMER_INIT_PARAM_NAME_VALUE))
{
providerConsumer = STEP2_CONSUMER_INIT_PARAM_NAME_VALUE;
}
}
// verify the authenticated request
VerificationResults verificationResults = null;
if (providerConsumer.equals(STEP2_CONSUMER_INIT_PARAM_NAME_VALUE))
{
// Step2 OpenId verification
verificationResults = openIDStep2Verification(authRequestURL, authParams, discovered);
}
else
{
// standard OpenId verification
verificationResults = openIDVerification(authRequestURL, authParams, discovered);
}
VerificationResult verification = verificationResults.verification;
Identifier verifiedIdentifier = verificationResults.verifiedIdentifier;
// extract identifier from verified authenticated request
if (verifiedIdentifier == null)
{
throw new RuntimeException("Verified identifier unavailable for authenticated OpenID login");
}
authenticatedByProvider = true;
String email = null;
String firstName = null;
String lastName = null;
String nickname = null;
String fullName = null;
// extract requested attribute exchange data
AuthSuccess authResponse = (AuthSuccess)verification.getAuthResponse();
if (authResponse.hasExtension(AxMessage.OPENID_NS_AX))
{
try
{
FetchResponse axResponse = (FetchResponse)authResponse.getExtension(AxMessage.OPENID_NS_AX);
email = axResponse.getAttributeValue("email");
fullName = axResponse.getAttributeValue("fullname");
firstName = axResponse.getAttributeValue("firstname");
lastName = axResponse.getAttributeValue("lastname");
nickname = axResponse.getAttributeValue("nickname");
}
catch (OpenIDException oide)
{
throw new RuntimeException("Unexpected OpenID authenticated attribute exchange fetch exception: "+oide, oide);
}
}
// extract requested simple registration data
if (authResponse.hasExtension(SRegMessage.OPENID_NS_SREG))
{
try
{
SRegResponse sregResponse = (SRegResponse)authResponse.getExtension(SRegMessage.OPENID_NS_SREG);
email = sregResponse.getAttributeValue("email");
fullName = sregResponse.getAttributeValue("fullname");
nickname = sregResponse.getAttributeValue("nickname");
}
catch (OpenIDException oide)
{
throw new RuntimeException("Unexpected OpenID authenticated simple registration fetch exception: "+oide, oide);
}
}
// log authenticated request
if (log.isDebugEnabled())
{
log.debug("Authenticated OpenID verified identifier: "+verifiedIdentifier.getIdentifier()+", email="+email+", fullname="+fullName+", firstname="+firstName+", lastname="+lastName+", nickname="+nickname);
}
// validate and default attributes
if (email == null)
{
throw new RuntimeException("OpenID email attribute required for portal login");
}
if (fullName != null)
{
String [] fullNames = fullName.split("\\s");
if ((firstName == null) && (fullNames.length > 1))
{
firstName = fullNames[0];
}
if (lastName == null)
{
lastName = ((fullNames.length > 1) ? fullNames[fullNames.length-1] : fullName);
}
}
if ((nickname == null) && (firstName != null))
{
nickname = firstName;
}
if (nickname == null)
{
int emailDomainIndex = email.indexOf('@');
if (emailDomainIndex != -1)
{
nickname = email.substring(0, emailDomainIndex);
}
}
// construct portal user attributes
Map<String,String> userAttributes = new HashMap<String,String>();
userAttributes.put(USER_ATTRIBUTE_EMAIL, email);
userAttributes.put(USER_ATTRIBUTE_NAME, email);
if (firstName != null)
{
userAttributes.put(USER_ATTRIBUTE_GIVEN_NAME, firstName);
}
if (lastName != null)
{
userAttributes.put(USER_ATTRIBUTE_FAMILY_NAME, lastName);
}
if (nickname != null)
{
userAttributes.put(USER_ATTRIBUTE_NICKNAME, nickname);
}
// login to portal using email, creating portal
// user if necessary
User portalUser = null;
try
{
portalUser = portalUserManager.getUser(email);
}
catch (Exception e)
{
}
// create portal user if not found
if (portalUser == null)
{
try
{
// select portal registration configuration and
// register portal user
String logConfiguration = "none";
if (portalRegistrationConfiguration != null)
{
portalRegistrationConfiguration.merge(initRegistrationConfiguration);
logConfiguration = "session, (from login)";
}
else if (initRegistrationConfiguration != null)
{
portalRegistrationConfiguration = initRegistrationConfiguration;
logConfiguration = "init params";
}
if ((portalRegistrationConfiguration == null) || portalRegistrationConfiguration.isEnableRegistration())
{
if (portalRegistrationConfiguration != null)
{
portalAdministration.registerUser(email, null,
portalRegistrationConfiguration.getRoles(),
portalRegistrationConfiguration.getGroups(),
userAttributes,
portalRegistrationConfiguration.getProfilerRules(),
portalRegistrationConfiguration.getUserTemplateDirectory(),
portalRegistrationConfiguration.getSubsiteRootFolder(),
loginLocale, loginServerName);
}
else
{
portalAdministration.registerUser(email, null, null, null, userAttributes, null, null, null,