Java Code Examples for org.springframework.social.connect.Connection.fetchUserProfile()

The following are Jave code examples for showing how to use fetchUserProfile() of the org.springframework.social.connect.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sentry   File: DiscordResource.java   View Source Code Vote up 6 votes
private DiscordConnectionVM viewModel(Connection<Discord> connection, boolean user, boolean guilds, boolean connections) {
    DiscordConnectionVM vm = new DiscordConnectionVM();
    vm.setUserId(connection.getKey().getProviderUserId());
    vm.setUsername(connection.getDisplayName());
    vm.setAvatarUrl(connection.getImageUrl());
    if (user) {
        UserProfile userProfile = connection.fetchUserProfile();
        vm.setNickname(userProfile.getFirstName() + "#" + userProfile.getLastName());
    }
    if (guilds) {
        vm.setGuilds(connection.getApi().userOperations().getGuilds());
    }
    if (connections) {
        vm.setConnections(connection.getApi().userOperations().getConnections());
    }
    return vm;
}
 
Example 2
Project: gamesboard   File: RegistrationController.java   View Source Code Vote up 6 votes
/**
 * Creates the form object used in the registration form.
 * @param connection
 * @return  If a user is signing in by using a social provider, this method returns a form
 *          object populated by the values given by the provider. Otherwise this method returns
 *          an empty form object (normal form registration).
 */
private RegistrationForm createRegistrationDTO(Connection<?> connection) {
    RegistrationForm dto = new RegistrationForm();

    if (connection != null) {
        UserProfile socialMediaProfile = connection.fetchUserProfile();
        dto.setEmail(socialMediaProfile.getEmail());
        dto.setFirstName(socialMediaProfile.getFirstName());
        dto.setLastName(socialMediaProfile.getLastName());

        ConnectionKey providerKey = connection.getKey();
        dto.setSignInProvider(SocialMediaService.valueOf(providerKey.getProviderId().toUpperCase()));
    }

    return dto;
}
 
Example 3
Project: Spring-Security-Third-Edition   File: SocialAuthenticationUtils.java   View Source Code Vote up 6 votes
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){
    // TODO: FIXME: Need to put this into a Utility:
    UserProfile profile = connection.fetchUserProfile();

    CalendarUser user = new CalendarUser();

    if(profile.getEmail() != null){
        user.setEmail(profile.getEmail());
    }
    else if(profile.getUsername() != null){
        user.setEmail(profile.getUsername());
    }
    else {
        user.setEmail(connection.getDisplayName());
    }

    user.setFirstName(profile.getFirstName());
    user.setLastName(profile.getLastName());

    user.setPassword(randomAlphabetic(32));

    return user;

}
 
Example 4
Project: sentry   File: DiscordResource.java   View Source Code Vote up 6 votes
private DiscordConnectionVM viewModel(Connection<Discord> connection, boolean user, boolean guilds, boolean connections) {
    DiscordConnectionVM vm = new DiscordConnectionVM();
    vm.setUserId(connection.getKey().getProviderUserId());
    vm.setUsername(connection.getDisplayName());
    vm.setAvatarUrl(connection.getImageUrl());
    if (user) {
        UserProfile userProfile = connection.fetchUserProfile();
        vm.setNickname(userProfile.getFirstName() + "#" + userProfile.getLastName());
    }
    if (guilds) {
        vm.setGuilds(connection.getApi().userOperations().getGuilds());
    }
    if (connections) {
        vm.setConnections(connection.getApi().userOperations().getConnections());
    }
    return vm;
}
 
Example 5
Project: travel_portal   File: UserDAO.java   View Source Code Vote up 6 votes
public User createUserAccount(Connection<?> connection) {

        UserProfile userProfile = connection.fetchUserProfile();

        String sql = "Insert into USERS "
                + " (id, email,user_name,first_name,last_name,role) "
                + " values (?,?,?,?,?,?) ";

        // Random string with 36 characters.
        String id = UUID.randomUUID().toString();

        String userName = userProfile.getFirstName().trim()
                + " " + userProfile.getLastName().trim();

        this.getJdbcTemplate().update(sql, id, userProfile.getEmail(), userName,
                userProfile.getFirstName(), userProfile.getLastName(), User.ROLE_USER);
        return findById(id);
    }
 
