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;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.entities.Credential;
|
2017-12-15 17:57:41 +01:00
|
|
|
import eu.eudat.entities.UserInfo;
|
2018-01-31 16:39:16 +01:00
|
|
|
import eu.eudat.entities.UserRole;
|
2017-12-15 17:57:41 +01:00
|
|
|
import eu.eudat.entities.UserToken;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.models.criteria.UserInfoCriteria;
|
2018-01-31 16:39:16 +01:00
|
|
|
import eu.eudat.models.login.Credentials;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.models.loginprovider.LoginProviderUser;
|
2017-12-15 17:57:41 +01:00
|
|
|
import eu.eudat.models.security.Principal;
|
2018-01-10 17:05:23 +01:00
|
|
|
import eu.eudat.security.validators.TokenValidatorFactoryImpl;
|
2018-01-31 16:39:16 +01:00
|
|
|
import eu.eudat.types.Authorities;
|
2017-12-15 17:57:41 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-01-31 16:39:16 +01:00
|
|
|
import org.springframework.core.env.Environment;
|
2017-12-15 17:57:41 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
2018-01-31 16:39:16 +01:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2017-12-15 17:57:41 +01:00
|
|
|
|
|
|
|
import javax.xml.ws.ServiceMode;
|
2018-01-10 17:05:23 +01:00
|
|
|
import java.util.*;
|
2018-02-07 10:56:30 +01:00
|
|
|
import java.util.stream.Collectors;
|
2017-12-15 17:57:41 +01:00
|
|
|
|
2018-02-01 10:08:06 +01:00
|
|
|
|
2017-12-18 16:55:12 +01:00
|
|
|
@Service("authenticationService ")
|
2017-12-15 17:57:41 +01:00
|
|
|
public class AuthenticationService {
|
2018-01-10 17:05:23 +01:00
|
|
|
private ApiContext apiContext;
|
2018-01-31 16:39:16 +01:00
|
|
|
private Environment environment;
|
2018-01-10 17:05:23 +01:00
|
|
|
|
2017-12-15 17:57:41 +01:00
|
|
|
@Autowired
|
2018-01-31 16:39:16 +01:00
|
|
|
public AuthenticationService(ApiContext apiContext, Environment environment) {
|
|
|
|
this.environment = environment;
|
2018-01-10 17:05:23 +01:00
|
|
|
this.apiContext = apiContext;
|
|
|
|
}
|
2017-12-15 17:57:41 +01:00
|
|
|
|
2018-01-31 16:39:16 +01:00
|
|
|
public Principal Touch(UUID token) {
|
2018-01-25 16:24:21 +01:00
|
|
|
UserToken tokenEntry = this.apiContext.getDatabaseRepository().getUserTokenDao().find(token);
|
2017-12-15 17:57:41 +01:00
|
|
|
if (tokenEntry == null || tokenEntry.getExpiresAt().before(new Date())) return null;
|
|
|
|
|
2018-02-01 08:47:24 +01:00
|
|
|
return this.Touch(tokenEntry);
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 16:39:16 +01:00
|
|
|
public void Logout(UUID token) {
|
2018-01-25 16:24:21 +01:00
|
|
|
UserToken tokenEntry = this.apiContext.getDatabaseRepository().getUserTokenDao().find(token);
|
2018-01-10 17:05:23 +01:00
|
|
|
this.apiContext.getDatabaseRepository().getUserTokenDao().delete(tokenEntry);
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 16:39:16 +01:00
|
|
|
private Principal Touch(UserToken token) {
|
2017-12-15 17:57:41 +01:00
|
|
|
if (token == null || token.getExpiresAt().before(new Date())) return null;
|
|
|
|
|
2018-01-10 17:05:23 +01:00
|
|
|
UserInfo user = this.apiContext.getDatabaseRepository().getUserInfoDao().find(token.getUser().getId());
|
2018-01-31 16:39:16 +01:00
|
|
|
if (user == null) return null;
|
2017-12-15 17:57:41 +01:00
|
|
|
|
|
|
|
Principal principal = new Principal();
|
|
|
|
principal.setId(user.getId());
|
|
|
|
principal.setToken(token.getToken());
|
|
|
|
principal.setExpiresAt(token.getExpiresAt());
|
|
|
|
principal.setName(user.getName());
|
|
|
|
|
2018-02-07 10:56:30 +01:00
|
|
|
List<UserRole> userRoles = user.getUserRoles().stream().collect(Collectors.toList());
|
2018-01-31 16:39:16 +01:00
|
|
|
for (UserRole item : userRoles) {
|
2018-02-01 08:47:24 +01:00
|
|
|
if (principal.getAuthz() == null) principal.setAuthorities(new HashSet<>());
|
2018-01-31 16:39:16 +01:00
|
|
|
principal.getAuthz().add(Authorities.fromInteger(item.getRole()));
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|
|
|
|
return principal;
|
|
|
|
}
|
2018-01-10 17:05:23 +01:00
|
|
|
|
2018-01-31 16:39:16 +01:00
|
|
|
public Principal Touch(Credentials credentials) {
|
|
|
|
Credential credential = this.apiContext.getDatabaseRepository().getCredentialDao().getLoggedInCredentials(credentials);
|
|
|
|
|
2018-02-01 08:47:24 +01:00
|
|
|
if (credential == null && credentials.getUsername().equals(environment.getProperty("autouser.root.username")))
|
|
|
|
credential = this.autoCreateUser(credentials.getUsername(), credentials.getSecret());
|
2018-01-31 16:39:16 +01:00
|
|
|
|
2018-02-01 08:47:24 +01:00
|
|
|
if (credential == null) return null;
|
2018-01-31 16:39:16 +01:00
|
|
|
|
|
|
|
UserToken userToken = new UserToken();
|
|
|
|
userToken.setUser(credential.getUserInfo());
|
|
|
|
userToken.setIssuedAt(new Date());
|
|
|
|
userToken.setToken(UUID.randomUUID());
|
|
|
|
userToken.setExpiresAt(addADay(new Date()));
|
|
|
|
userToken = apiContext.getDatabaseRepository().getUserTokenDao().createOrUpdate(userToken);
|
|
|
|
|
|
|
|
return this.Touch(userToken);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Principal Touch(LoginProviderUser profile) {
|
2018-01-10 17:05:23 +01:00
|
|
|
UserInfoCriteria criteria = new UserInfoCriteria();
|
|
|
|
criteria.setEmail(profile.getEmail());
|
|
|
|
List<UserInfo> users = apiContext.getDatabaseRepository().getUserInfoDao().getWithCriteria(criteria).toList();
|
|
|
|
UserInfo userInfo = null;
|
2018-01-31 16:39:16 +01:00
|
|
|
if (users.size() > 0) userInfo = users.get(0);
|
2018-01-10 17:05:23 +01:00
|
|
|
final Credential credential = new Credential();
|
|
|
|
credential.setId(UUID.randomUUID());
|
|
|
|
credential.setCreationTime(new Date());
|
|
|
|
credential.setStatus(1);
|
|
|
|
credential.setLastUpdateTime(new Date());
|
2018-01-11 17:19:15 +01:00
|
|
|
credential.setProvider((int) profile.getProvider().getValue());
|
2018-01-10 17:05:23 +01:00
|
|
|
credential.setSecret(profile.getSecret());
|
2018-01-31 16:39:16 +01:00
|
|
|
if (userInfo == null) {
|
2018-01-10 17:05:23 +01:00
|
|
|
userInfo = new UserInfo();
|
2018-02-01 08:47:24 +01:00
|
|
|
userInfo.setName(profile.getName());
|
2018-01-10 17:05:23 +01:00
|
|
|
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);
|
2018-01-31 16:39:16 +01:00
|
|
|
|
|
|
|
UserRole role = new UserRole();
|
|
|
|
role.setRole(Authorities.USER.getValue());
|
|
|
|
role.setUserInfo(userInfo);
|
|
|
|
|
|
|
|
} else {
|
2018-01-10 17:05:23 +01:00
|
|
|
userInfo.setLastloggedin(new Date());
|
|
|
|
Set<Credential> credentials = userInfo.getCredentials();
|
2018-01-31 16:39:16 +01:00
|
|
|
if (credentials.contains(credential)) {
|
|
|
|
Credential oldCredential = credentials.stream().filter(item -> credential.getProvider().equals(item.getProvider())).findFirst().get();
|
2018-01-10 17:05:23 +01:00
|
|
|
credential.setId(oldCredential.getId());
|
2018-01-31 16:39:16 +01:00
|
|
|
} else {
|
2018-01-10 17:05:23 +01:00
|
|
|
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()));
|
2018-01-25 16:24:21 +01:00
|
|
|
apiContext.getDatabaseRepository().getUserTokenDao().createOrUpdate(userToken);
|
2018-01-10 17:05:23 +01:00
|
|
|
return Touch(userToken.getToken());
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:39:16 +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;
|
|
|
|
}
|
2018-01-31 16:39:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
2018-02-01 08:47:24 +01:00
|
|
|
private Credential autoCreateUser(String username, String password) {
|
|
|
|
if (!environment.getProperty("autouser.root.username").equals(username) || !environment.getProperty("autouser.root.password").equals(password))
|
|
|
|
return null;
|
2018-01-31 16:39:16 +01:00
|
|
|
|
|
|
|
UserInfo userInfo = new UserInfo();
|
|
|
|
userInfo.setName(username);
|
|
|
|
userInfo.setEmail(environment.getProperty("autouser.root.email"));
|
|
|
|
userInfo.setCreated(new Date());
|
|
|
|
userInfo.setLastloggedin(new Date());
|
2018-02-01 08:47:24 +01:00
|
|
|
userInfo.setAuthorization_level((short) 1);
|
|
|
|
userInfo.setUsertype((short) 1);
|
2018-01-31 16:39:16 +01:00
|
|
|
userInfo = this.apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
|
|
|
|
|
|
|
|
UserRole role = new UserRole();
|
|
|
|
role.setRole(Authorities.ADMIN.getValue());
|
|
|
|
role.setUserInfo(userInfo);
|
|
|
|
this.apiContext.getDatabaseRepository().getUserRoleDao().createOrUpdate(role);
|
|
|
|
|
|
|
|
Credential credential = new Credential();
|
|
|
|
credential.setUserInfo(userInfo);
|
|
|
|
credential.setPublicValue(username);
|
|
|
|
credential.setSecret(password);
|
2018-02-01 08:47:24 +01:00
|
|
|
credential.setProvider((int) TokenValidatorFactoryImpl.LoginProvider.NATIVELOGIN.getValue());
|
2018-01-31 16:39:16 +01:00
|
|
|
credential.setCreationTime(new Date());
|
|
|
|
credential.setLastUpdateTime(new Date());
|
|
|
|
credential.setStatus(0);
|
|
|
|
return this.apiContext.getDatabaseRepository().getCredentialDao().createOrUpdate(credential);
|
|
|
|
}
|
2017-12-15 17:57:41 +01:00
|
|
|
}
|