-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2155 from moifort/feature/social
Add social sign in functionality (Google, Facebook, Twitter)
- Loading branch information
Showing
77 changed files
with
2,743 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/templates/src/main/java/package/social/_SocialController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package <%=packageName%>.social; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.web.ProviderSignInUtils; | ||
import org.springframework.social.support.URIBuilder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.CookieValue; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import javax.inject.Inject; | ||
|
||
@Controller | ||
@RequestMapping("/social") | ||
public class SocialController { | ||
private final Logger log = LoggerFactory.getLogger(SocialController.class); | ||
|
||
@Inject | ||
private SocialService socialService; | ||
|
||
@Inject | ||
private ProviderSignInUtils providerSignInUtils; | ||
|
||
@RequestMapping(value = "/signup", method = RequestMethod.GET) | ||
public RedirectView signUp(WebRequest webRequest, @CookieValue("NG_TRANSLATE_LANG_KEY") String langKey) { | ||
try { | ||
Connection<?> connection = providerSignInUtils.getConnectionFromSession(webRequest); | ||
socialService.createSocialUser(connection, langKey.replace("\"", "")); | ||
return new RedirectView(URIBuilder.fromUri("/#/social-register/" + connection.getKey().getProviderId()) | ||
.queryParam("success", "true") | ||
.build().toString(), true); | ||
} catch (Exception e) { | ||
log.error("Exception creating social user: ", e); | ||
return new RedirectView(URIBuilder.fromUri("/#/social-register/no-provider") | ||
.queryParam("success", "false") | ||
.build().toString(), true); | ||
} | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
app/templates/src/main/java/package/social/_SocialService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package <%=packageName%>.social; | ||
|
||
import <%=packageName%>.domain.Authority; | ||
import <%=packageName%>.domain.User; | ||
import <%=packageName%>.repository.AuthorityRepository; | ||
import <%=packageName%>.repository.UserRepository; | ||
import <%=packageName%>.service.MailService; | ||
|
||
import org.apache.commons.lang.RandomStringUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.ConnectionRepository; | ||
import org.springframework.social.connect.UserProfile; | ||
import org.springframework.social.connect.UsersConnectionRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.inject.Inject; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@Service | ||
public class SocialService { | ||
private final Logger log = LoggerFactory.getLogger(SocialService.class); | ||
|
||
@Inject | ||
private UsersConnectionRepository usersConnectionRepository; | ||
|
||
@Inject | ||
private AuthorityRepository authorityRepository; | ||
|
||
@Inject | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Inject | ||
private UserRepository userRepository; | ||
|
||
@Inject | ||
private MailService mailService; | ||
|
||
public void createSocialUser(Connection<?> connection, String langKey) { | ||
if (connection == null) { | ||
log.error("Cannot create social user because connection is null"); | ||
throw new IllegalArgumentException("Connection cannot be null"); | ||
} | ||
UserProfile userProfile = connection.fetchUserProfile(); | ||
String providerId = connection.getKey().getProviderId(); | ||
User user = createUserIfNotExist(userProfile, langKey, providerId); | ||
createSocialConnection(user.getLogin(), connection); | ||
mailService.sendSocialRegistrationValidationEmail(user, providerId); | ||
} | ||
|
||
private User createUserIfNotExist(UserProfile userProfile, String langKey, String providerId) { | ||
String email = userProfile.getEmail(); | ||
String userName = userProfile.getUsername(); | ||
if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) { | ||
log.error("Cannot create social user because email and login are null"); | ||
throw new IllegalArgumentException("Email and login cannot be null"); | ||
} | ||
if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) { | ||
log.error("Cannot create social user because email is null and login already exist, login -> {}", userName); | ||
throw new IllegalArgumentException("Email cannot be null with an existing login"); | ||
} | ||
Optional<User> user = userRepository.findOneByEmail(email); | ||
if (user.isPresent()) { | ||
log.info("User already exist associate the connection to this account"); | ||
return user.get(); | ||
} | ||
|
||
String login = getLoginDependingOnProviderId(userProfile, providerId); | ||
String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10)); | ||
Set<Authority> authorities = new HashSet<>(1); | ||
authorities.add(authorityRepository.findOne("ROLE_USER")); | ||
|
||
User newUser = new User(); | ||
newUser.setLogin(login); | ||
newUser.setPassword(encryptedPassword); | ||
newUser.setFirstName(userProfile.getFirstName()); | ||
newUser.setLastName(userProfile.getLastName()); | ||
newUser.setEmail(email); | ||
newUser.setActivated(true); | ||
newUser.setAuthorities(authorities); | ||
newUser.setLangKey(langKey); | ||
|
||
return userRepository.save(newUser); | ||
} | ||
|
||
/** | ||
* @param userProfile | ||
* @param providerId | ||
* @return login if provider manage a login like Twitter or Github otherwise email address. | ||
* Because provider like Google or Facebook didn't provide login or login like "12099388847393" | ||
*/ | ||
private String getLoginDependingOnProviderId(UserProfile userProfile, String providerId) { | ||
switch (providerId) { | ||
case "twitter": | ||
return userProfile.getUsername(); | ||
default: | ||
return userProfile.getEmail(); | ||
} | ||
} | ||
|
||
private void createSocialConnection(String login, Connection<?> connection) { | ||
ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(login); | ||
connectionRepository.addConnection(connection); | ||
} | ||
} |
Oops, something went wrong.