Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google sing in upgrade #5187

Merged
merged 5 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions orcid-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.4.RELEASE</version>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>1.1.4.RELEASE</version>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>3.0.0.GRAPH-v2.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-google</artifactId>
<version>1.0.0.RELEASE</version>
<groupId>com.github.spring-social</groupId>
<artifactId>spring-social-google</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.http.HttpServletResponse;

import org.orcid.core.manager.UserConnectionManager;
import org.orcid.frontend.spring.web.social.GoogleSignIn;
import org.orcid.frontend.spring.web.social.config.SocialContext;
import org.orcid.frontend.spring.web.social.config.SocialType;
import org.orcid.persistence.jpa.entities.UserconnectionEntity;
Expand All @@ -18,6 +19,7 @@
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.User;
import org.springframework.social.google.api.Google;
import org.springframework.social.google.api.oauth2.UserInfo;
import org.springframework.social.google.api.plus.Person;

public class SocialAjaxAuthenticationSuccessHandler extends AjaxAuthenticationSuccessHandlerBase {
Expand Down Expand Up @@ -65,11 +67,13 @@ private Map<String, String> retrieveUserDetails(SocialType connectionType) {
userMap.put("userName", user.getName());
userMap.put("email", user.getEmail());
} else if (SocialType.GOOGLE.equals(connectionType)) {
Google google = socialContext.getGoogle();
Person person = google.plusOperations().getGoogleProfile();
userMap.put("providerUserId", person.getId());
userMap.put("userName", person.getDisplayName());
userMap.put("email", person.getAccountEmail());
GoogleSignIn google = socialContext.getGoogle();
UserInfo userInfo = google.getUserInfo();
userMap.put("providerUserId", userInfo.getId());
userMap.put("userName", userInfo.getName());
userMap.put("email", userInfo.getEmail());
userMap.put("firstName", userInfo.getGivenName());
userMap.put("lastName", userInfo.getFamilyName());
}

return userMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.orcid.frontend.spring.web.social;

import org.springframework.social.google.api.Google;
import org.springframework.social.google.api.oauth2.UserInfo;

public interface GoogleSignIn extends Google {

UserInfo getUserInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.orcid.frontend.spring.web.social;

import org.springframework.social.google.api.impl.GoogleTemplate;
import org.springframework.social.google.api.oauth2.OAuth2Operations;
import org.springframework.social.google.api.oauth2.UserInfo;

public class GoogleSignInImpl extends GoogleTemplate implements GoogleSignIn {

public GoogleSignInImpl() {
super();
}

public GoogleSignInImpl(String accessToken) {
super(accessToken);
}

@Override
public UserInfo getUserInfo() {
OAuth2Operations op = oauth2Operations();
return op.getUserinfo();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.annotation.Resource;
import javax.sql.DataSource;

import org.orcid.frontend.spring.web.social.GoogleSignIn;
import org.orcid.frontend.spring.web.social.GoogleSignInImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
Expand All @@ -22,7 +24,6 @@
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.google.api.Google;
import org.springframework.social.google.api.impl.GoogleTemplate;
import org.springframework.social.google.connect.GoogleConnectionFactory;

/**
Expand All @@ -31,84 +32,82 @@
@Configuration
public class SocialConfig implements InitializingBean {

private static final Logger logger = LoggerFactory.getLogger(SocialConfig.class);

@Value("${org.orcid.social.fb.key}")
private String fb_key;
@Value("${org.orcid.social.fb.secret}")
private String fb_secret;
@Value("${org.orcid.social.gg.key}")
private String gg_key;
@Value("${org.orcid.social.gg.secret}")
private String gg_secret;

private SocialContext socialContext;

private UsersConnectionRepository usersConnectionRepositiory;

@Resource
private DataSource simpleDataSource;

@Value("${org.orcid.core.baseUri}")
private String appUrl;

@Bean
public SocialContext socialContext() {

return socialContext;
}

@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
logger.info("getting connectionFactoryLocator");
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(fb_key, fb_secret));
registry.addConnectionFactory(new GoogleConnectionFactory(gg_key, gg_secret));
return registry;
}

@Bean
public UsersConnectionRepository usersConnectionRepository() {
return usersConnectionRepositiory;
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
String userId = socialContext.getUserId();
return usersConnectionRepository().createConnectionRepository(userId);
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook() {
String accessToken = connectionRepository().getPrimaryConnection(Facebook.class).createData().getAccessToken();
return new FacebookTemplate(accessToken);
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Google google() {
String accessToken = connectionRepository().getPrimaryConnection(Google.class).createData().getAccessToken();
return new GoogleTemplate(accessToken);
}

@Bean
public ProviderSignInController providerSignInController() {
ProviderSignInController providerSigninController = new ProviderSignInController(connectionFactoryLocator(),
usersConnectionRepository(), socialContext);
providerSigninController.setPostSignInUrl(appUrl + "/social/access");
providerSigninController.setApplicationUrl(appUrl);
return providerSigninController;
}

@Override
public void afterPropertiesSet() throws Exception {

JdbcUsersConnectionRepository usersConnectionRepositiory = new JdbcUsersConnectionRepository(simpleDataSource,
connectionFactoryLocator(), Encryptors.noOpText());
socialContext = new SocialContext(usersConnectionRepositiory, new UserCookieGenerator(), facebook(), google());
usersConnectionRepositiory.setConnectionSignUp(socialContext);
this.usersConnectionRepositiory = usersConnectionRepositiory;
}
private static final Logger logger = LoggerFactory.getLogger(SocialConfig.class);

@Value("${org.orcid.social.fb.key}")
private String fb_key;
@Value("${org.orcid.social.fb.secret}")
private String fb_secret;
@Value("${org.orcid.social.gg.key}")
private String gg_key;
@Value("${org.orcid.social.gg.secret}")
private String gg_secret;

private SocialContext socialContext;

private UsersConnectionRepository usersConnectionRepositiory;

@Resource
private DataSource simpleDataSource;

@Value("${org.orcid.core.baseUri}")
private String appUrl;

@Bean
public SocialContext socialContext() {

return socialContext;
}

@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
logger.info("getting connectionFactoryLocator");
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(fb_key, fb_secret));
registry.addConnectionFactory(new GoogleConnectionFactory(gg_key, gg_secret));
return registry;
}

@Bean
public UsersConnectionRepository usersConnectionRepository() {
return usersConnectionRepositiory;
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
String userId = socialContext.getUserId();
return usersConnectionRepository().createConnectionRepository(userId);
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook() {
String accessToken = connectionRepository().getPrimaryConnection(Facebook.class).createData().getAccessToken();
return new FacebookTemplate(accessToken);
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public GoogleSignIn google() {
String accessToken = connectionRepository().getPrimaryConnection(Google.class).createData().getAccessToken();
return new GoogleSignInImpl(accessToken);
}

@Bean
public ProviderSignInController providerSignInController() {
ProviderSignInController providerSigninController = new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), socialContext);
providerSigninController.setPostSignInUrl(appUrl + "/social/access");
providerSigninController.setApplicationUrl(appUrl);
return providerSigninController;
}

@Override
public void afterPropertiesSet() throws Exception {

JdbcUsersConnectionRepository usersConnectionRepositiory = new JdbcUsersConnectionRepository(simpleDataSource, connectionFactoryLocator(), Encryptors.noOpText());
socialContext = new SocialContext(usersConnectionRepositiory, new UserCookieGenerator(), facebook(), google());
usersConnectionRepositiory.setConnectionSignUp(socialContext);
this.usersConnectionRepositiory = usersConnectionRepositiory;
}
}
Loading