2018-01-10 17:05:23 +01:00
|
|
|
package eu.eudat.security.validators.facebook;
|
|
|
|
|
2018-02-07 10:56:30 +01:00
|
|
|
import eu.eudat.exceptions.security.NonValidTokenException;
|
|
|
|
import eu.eudat.exceptions.security.UnauthorisedException;
|
2018-01-11 12:13:01 +01:00
|
|
|
import eu.eudat.models.login.LoginInfo;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.models.loginprovider.LoginProviderUser;
|
|
|
|
import eu.eudat.models.security.Principal;
|
|
|
|
import eu.eudat.security.validators.TokenValidator;
|
|
|
|
import eu.eudat.security.validators.TokenValidatorFactoryImpl;
|
|
|
|
import eu.eudat.services.ApiContext;
|
2018-03-05 17:18:45 +01:00
|
|
|
import eu.eudat.services.operations.AuthenticationServiceImpl;
|
2018-01-10 17:05:23 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.social.facebook.api.User;
|
|
|
|
import org.springframework.social.facebook.connect.FacebookServiceProvider;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.GeneralSecurityException;
|
2018-02-16 11:34:02 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2018-01-10 17:05:23 +01:00
|
|
|
|
2018-02-01 10:08:06 +01:00
|
|
|
|
2018-01-10 17:05:23 +01:00
|
|
|
@Component("facebookTokenValidator")
|
|
|
|
public class FacebookTokenValidator implements TokenValidator {
|
|
|
|
|
|
|
|
private Environment environment;
|
|
|
|
private ApiContext apiContext;
|
2018-03-05 17:18:45 +01:00
|
|
|
private AuthenticationServiceImpl authenticationServiceImpl;
|
2018-01-10 17:05:23 +01:00
|
|
|
private FacebookServiceProvider facebookServiceProvider;
|
|
|
|
|
|
|
|
@Autowired
|
2018-03-05 17:18:45 +01:00
|
|
|
public FacebookTokenValidator(Environment environment, ApiContext apiContext, AuthenticationServiceImpl authenticationServiceImpl) {
|
2018-01-10 17:05:23 +01:00
|
|
|
this.environment = environment;
|
2018-02-16 11:34:02 +01:00
|
|
|
this.apiContext = apiContext;
|
2018-03-05 17:18:45 +01:00
|
|
|
this.authenticationServiceImpl = authenticationServiceImpl;
|
2018-02-16 11:34:02 +01:00
|
|
|
this.facebookServiceProvider = new FacebookServiceProvider(this.environment.getProperty("facebook.login.clientId"), this.environment.getProperty("facebook.login.clientSecret"), this.environment.getProperty("facebook.login.namespace"));
|
2018-01-10 17:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-11 12:13:01 +01:00
|
|
|
public Principal validateToken(LoginInfo credentials) throws NonValidTokenException, IOException, GeneralSecurityException {
|
|
|
|
User profile = getFacebookUser(credentials.getTicket());
|
2018-01-10 17:05:23 +01:00
|
|
|
LoginProviderUser user = new LoginProviderUser();
|
2018-02-16 11:34:02 +01:00
|
|
|
if (profile.getEmail() == null)
|
|
|
|
throw new UnauthorisedException("Cannot login user.Facebook account did not provide email");
|
2018-01-10 17:05:23 +01:00
|
|
|
user.setEmail(profile.getEmail());
|
2018-02-23 11:36:51 +01:00
|
|
|
user.setId(profile.getId());
|
2018-01-10 17:05:23 +01:00
|
|
|
user.setIsVerified(profile.isVerified());
|
|
|
|
user.setName(profile.getName());
|
|
|
|
user.setProvider(TokenValidatorFactoryImpl.LoginProvider.FACEBOOK);
|
2018-01-11 12:13:01 +01:00
|
|
|
user.setSecret(credentials.getTicket());
|
2018-03-05 17:18:45 +01:00
|
|
|
return this.authenticationServiceImpl.Touch(user);
|
2018-01-10 17:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private User getFacebookUser(String accessToken) {
|
2018-02-16 11:34:02 +01:00
|
|
|
String[] fields = {"id", "email", "first_name", "last_name", "name", "verified"};
|
|
|
|
User profile = this.facebookServiceProvider.getApi(accessToken).fetchObject("me", User.class, fields);
|
2018-01-10 17:05:23 +01:00
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
private Date addADay(Date date) {
|
2018-01-10 17:05:23 +01:00
|
|
|
Date dt = new Date();
|
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
c.setTime(dt);
|
|
|
|
c.add(Calendar.DATE, 1);
|
|
|
|
dt = c.getTime();
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
}
|