@RequestMapping(value = "/user/register.html", method = RequestMethod.GET)
  public String registerOrLoginSocialUser(WebRequest request) {
    Connection<?> connection = ProviderSignInUtils.getConnection(request);

    if (connection != null) {
      String providerId = connection.getKey().getProviderId();
      String providerUserId = connection.getKey().getProviderUserId();

      Usuario usuario = usuarioService.buscarPorProviderYuserId(providerId, providerUserId);

      if (usuario == null) {
        UserProfile profile = connection.fetchUserProfile();
        usuario = new Usuario();
        usuario.setNombre(profile.getFirstName());
        usuario.setApellidos(profile.getLastName());
        usuario.setCorreo(profile.getEmail());
        usuario.setFoto(connection.getImageUrl());
        usuario.setClave(null);
        usuario.setProviderid(providerId);
        usuario.setProvideruserid(providerUserId);
        usuario.setRol(new Rol(1));

        usuarioService.save(usuario);
      }

      SecurityUtil.logInUserSocial(usuario);
    }

    return "redirect:/index.html";
  }
 /**
  * @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();
   }
 }
  @Override
  public void postConnect(Connection<Facebook> connection, WebRequest request) {
    UserProfile userProfile = connection.fetchUserProfile();

    FacebookDTO dto = new FacebookDTO();
    dto.setDisplayName(connection.getDisplayName());
    dto.setEmail(userProfile.getEmail());
    dto.setId(extractId(connection.getProfileUrl()));
    dto.setImageURL(getImageUrl(connection, 285, 285));

    userTaskService.loginOrCreateFacebookUser(dto);
  }
예제 #4
0
 /**
  * Attempt to retrieve signup from social provider.
  *
  * @param contextId
  * @param request
  */
 public Signup socialSignUpAttempt(int contextId, WebRequest request) {
   Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
   if (connection != null) {
     UserProfile providerUser = connection.fetchUserProfile();
     if (providerUser != null) {
       return new Signup(
           contextId,
           providerUser.getEmail(),
           providerUser.getFirstName(),
           providerUser.getLastName());
     }
   }
   return new Signup(contextId, "");
 }
예제 #5
0
  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;
  }
예제 #6
0
  @Override
  public String execute(Connection<?> connection) {
    User user = new User();
    UserProfile userProfile = connection.fetchUserProfile();
    user.setName(userProfile.getName());
    user.setEmail(userProfile.getEmail());
    user.setImageUrl(connection.getImageUrl());
    String username = getUsernameFromProfile(userProfile);
    user.setUsername(username);
    Set<String> auth = new HashSet<String>();
    auth.add(Role.USER_ROLE);
    user.setAuthorities(auth);
    user.setEnabled(true);

    User createdUser = userRepository.store(user);
    return createdUser.getUsername();
  }
 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));
 }
예제 #8
0
 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;
 }
  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);
  }
예제 #10
0
  @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;
  }