129 lines
4.4 KiB
Java
129 lines
4.4 KiB
Java
package eu.eudat.security.validators;
|
|
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.security.GeneralSecurityException;
|
|
import java.security.Principal;
|
|
import java.util.*;
|
|
|
|
import com.google.api.client.googleapis.auth.oauth2.*;
|
|
import eu.eudat.dao.entities.security.CredentialDao;
|
|
import eu.eudat.dao.entities.security.UserTokenDao;
|
|
import eu.eudat.entities.Credential;
|
|
import eu.eudat.entities.UserToken;
|
|
import eu.eudat.models.login.LoginInfo;
|
|
import eu.eudat.services.AuthenticationService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
|
|
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.dao.entities.UserInfoDao;
|
|
import eu.eudat.entities.UserInfo;
|
|
import eu.eudat.exceptions.NonValidTokenException;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import static com.sun.org.apache.xalan.internal.xsltc.compiler.Constants.REDIRECT_URI;
|
|
|
|
@Component
|
|
public class GoogleTokenValidator implements TokenValidator {
|
|
|
|
private static final JacksonFactory jacksonFactory = new JacksonFactory();
|
|
private static final HttpTransport transport = new NetHttpTransport();
|
|
|
|
@Autowired private UserInfoDao userInfoDao;
|
|
@Autowired private CredentialDao credentialDao;
|
|
@Autowired private AuthenticationService authenticationService;
|
|
@Autowired private UserTokenDao userTokenDao;
|
|
|
|
private GoogleTokenResponse verifyUser(String authCode) throws IOException {
|
|
|
|
GoogleTokenResponse tokenResponse =
|
|
new GoogleAuthorizationCodeTokenRequest(
|
|
new NetHttpTransport(),
|
|
JacksonFactory.getDefaultInstance(),
|
|
"https://www.googleapis.com/oauth2/v4/token",
|
|
"524432312250-sc9qsmtmbvlv05r44onl6l93ia3k9deo.apps.googleusercontent.com",
|
|
"IQjhbYdExPIsjy37mOfB103F",
|
|
authCode,
|
|
"postmessage") // Specify the same redirect URI that you use with your web
|
|
// app. If you don't have a web version of your app, you can
|
|
// specify an empty string.
|
|
.execute();
|
|
return tokenResponse;
|
|
}
|
|
|
|
private String getAccessToken(GoogleTokenResponse response){
|
|
return response.getAccessToken();
|
|
}
|
|
|
|
private GoogleIdToken getUser(GoogleTokenResponse response) throws IOException {
|
|
return response.parseIdToken();
|
|
}
|
|
@Override
|
|
public eu.eudat.models.security.Principal validateToken(String authCode) throws NonValidTokenException, IOException {
|
|
|
|
GoogleTokenResponse tokenResponse = this.verifyUser(authCode);
|
|
String token = getAccessToken(tokenResponse);
|
|
GoogleIdToken idToken = getUser(tokenResponse);
|
|
|
|
Payload payload = idToken.getPayload();
|
|
|
|
UserInfo userInfo = userInfoDao.getByMail(payload.getEmail());
|
|
|
|
Credential credential = new Credential();
|
|
credential.setCreationTime(new Date());
|
|
credential.setId(UUID.randomUUID());
|
|
credential.setStatus(1);
|
|
credential.setLastUpdateTime(new Date());
|
|
credential.setProvider(1);
|
|
credential.setSecret(token);
|
|
credential.setPublicValue(userInfo.getName());
|
|
credential.setUserInfo(userInfo);
|
|
credentialDao.create(credential);
|
|
if(userInfo == null) { //means not existing in db, so create one
|
|
userInfo = new UserInfo();
|
|
userInfo.setName((String)payload.get("name"));
|
|
userInfo.setVerified_email(payload.getEmailVerified());
|
|
userInfo.setEmail(payload.getEmail());
|
|
userInfo.setCreated(new Date());
|
|
userInfo.setLastloggedin(new Date());
|
|
userInfo.setAuthorization_level(new Short("1"));
|
|
userInfo.setUsertype(new Short("1"));
|
|
userInfo = userInfoDao.create(userInfo);
|
|
credential = credentialDao.create(credential);
|
|
}
|
|
else {
|
|
userInfo.setLastloggedin(new Date());
|
|
Set<Credential> credentials = userInfo.getCredentials();
|
|
if(credentials.contains(credential))credentials.remove(credential);
|
|
credentials.add(credential);
|
|
userInfo = userInfoDao.update(userInfo);
|
|
}
|
|
|
|
UserToken userToken = new UserToken();
|
|
userToken.setUser(userInfo);
|
|
userToken.setIssuedAt(new Date());
|
|
userToken.setToken(UUID.randomUUID());
|
|
userToken.setExpiresAt(addADay(new Date()));
|
|
userTokenDao.create(userToken);
|
|
return authenticationService.Touch(userToken.getToken());
|
|
|
|
}
|
|
|
|
private Date addADay(Date date){
|
|
Date dt = new Date();
|
|
Calendar c = Calendar.getInstance();
|
|
c.setTime(dt);
|
|
c.add(Calendar.DATE, 1);
|
|
dt = c.getTime();
|
|
return dt;
|
|
}
|
|
|
|
|
|
|
|
}
|