2018-01-09 12:31:01 +01:00
|
|
|
package eu.eudat.security.validators.google;
|
2017-09-27 18:15:39 +02:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
|
|
|
|
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
|
|
|
|
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
|
|
|
|
import com.google.api.client.http.HttpTransport;
|
|
|
|
import com.google.api.client.http.javanet.NetHttpTransport;
|
|
|
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
|
|
|
import eu.eudat.exceptions.security.NonValidTokenException;
|
2017-12-18 16:55:12 +01:00
|
|
|
import eu.eudat.models.login.LoginInfo;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.models.loginprovider.LoginProviderUser;
|
2018-01-09 12:31:01 +01:00
|
|
|
import eu.eudat.security.validators.TokenValidator;
|
|
|
|
import eu.eudat.security.validators.TokenValidatorFactoryImpl;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.services.ApiContext;
|
2017-12-15 17:57:41 +01:00
|
|
|
import eu.eudat.services.AuthenticationService;
|
2017-10-20 12:03:55 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-01-10 17:05:23 +01:00
|
|
|
import org.springframework.core.env.Environment;
|
2017-12-15 00:01:26 +01:00
|
|
|
import org.springframework.stereotype.Component;
|
2017-12-18 16:55:12 +01:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.GeneralSecurityException;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2018-01-09 12:31:01 +01:00
|
|
|
@Component("googleTokenValidator")
|
2017-10-13 17:08:49 +02:00
|
|
|
public class GoogleTokenValidator implements TokenValidator {
|
2017-09-27 18:15:39 +02:00
|
|
|
|
2018-02-16 11:34:02 +01:00
|
|
|
private static final HttpTransport transport = new NetHttpTransport();
|
|
|
|
private ApiContext apiContext;
|
|
|
|
private AuthenticationService authenticationService;
|
|
|
|
private GoogleIdTokenVerifier verifier;
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public GoogleTokenValidator(ApiContext apiContext, Environment environment, AuthenticationService authenticationService) {
|
|
|
|
this.apiContext = apiContext;
|
|
|
|
this.environment = environment;
|
|
|
|
this.authenticationService = authenticationService;
|
|
|
|
verifier = new GoogleIdTokenVerifier.Builder(transport, JacksonFactory.getDefaultInstance())
|
|
|
|
.setAudience(Collections.singletonList(this.environment.getProperty("google.login.clientId")))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private GoogleIdToken verifyUserAndGetUser(String idTokenString) throws IOException, GeneralSecurityException {
|
|
|
|
GoogleIdToken idToken = verifier.verify(idTokenString);
|
|
|
|
return idToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public eu.eudat.models.security.Principal validateToken(LoginInfo credentials) throws NonValidTokenException, IOException, GeneralSecurityException {
|
|
|
|
GoogleIdToken idToken = this.verifyUserAndGetUser(credentials.getTicket());
|
|
|
|
Payload payload = idToken.getPayload();
|
|
|
|
LoginProviderUser user = new LoginProviderUser();
|
|
|
|
user.setSecret(credentials.getTicket());
|
2018-02-23 11:36:51 +01:00
|
|
|
user.setId( payload.getSubject());
|
2018-02-16 11:34:02 +01:00
|
|
|
user.setProvider(TokenValidatorFactoryImpl.LoginProvider.GOOGLE);
|
|
|
|
user.setName((String) payload.get("name"));
|
|
|
|
user.setEmail(payload.getEmail());
|
|
|
|
user.setIsVerified(payload.getEmailVerified());
|
|
|
|
return this.authenticationService.Touch(user);
|
|
|
|
}
|
2018-01-10 17:05:23 +01:00
|
|
|
|
2017-09-27 18:15:39 +02:00
|
|
|
}
|