Example 6
Project: spring-oauth-social-microservice-starter   File: SocialConnectionSignUp.java   View Source Code Vote up 6 votes
public String execute(Connection<?> connection) {
      UserProfile profile = connection.fetchUserProfile();

      // If the user already exists we return the user id (email)
      Optional<User> user = userService.findByEmail(profile.getEmail());
      if (user.isPresent()) {
          log.info("Connecting existing account to " + connection.getKey().getProviderId());
          return user.get().getEmail();
      }

      // The user doesn't exist so we need to create them
      User newUser = userService.create(profile.getEmail(), profile.getFirstName(), profile.getLastName());

log.info("Creating new user {} from {}", newUser.getEmail(), connection.getKey().getProviderId());

      return newUser.getEmail();
  }
 
Example 7
Project: music-for-all-application   File: AccountConnectionSignUpService.java   View Source Code Vote up 6 votes
@Override
public String execute(Connection<?> connection) {
    final UserProfile profile = connection.fetchUserProfile();
    final User user = new User();
    user.setEmail(profile.getEmail());
    if ((profile.getUsername() == null) ||
            (profile.getUsername().length() < 2) ||
            (profile.getUsername().length() > MAX_NAME_LENGTH)) {
        user.setUserData(new UserData(user, profile.getFirstName(), profile.getFirstName(),
                profile.getLastName(), connection.getImageUrl(), "", false));
    } else {
        user.setUserData(new UserData(user, profile.getUsername(), profile.getFirstName(),
                profile.getLastName(), connection.getImageUrl(), "", false));
    }
    user.setPassword(KeyGenerators.string().generateKey());
    if (userService.getByEmail(user.getEmail()) == null) {
        userService.save(user);
        publisher.publishEvent(mails.welcomeMail(user));
    }
    return user.getEmail();
}
 
Example 8
Project: metaworks_framework   File: BroadleafSocialRegisterController.java   View Source Code Vote up 6 votes
public String register(RegisterCustomerForm registerCustomerForm, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
    Connection<?> connection = ProviderSignInUtils.getConnection(new ServletWebRequest(request));
    if (connection != null) {
        UserProfile userProfile = connection.fetchUserProfile();
        Customer customer = registerCustomerForm.getCustomer();
        customer.setFirstName(userProfile.getFirstName());
        customer.setLastName(userProfile.getLastName());
        customer.setEmailAddress(userProfile.getEmail());
        if (isUseEmailForLogin()){
            customer.setUsername(userProfile.getEmail());
        } else {
            customer.setUsername(userProfile.getUsername());
        }
    }

    return super.register(registerCustomerForm, request, response, model);
}
 
Example 9
Project: cloudstreetmarket.com   File: OAuth2CloudstreetController.java   View Source Code Vote up 6 votes
@RequestMapping(value="/signup", method = RequestMethod.GET)
public String getForm(NativeWebRequest request,  @ModelAttribute User user) {
	String view = successView;

	// check if this is a new user signing in via Spring Social
    Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
    if (connection != null) {
        // populate new User from social connection user profile
        UserProfile userProfile = connection.fetchUserProfile();
        user.setId(userProfile.getUsername());

        // finish social signup/login
        providerSignInUtils.doPostSignUp(user.getUsername(), request);

        // sign the user in and send them to the user home page
        signInAdapter.signIn(user.getUsername(), connection, request);
		view += "?spi="+ user.getUsername();
    }
    
    return view;
}
 
Example 10
Project: SparkCommerce   File: SparkSocialRegisterController.java   View Source Code Vote up 6 votes
public String register(RegisterCustomerForm registerCustomerForm, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
    Connection<?> connection = ProviderSignInUtils.getConnection(new ServletWebRequest(request));
    if (connection != null) {
        UserProfile userProfile = connection.fetchUserProfile();
        Customer customer = registerCustomerForm.getCustomer();
        customer.setFirstName(userProfile.getFirstName());
        customer.setLastName(userProfile.getLastName());
        customer.setEmailAddress(userProfile.getEmail());
        if (isUseEmailForLogin()){
            customer.setUsername(userProfile.getEmail());
        } else {
            customer.setUsername(userProfile.getUsername());
        }
    }

    return super.register(registerCustomerForm, request, response, model);
}
 
