argos/dmp-backend/src/main/java/eu/eudat/services/AuthenticationService.java

68 lines
2.1 KiB
Java
Raw Normal View History

2017-12-15 17:57:41 +01:00
package eu.eudat.services;
import eu.eudat.dao.entities.UserInfoDao;
import eu.eudat.dao.entities.security.UserTokenDao;
import eu.eudat.entities.UserInfo;
import eu.eudat.entities.UserToken;
import eu.eudat.models.security.Principal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.ws.ServiceMode;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* Created by ikalyvas on 12/15/2017.
*/
2017-12-18 16:55:12 +01:00
@Service("authenticationService ")
2017-12-15 17:57:41 +01:00
public class AuthenticationService {
@Autowired
UserTokenDao userTokenDao;
@Autowired
UserInfoDao userInfoDao;
public Principal Touch(UUID token)
{
UserToken tokenEntry = userTokenDao.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 = userTokenDao.read(token);
userTokenDao.delete(tokenEntry);
}
private Principal Touch(UserToken token)
{
if (token == null || token.getExpiresAt().before(new Date())) return null;
2018-01-05 08:47:52 +01:00
UserInfo user = this.userInfoDao.find(token.getUser().getId());
2017-12-15 17:57:41 +01:00
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;
}
}