134 lines
5.3 KiB
Java
134 lines
5.3 KiB
Java
package eu.eudat.services;
|
|
|
|
import eu.eudat.dao.entities.UserInfoDao;
|
|
import eu.eudat.dao.entities.security.UserTokenDao;
|
|
import eu.eudat.entities.Credential;
|
|
import eu.eudat.entities.UserInfo;
|
|
import eu.eudat.entities.UserToken;
|
|
import eu.eudat.models.criteria.UserInfoCriteria;
|
|
import eu.eudat.models.loginprovider.LoginProviderUser;
|
|
import eu.eudat.models.security.Principal;
|
|
import eu.eudat.security.validators.TokenValidatorFactoryImpl;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.xml.ws.ServiceMode;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Created by ikalyvas on 12/15/2017.
|
|
*/
|
|
@Service("authenticationService ")
|
|
public class AuthenticationService {
|
|
private ApiContext apiContext;
|
|
|
|
@Autowired
|
|
public AuthenticationService(ApiContext apiContext) {
|
|
this.apiContext = apiContext;
|
|
}
|
|
|
|
public Principal Touch(UUID token)
|
|
{
|
|
UserToken tokenEntry = this.apiContext.getDatabaseRepository().getUserTokenDao().read(token);
|
|
if (tokenEntry == null || tokenEntry.getExpiresAt().before(new Date())) return null;
|
|
|
|
Principal principal = this.Touch(tokenEntry);
|
|
|
|
return principal;
|
|
}
|
|
|
|
public void Logout(UUID token)
|
|
{
|
|
UserToken tokenEntry = this.apiContext.getDatabaseRepository().getUserTokenDao().read(token);
|
|
this.apiContext.getDatabaseRepository().getUserTokenDao().delete(tokenEntry);
|
|
}
|
|
|
|
private Principal Touch(UserToken token)
|
|
{
|
|
if (token == null || token.getExpiresAt().before(new Date())) return null;
|
|
|
|
UserInfo user = this.apiContext.getDatabaseRepository().getUserInfoDao().find(token.getUser().getId());
|
|
if (user == null /*|| user.Status != ActivityStatus.Active*/) return null;
|
|
|
|
//List<UserRole> appRoles = this._unitOfWork.UserRoles.GetAll().Where(x => x.UserId == token.UserId /*&& x.Status == ActivityStatus.Active*/).ToList();
|
|
|
|
Principal principal = new Principal();
|
|
principal.setId(user.getId());
|
|
principal.setToken(token.getToken());
|
|
principal.setExpiresAt(token.getExpiresAt());
|
|
principal.setName(user.getName());
|
|
|
|
/*foreach (UserRole item in appRoles)
|
|
{
|
|
if (principal.AppRoles == null) principal.AppRoles = new HashSet<AppRole>();
|
|
principal.AppRoles.Add(item.Role);
|
|
}
|
|
|
|
if (this._config.Refresh) token.ExpiresAt = DateTime.UtcNow.AddMinutes(this._config.Lifetime);
|
|
*/
|
|
return principal;
|
|
}
|
|
|
|
public Principal Touch(LoginProviderUser profile){
|
|
UserInfoCriteria criteria = new UserInfoCriteria();
|
|
criteria.setEmail(profile.getEmail());
|
|
List<UserInfo> users = apiContext.getDatabaseRepository().getUserInfoDao().getWithCriteria(criteria).toList();
|
|
UserInfo userInfo = null;
|
|
if(users.size()>0)userInfo = users.get(0);
|
|
final Credential credential = new Credential();
|
|
credential.setId(UUID.randomUUID());
|
|
credential.setCreationTime(new Date());
|
|
credential.setStatus(1);
|
|
credential.setLastUpdateTime(new Date());
|
|
credential.setProvider((int) TokenValidatorFactoryImpl.LoginProvider.FACEBOOK.getValue());
|
|
credential.setSecret(profile.getSecret());
|
|
if(userInfo == null) {
|
|
userInfo = new UserInfo();
|
|
userInfo.setName((String)profile.getName());
|
|
userInfo.setVerified_email(profile.getIsVerified());
|
|
userInfo.setEmail(profile.getEmail());
|
|
userInfo.setCreated(new Date());
|
|
userInfo.setLastloggedin(new Date());
|
|
userInfo.setAuthorization_level(new Short("1"));
|
|
userInfo.setUsertype(new Short("1"));
|
|
userInfo = apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
|
|
credential.setPublicValue(userInfo.getName());
|
|
credential.setUserInfo(userInfo);
|
|
apiContext.getDatabaseRepository().getCredentialDao().createOrUpdate(credential);
|
|
}
|
|
else {
|
|
userInfo.setLastloggedin(new Date());
|
|
Set<Credential> credentials = userInfo.getCredentials();
|
|
if(credentials.contains(credential)){
|
|
Credential oldCredential = credentials.stream().filter(item->credential.getProvider().equals(item.getProvider())).findFirst().get();
|
|
credential.setId(oldCredential.getId());
|
|
}
|
|
else{
|
|
credential.setUserInfo(userInfo);
|
|
credential.setId(UUID.randomUUID());
|
|
credential.setPublicValue(userInfo.getName());
|
|
apiContext.getDatabaseRepository().getCredentialDao().createOrUpdate(credential);
|
|
userInfo.getCredentials().add(credential);
|
|
}
|
|
userInfo = apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
|
|
}
|
|
|
|
UserToken userToken = new UserToken();
|
|
userToken.setUser(userInfo);
|
|
userToken.setIssuedAt(new Date());
|
|
userToken.setToken(UUID.randomUUID());
|
|
userToken.setExpiresAt(addADay(new Date()));
|
|
apiContext.getDatabaseRepository().getUserTokenDao().create(userToken);
|
|
return 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;
|
|
}
|
|
}
|