Example 11
Project: SparkCore   File: SparkSocialRegisterController.java   View Source Code Vote up 6 votes
public String register(RegisterCustomerForm registerCustomerForm, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
    Connection<?> connection = ProviderSignInUtils.getConnection(new ServletWebRequest(request));
    if (connection != null) {
        UserProfile userProfile = connection.fetchUserProfile();
        Customer customer = registerCustomerForm.getCustomer();
        customer.setFirstName(userProfile.getFirstName());
        customer.setLastName(userProfile.getLastName());
        customer.setEmailAddress(userProfile.getEmail());
        if (isUseEmailForLogin()){
            customer.setUsername(userProfile.getEmail());
        } else {
            customer.setUsername(userProfile.getUsername());
        }
    }

    return super.register(registerCustomerForm, request, response, model);
}
 
Example 12
Project: egd-web   File: SocialConnectionSignUp.java   View Source Code Vote up 6 votes
private User retreiveSocialAsUser(Connection<?> con) {
    if (con == null) throw new IllegalStateException("No connection to social api");

    // build a new User from the external provider's version of the User
    UserProfile profile = con.fetchUserProfile();
    String firstName = profile.getFirstName();
    String lastName = profile.getLastName();
    String email = profile.getEmail();

    // build the UserAccountExternal from the ConnectionKey
    String externalAccountProviderName = con.getKey().getProviderId();
    ExternalProvider externalProvider = ExternalProvider.caseInsensitiveValueOf(externalAccountProviderName);
    String externalUserId = con.getKey().getProviderUserId();

    // check that we got the information we needed
    if (StringUtils.isBlank(email))
        throw new ApiException(externalAccountProviderName, "provider failed to return email");

    User socialUser = new User(firstName, lastName, email, externalProvider, externalUserId);

    log.debug("Retrieved details from {} for user '{}'", externalAccountProviderName, externalUserId);
    return socialUser;
}
 
Example 13
Project: blcdemo   File: BroadleafSocialRegisterController.java   View Source Code Vote up 6 votes
public String register(RegisterCustomerForm registerCustomerForm, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
    Connection<?> connection = ProviderSignInUtils.getConnection(new ServletWebRequest(request));
    if (connection != null) {
        UserProfile userProfile = connection.fetchUserProfile();
        Customer customer = registerCustomerForm.getCustomer();
        customer.setFirstName(userProfile.getFirstName());
        customer.setLastName(userProfile.getLastName());
        customer.setEmailAddress(userProfile.getEmail());
        if (isUseEmailForLogin()){
            customer.setUsername(userProfile.getEmail());
        } else {
            customer.setUsername(userProfile.getUsername());
        }
    }

    return super.register(registerCustomerForm, request, response, model);
}
 
Example 14
Project: xm-uaa   File: SocialService.java   View Source Code Vote up 5 votes
/**
 * Create new user.
 * @param connection connection
 * @param langKey lang key
 */
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();
    String imageUrl = connection.getImageUrl();
    User user = createUserIfNotExist(userProfile, langKey, imageUrl);
    createSocialConnection(user.getUserKey(), connection);
    mailService.sendSocialRegistrationValidationEmail(user, userProfile.getEmail(), providerId,
        TenantContext.getCurrent().getTenant(), MDCUtil.getRid());
}
 
Example 15
Project: spring-security-oauth2-boot   File: SpringSocialTokenServices.java   View Source Code Vote up 5 votes
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
		throws AuthenticationException, InvalidTokenException {
	AccessGrant accessGrant = new AccessGrant(accessToken);
	Connection<?> connection = this.connectionFactory.createConnection(accessGrant);
	UserProfile user = connection.fetchUserProfile();
	return extractAuthentication(user);
}
 
Example 16
Project: MTC_Labrat   File: SocialService.java   View Source Code Vote up 5 votes
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();
    String imageUrl = connection.getImageUrl();
    User user = createUserIfNotExist(userProfile, langKey, providerId, imageUrl);
    createSocialConnection(user.getLogin(), connection);
    mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
 
