providerOpenIDConsumerManager = openIDStep2ConsumerManager;
}
}
// OpenID discovery
DiscoveryInformation discovered = null;
try
{
if (userSuppliedDiscoveryString != null)
{
List discoveries = null;
if (providerConsumer.equals(STEP2_CONSUMER_INIT_PARAM_NAME_VALUE))
{
// verify discovery string is likely a host name
if ((userSuppliedDiscoveryString.indexOf("://") == -1) && (userSuppliedDiscoveryString.indexOf('@') == -1) && (userSuppliedDiscoveryString.indexOf('=') == -1))
{
// Step2 OpenId discovery
IdpIdentifier providerIdentifier = new IdpIdentifier(userSuppliedDiscoveryString);
discoveries = providerOpenIDConsumerManager.getDiscovery().discover(providerIdentifier);
}
}
else
{
// standard OpenId discovery
discoveries = providerOpenIDConsumerManager.discover(userSuppliedDiscoveryString);
}
if ((discoveries != null) && !discoveries.isEmpty())
{
discovered = providerOpenIDConsumerManager.associate(discoveries);
}
}
}
catch (OpenIDException oide)
{
throw new RuntimeException("Unexpected OpenID discovery exception: "+oide, oide);
}
if (discovered == null)
{
throw new RuntimeException("No OpenID provider discovered for: "+userSuppliedDiscoveryString);
}
discoveredProvider = true;
// log OpenID provider
if (log.isDebugEnabled())
{
log.debug("Discovered OpenID provider endpoint: "+discovered.getOPEndpoint()+", ["+discovered.getClass().getSimpleName()+"]");
}
// save login state
httpSession.setAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME, request.getLocale());
httpSession.setAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME, request.getServerName());
// save OpenID provider in session
httpSession.setAttribute(OPEN_ID_PROVIDER_ATTR_NAME, provider);
httpSession.setAttribute(OPEN_ID_DISCOVERY_INFO_ATTR_NAME, discovered);
// create OpenID authentication request and redirect
String authReturnToURL = openIDRealmURL+"/"+OpenIDConstants.OPEN_ID_AUTHENTICATED_REQUEST+"?"+OpenIDConstants.OPEN_ID_RETURN+"="+returnPath;
String authRedirectURL = null;
try
{
// authentication request
AuthRequest authRequest = providerOpenIDConsumerManager.authenticate(discovered, authReturnToURL, openIDRealmURL);
// request attribute exchange data
FetchRequest axRequest = FetchRequest.createFetchRequest();
axRequest.addAttribute("email", "http://axschema.org/contact/email", true);
axRequest.addAttribute("fullname", "http://axschema.org/namePerson", true);
axRequest.addAttribute("lastname", "http://axschema.org/namePerson/last", true);
axRequest.addAttribute("firstname", "http://axschema.org/namePerson/first", true);
axRequest.addAttribute("nickname", "http://axschema.org/namePerson/friendly", true);
authRequest.addExtension(axRequest);
// request simple registration data
SRegRequest sregRequest = SRegRequest.createFetchRequest();
sregRequest.addAttribute("email", true);
sregRequest.addAttribute("fullname", true);
sregRequest.addAttribute("nickname", true);
authRequest.addExtension(sregRequest);
// authentication redirect
authRedirectURL = authRequest.getDestinationUrl(true);
}
catch (OpenIDException oide)
{
throw new RuntimeException("Unexpected OpenID authentication request exception: "+oide, oide);
}
response.sendRedirect(authRedirectURL);
// log authentication redirect
if (log.isDebugEnabled())
{
log.debug("OpenID authentication redirect: "+authRedirectURL);
}
}
catch (Exception e)
{
// log error and redirect back to portal with error
// set as session attribute
log.error("OpenID login error: "+e, e);
httpSession.setAttribute(OpenIDConstants.OPEN_ID_ERROR, (!discoveredProvider ? OpenIDConstants.OPEN_ID_ERROR_NO_PROVIDER : OpenIDConstants.OPEN_ID_ERROR_CANNOT_AUTH));
response.sendRedirect(returnPath);
}
}
else if (requestPath.equals(OpenIDConstants.OPEN_ID_AUTHENTICATED_REQUEST))
{
// request parameters
String returnPath = request.getParameter(OpenIDConstants.OPEN_ID_RETURN);
if ((returnPath == null) || (returnPath.length() == 0))
{
returnPath = request.getContextPath()+"/";
}
// session parameters
Locale loginLocale = (Locale)httpSession.getAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME);
if (loginLocale != null)
{
httpSession.removeAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME);
}
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))