Java Code Examples for org.springframework.social.connect.UserProfile

The following examples show how to use org.springframework.social.connect.UserProfile. These examples are extracted from open source projects. 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
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 2
@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 Project: JiwhizBlogWeb   Source File: UserAccountServiceImpl.java    License: 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 4
@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 5
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 6
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 7
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 8
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
/**
 * @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 10
Source Project: pazuzu-registry   Source File: ApplicationConnectionSignUp.java    License: 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 11
@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 12
Source Project: google-plus-java-api   Source File: GooglePlusAdapter.java    License: 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 13
Source Project: cola   Source File: WechatAdapter.java    License: MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Wechat api) {
    return null;
}
 
Example 14
Source Project: cola   Source File: QQAdapter.java    License: MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(QQ api) {
	return null;
}
 
Example 15
Source Project: cola   Source File: WechatMpAdapter.java    License: MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(WechatMp api) {
    return null;
}
 
Example 16
Source Project: cola   Source File: AlipayAdapater.java    License: MIT License 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Alipay api) {
	return null;
}
 
Example 17
Source Project: pre   Source File: GitHubAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(GitHub api) {
    return null;
}
 
Example 18
Source Project: pre   Source File: GiteeAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Gitee api) {
    return null;
}
 
Example 19
Source Project: pre   Source File: WeixinAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(Weixin api) {
    return null;
}
 
Example 20
Source Project: FEBS-Security   Source File: QQAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(QQ api) {
    return null;
}
 
Example 21
Source Project: FEBS-Security   Source File: WeiXinAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public UserProfile fetchUserProfile(WeiXin api) {
    return null;
}
 
Example 22
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 23
Source Project: pre   Source File: QQAdapter.java    License: GNU General Public License v3.0 2 votes vote down vote up
/**
 * 和上面的方法类似
 * @param qq
 * @return
 */
@Override
public UserProfile fetchUserProfile(QQ qq) {
    return null;
}
 
Example 24
Source Project: paascloud-master   Source File: QQAdapter.java    License: 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 Project: paascloud-master   Source File: WeixinAdapter.java    License: 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 26
Source Project: JiwhizBlogWeb   Source File: UserAccountService.java    License: 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);