if (httpRequest.getRemoteUser() == null) {
chain.doFilter(request, response);
return;
}
OAuthPrincipal oauthPrincipal = (OAuthPrincipal)request.getAttribute(OAuthConstants.ATTRIBUTE_AUTHENTICATED_OAUTH_PRINCIPAL);
if (oauthPrincipal == null) {
chain.doFilter(request, response);
return;
}
try {
socialNetworkService.updateOAuthInfo(oauthPrincipal.getOauthProviderType(), httpRequest.getRemoteUser(),
oauthPrincipal.getUserName(), oauthPrincipal.getAccessToken());
// Add some attribute to session, which will be read by OAuthLifecycle
session.setAttribute(OAuthConstants.ATTRIBUTE_LINKED_OAUTH_PROVIDER, oauthPrincipal.getOauthProviderType().getFriendlyName());
} catch (OAuthException gtnOauthOAuthException) {
// Show warning message if user with this facebookUsername (or googleUsername) already exists
if (gtnOauthOAuthException.getExceptionCode() == OAuthExceptionCode.DUPLICATE_OAUTH_PROVIDER_USERNAME) {
// Add some attribute to session, which will be read by OAuthLifecycle
session.setAttribute(OAuthConstants.ATTRIBUTE_EXCEPTION_AFTER_FAILED_LINK, gtnOauthOAuthException);
} else {
throw gtnOauthOAuthException;
}
}
String urlToRedirect = OAuthUtils.getURLToRedirectAfterLinkAccount(httpRequest, session);
if (log.isTraceEnabled()) {
log.trace("User profile successfully updated with new userName and accessToken. oauthProvider=" + oauthPrincipal.getOauthProviderType() +
", username=" + httpRequest.getRemoteUser() + ", oauthUsername=" + oauthPrincipal.getUserName());
log.trace("Will redirect user to URL: " + urlToRedirect);
}
httpResponse.sendRedirect(httpResponse.encodeRedirectURL(urlToRedirect));
}