private String getUsernameFromProfile(UserProfile profile) { String username; if (profile.getEmail() != null) { username = profile.getEmail(); } else if (profile.getUsername() != null) { username = profile.getUsername(); } else if (profile.getName() != null) { username = profile.getName(); } else { username = profile.toString(); } return username; }
@ApiOperation( value = "SNS 기반 회원 가입시 필요한 회원 프로필 정보", produces = "application/json", response = UserProfileForm.class) @RequestMapping(value = "/social/attempted", method = RequestMethod.GET) public UserProfileForm loginSocialUser(NativeWebRequest request) { Connection<?> connection = providerSignInUtils.getConnectionFromSession(request); if (Objects.isNull(connection)) throw new ServiceException(ServiceError.CANNOT_GET_SNS_PROFILE); ConnectionKey connectionKey = connection.getKey(); CommonConst.ACCOUNT_TYPE convertProviderId = CommonConst.ACCOUNT_TYPE.valueOf(connectionKey.getProviderId().toUpperCase()); UserProfile existUser = userService.findUserProfileByProviderIdAndProviderUserId( convertProviderId, connectionKey.getProviderUserId()); org.springframework.social.connect.UserProfile socialProfile = connection.fetchUserProfile(); String username = null; if (Objects.nonNull(socialProfile.getName())) { username = socialProfile.getName(); } else if (Objects.nonNull(socialProfile.getUsername())) { username = socialProfile.getUsername(); } else { if (Objects.nonNull(socialProfile.getFirstName())) { username = socialProfile.getFirstName(); } if (Objects.nonNull(socialProfile.getLastName())) { username = Objects.isNull(username) ? socialProfile.getLastName() : ' ' + socialProfile.getLastName(); } } UserProfileForm user = new UserProfileForm(); user.setEmail(socialProfile.getEmail()); user.setUsername(username); if (Objects.nonNull(existUser)) { user.setId(existUser.getId()); user.setAbout(existUser.getAbout()); if (Objects.nonNull(existUser.getSupportFC())) user.setFootballClub(existUser.getSupportFC().getId()); } return user; }
/** * @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 OAuth2Authentication extractAuthentication(UserProfile user) { String principal = user.getUsername(); List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"); OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null); return new OAuth2Authentication( request, new UsernamePasswordAuthenticationToken(principal, "N/A", authorities)); }
private Connection<?> createConnection( String login, String email, String firstName, String lastName, String providerId) { UserProfile userProfile = mock(UserProfile.class); when(userProfile.getEmail()).thenReturn(email); when(userProfile.getUsername()).thenReturn(login); when(userProfile.getFirstName()).thenReturn(firstName); when(userProfile.getLastName()).thenReturn(lastName); Connection<?> connection = mock(Connection.class); ConnectionKey key = new ConnectionKey(providerId, "PROVIDER_USER_ID"); when(connection.fetchUserProfile()).thenReturn(userProfile); when(connection.getKey()).thenReturn(key); return connection; }
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); }