Example 17
Project: theskeleton   File: RegistrationController.java   View Source Code Vote up 5 votes
@GetMapping
public String registrationView(RegistrationForm registrationForm, WebRequest request) {
	Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
	if (connection != null) {
		UserProfile profile = connection.fetchUserProfile();
		registrationForm.setUsername(profile.getUsername());
		registrationForm.setEmail(profile.getEmail());
	}
	return REGISTRATION;
}
 
Example 18
Project: sentry   File: SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 19
Project: Code4Health-Platform   File: SocialService.java   View Source Code Vote up 5 votes
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();
    String imageUrl = connection.getImageUrl();
    User user = createUserIfNotExist(userProfile, langKey, providerId, imageUrl);
    createSocialConnection(user.getLogin(), connection);
    mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
 
Example 20
Project: spring-io   File: SocialService.java   View Source Code Vote up 5 votes
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();
    String imageUrl = connection.getImageUrl();
    User user = createUserIfNotExist(userProfile, langKey, providerId, imageUrl);
    createSocialConnection(user.getLogin(), connection);
    mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
 
Example 21
Project: Spring-Security-Third-Edition   File: SocialAuthenticationUtils.java   View Source Code Vote up 5 votes
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){

        // TODO: There is a defect with Facebook:
        // org.springframework.social.UncategorizedApiException: (#12) bio field is
        // deprecated for versions v2.8 and higher:
//
//        Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
//        Facebook facebook = connection.getApi();
//        String [] fields = { "id", "email",  "first_name", "last_name" };
//        User userProfile = facebook.fetchObject("me", User.class, fields);
//
//        Object api = connection.getApi();
//        if(api instanceof FacebookTemplate){
//            System.out.println("Facebook");
//        }


        // Does not work with spring-social-facebook:2.0.3.RELEASE
        UserProfile profile = connection.fetchUserProfile();

        CalendarUser user = new CalendarUser();

        if(profile.getEmail() != null){
            user.setEmail(profile.getEmail());
        }
        else if(profile.getUsername() != null){
            user.setEmail(profile.getUsername());
        }
        else {
            user.setEmail(connection.getDisplayName());
        }

        user.setFirstName(profile.getFirstName());
        user.setLastName(profile.getLastName());

        user.setPassword(randomAlphabetic(32));

        return user;

    }
 
Example 22
Project: blogAggr   File: SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 23
Project: transandalus-backend   File: SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 24
Project: sentry   File: SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 25
Project: jhipster-ribbon-hystrix   File: _SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 26
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: SpringSocialTokenServices.java   View Source Code Vote up 5 votes
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
		throws AuthenticationException, InvalidTokenException {
	AccessGrant accessGrant = new AccessGrant(accessToken);
	Connection<?> connection = this.connectionFactory.createConnection(accessGrant);
	UserProfile user = connection.fetchUserProfile();
	return extractAuthentication(user);
}
 
Example 27
Project: Thesis-JHipster   File: _SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 28
Project: pazuzu-registry   File: ApplicationConnectionSignUp.java   View Source Code Vote up 5 votes
@Override
public String execute(Connection<?> connection) {
    UserProfile profile = connection.fetchUserProfile();
    String username = profile.getUsername();

    logger.info("Creating user with id: {}", username);
    User user = new User(username, "", Collections.emptyList());

    userDetailsManager.createUser(user);
    return username;
}
 
Example 29
Project: readthisstuff.com   File: SocialService.java   View Source Code Vote up 5 votes
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);
}
 
Example 30
Project: spring-boot-concourse   File: SpringSocialTokenServices.java   View Source Code Vote up 5 votes
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
		throws AuthenticationException, InvalidTokenException {
	AccessGrant accessGrant = new AccessGrant(accessToken);
	Connection<?> connection = this.connectionFactory.createConnection(accessGrant);
	UserProfile user = connection.fetchUserProfile();
	return extractAuthentication(user);
}
 
Example 31
Project: awesome-agile   File: AwesomeAgileConnectionSignup.java   View Source Code Vote up 5 votes
private User createUserFromProfile(Connection<?> connection) {
  UserProfile profile = connection.fetchUserProfile();
  ConnectionKey key = connection.getKey();
  return new User()
      .setPrimaryEmail(profile.getEmail())
      .setDisplayName(connection.getDisplayName())
      .setIsVisible(true)
      .setStatus(UserStatus.ACTIVE)
      .setAvatar(connection.getImageUrl())
      .setAuthProviderId(key.getProviderId())
      .setAuthProviderUserId(key.getProviderUserId());
}
 
