user.save();
// Update credentials for the user (at login or password change)
String provider = socialUser.identityId().providerId();
Credentials credentials = null;
for (Credentials creds : user.credentials) {
if (creds.providerId.equals(provider)) {
credentials = creds;
}
}
if (credentials == null) {
credentials = new Credentials();
}
credentials.extUserId = socialUser.identityId().userId();
credentials.providerId = socialUser.identityId().providerId();
if (socialUser.passwordInfo().isDefined()) {
PasswordInfo pInfo = socialUser.passwordInfo().get();
credentials.passwordHasher = pInfo.hasher();
credentials.password = pInfo.password();
if (pInfo.salt().isDefined()) {
credentials.passwordSalt = pInfo.salt().get();
}
}
if (socialUser.oAuth1Info().isDefined()) {
OAuth1Info oAuth1 = socialUser.oAuth1Info().get();
credentials.oAuth1Secret = oAuth1.secret();
credentials.oAuth1Token = oAuth1.token();
}
if (socialUser.oAuth2Info().isDefined()) {
OAuth2Info oAuth2 = socialUser.oAuth2Info().get();
credentials.oAuth2AccessToken = oAuth2.accessToken();
if (oAuth2.expiresIn().isDefined()) {
credentials.oAuth2ExpiresIn = (Integer) oAuth2.expiresIn().get();
}
if (oAuth2.tokenType().isDefined()) {
credentials.oAuth2TokenType = oAuth2.tokenType().get();
}
if (oAuth2.refreshToken().isDefined()) {
credentials.oAuth2RefreshToken = oAuth2.refreshToken().get();
}
}
credentials.user = user;
credentials.save();
return user;
}