org.springframework.social.connect.UserProfile Java Examples

The following examples show how to use org.springframework.social.connect.UserProfile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserAccountServiceImpl.java    From JiwhizBlogWeb with Apache License 2.0 6 votes vote down vote up
@Override
public UserAccount createUserAccount(ConnectionData data, UserProfile profile) {
    long userIdSequence = this.counterService.getNextUserIdSequence();
    
    UserRoleType[] roles = (userIdSequence == 1l) ? 
            new UserRoleType[] { UserRoleType.ROLE_USER, UserRoleType.ROLE_AUTHOR, UserRoleType.ROLE_ADMIN } :
            new UserRoleType[] { UserRoleType.ROLE_USER };
    UserAccount account = new UserAccount(USER_ID_PREFIX + userIdSequence, roles);
    account.setEmail(profile.getEmail());
    account.setDisplayName(data.getDisplayName());
    account.setImageUrl(data.getImageUrl());
    if (userIdSequence == 1l) {
        account.setTrustedAccount(true);
    }
    LOGGER.info(String.format("A new user is created (userId='%s') for '%s' with email '%s'.", account.getUserId(),
            account.getDisplayName(), account.getEmail()));
    return this.accountRepository.save(account);
}
 
Example #2
Source File: OAuth2CloudstreetController.java    From cloudstreetmarket.com with GNU General Public License v3.0 6 votes vote down vote up
@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 #3
Source File: SocialAuthenticationUtils.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
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
Source File: GooglePlusAdapter.java    From google-plus-java-api with Apache License 2.0 5 votes vote down vote up
public UserProfile fetchUserProfile(Plus api) {
    Person person = api.getPeopleOperations().get("me");
    UserProfileBuilder builder = new UserProfileBuilder().setFirstName(person.getName().getGivenName()).setLastName(person.getName().getFamilyName());
    builder.setEmail(person.getGoogleAccountEmail());
    builder.setName(person.getName().getFormatted());
    return builder.build();
}
 
Example #5
Source File: YahooOAuth2Adapter.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Yahoo2 yahoo) {
	TinyUsercard tinyUserCard = yahoo.profilesOperations().getTinyUsercard();
	return new YahooUserProfile(
			tinyUserCard.getProfile().getNickname(), 
			tinyUserCard.getProfile().getGivenName(), 
			tinyUserCard.getProfile().getFamilyName(), 
			null, 
			tinyUserCard.getProfile().getGuid());
}
 
Example #6
Source File: ApplicationConnectionSignUp.java    From pazuzu-registry with MIT License 5 votes vote down vote up
@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 #7
Source File: _SocialService.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @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().toLowerCase();
        default:
            return userProfile.getEmail();
    }
}
 
Example #8
Source File: _SocialService.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: _SocialService.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
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 #10
Source File: SocialAuthenticationUtils.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
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 #11
Source File: SpringSocialTokenServices.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@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 #12
Source File: SpringSocialTokenServices.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #13
Source File: QQAdapter.java    From cola with MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(QQ api) {
	return null;
}
 
Example #14
Source File: SocialAuthenticationUtils.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
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 #15
Source File: WeiXinAdapter.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(WeiXin api) {
    return null;
}
 
Example #16
Source File: QQAdapter.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(QQ api) {
    return null;
}
 
Example #17
Source File: WeixinAdapter.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Weixin api) {
    return null;
}
 
Example #18
Source File: GiteeAdapter.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Gitee api) {
    return null;
}
 
Example #19
Source File: GitHubAdapter.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(GitHub api) {
    return null;
}
 
Example #20
Source File: AlipayAdapater.java    From cola with MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Alipay api) {
	return null;
}
 
Example #21
Source File: WechatMpAdapter.java    From cola with MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(WechatMp api) {
    return null;
}
 
Example #22
Source File: WechatAdapter.java    From cola with MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Wechat api) {
    return null;
}
 
Example #23
Source File: WeixinAdapter.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * Fetch user profile user profile.
 *
 * @param api the api
 *
 * @return user profile
 */
@Override
public UserProfile fetchUserProfile(Weixin api) {
	return null;
}
 
Example #24
Source File: QQAdapter.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * Fetch user profile user profile.
 *
 * @param api the api
 *
 * @return the user profile
 */
@Override
public UserProfile fetchUserProfile(QQ api) {
	return null;
}
 
Example #25
Source File: QQAdapter.java    From pre with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 和上面的方法类似
 * @param qq
 * @return
 */
@Override
public UserProfile fetchUserProfile(QQ qq) {
    return null;
}
 
Example #26
Source File: UserAccountService.java    From JiwhizBlogWeb with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new UserAccount with user social network account Connection Data and UserProfile.
 * Default has ROLE_USER role.
 * 
 * @param data
 * @param profile
 * @return
 */
UserAccount createUserAccount(ConnectionData data, UserProfile profile);