Example 32
Project: contestparser   File: SpringSocialTokenServices.java   View Source Code Vote up 5 votes
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
		throws AuthenticationException, InvalidTokenException {
	AccessGrant accessGrant = new AccessGrant(accessToken);
	Connection<?> connection = this.connectionFactory.createConnection(accessGrant);
	UserProfile user = connection.fetchUserProfile();
	return extractAuthentication(user);
}
 
Example 33
Project: simple-facebook-integration-spring-sec   File: CustomConnectInterceptor.java   View Source Code Vote up 5 votes
@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);	
}
 
Example 34
Project: p2p-webtv   File: SocialService.java   View Source Code Vote up 5 votes
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();
  String imageUrl = connection.getImageUrl();
  User user = createUserIfNotExist(userProfile, langKey, providerId, imageUrl);
  createSocialConnection(user.getLogin(), connection);
  mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
 
Example 35
Project: sentry   File: ImplicitConnectionSignUp.java   View Source Code Vote up 4 votes
@Override
public String execute(Connection<?> connection) {
    UserProfile userProfile = connection.fetchUserProfile();

    String email = userProfile.getEmail();
    String login = userProfile.getId();
    String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10));

    Set<Authority> authorities;
    if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving administrative role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(5);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.ADMIN));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.ACTUATOR));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.MANAGER));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving manager role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(3);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.MANAGER));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving support role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(2);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else {
        authorities = new HashSet<>(1);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.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("en");

    userRepository.save(newUser);

    log.debug("Performing implicit sign up with user : {}", connection.getKey().getProviderUserId());
    return connection.getKey().getProviderUserId();
}
 
Example 36
Project: Spring-Security-Third-Edition   File: SocialAuthenticationUtils.java   View Source Code Vote up 4 votes
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){

        // TODO: There is a defect with Facebook:
//        Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
//        Facebook facebook = connection.getApi();
//        String [] fields = { "id", "email",  "first_name", "last_name" };
//        User userProfile = facebook.fetchObject("me", User.class, fields);

//        Object api = connection.getApi();
//        if(connection instanceof FacebookTemplate){
//            System.out.println("HERE");
//        }
        /*
            <form name='facebookSocialloginForm'
                  action="<c:url value='/auth/facebook' />" method='POST'>
                <input type="hidden" name="scope"
                    value="public_profile,email,user_about_me,user_birthday,user_likes"/>
                ...
            </form>
         */

        // FIXME: Does not work with Facebook:
        UserProfile profile = connection.fetchUserProfile();

        CalendarUser user = new CalendarUser();

        if(profile.getEmail() != null){
            user.setEmail(profile.getEmail());
        }
        else if(profile.getUsername() != null){
            user.setEmail(profile.getUsername());
        }
        else {
            user.setEmail(connection.getDisplayName());
        }

        user.setFirstName(profile.getFirstName());
        user.setLastName(profile.getLastName());

        user.setPassword(randomAlphabetic(32));

        return user;

    }
 
Example 37
Project: sentry   File: ImplicitConnectionSignUp.java   View Source Code Vote up 4 votes
@Override
public String execute(Connection<?> connection) {
    UserProfile userProfile = connection.fetchUserProfile();

    String email = userProfile.getEmail();
    String login = userProfile.getId();
    String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10));

    Set<Authority> authorities;
    if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving administrative role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(5);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.ADMIN));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.ACTUATOR));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.MANAGER));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving manager role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(3);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.MANAGER));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else if (sentryProperties.getDiscord().getAdministrators().contains(login)) {
        log.info("Giving support role to {} ({})", userProfile.getUsername(), userProfile.getId());
        authorities = new HashSet<>(2);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.SUPPORT));
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    } else {
        authorities = new HashSet<>(1);
        authorities.add(authorityRepository.findOne(AuthoritiesConstants.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("en");

    userRepository.save(newUser);

    log.debug("Performing implicit sign up with user : {}", connection.getKey().getProviderUserId());
    return connection.getKey().getProviderUserId();
}