uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/service/security/AaiRoleMappingService.java

112 lines
3.6 KiB
Java
Raw Normal View History

2021-07-21 13:51:18 +02:00
package eu.dnetlib.repo.manager.service.security;
2023-01-11 17:50:31 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-07-21 13:51:18 +02:00
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
2023-02-17 15:17:37 +01:00
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
2021-07-21 13:51:18 +02:00
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
@Service("roleMappingService")
public class AaiRoleMappingService implements RoleMappingService {
2023-01-11 17:50:31 +01:00
private static final Logger logger = LoggerFactory.getLogger(AaiRoleMappingService.class);
2021-07-21 13:51:18 +02:00
2021-10-26 13:46:08 +02:00
@Value("${services.provide.aai.registry.production:true}")
2021-07-21 13:51:18 +02:00
private boolean production;
@Override
2023-02-17 15:17:37 +01:00
public String getRepositoryId(String roleId) {
2021-07-21 13:51:18 +02:00
if (!roleActive(roleId)) {
return null;
}
return roleId.replaceFirst(".*datasource\\.", "").replace("$", ":");
}
@Override
2023-02-17 15:17:37 +01:00
public Collection<String> getRepositoryIds(Collection<String> roleIds) {
2021-07-21 13:51:18 +02:00
return roleIds
.stream()
2023-02-17 15:17:37 +01:00
.map(this::getRepositoryId)
2021-07-21 13:51:18 +02:00
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
@Override
2023-02-17 15:17:37 +01:00
public String getRole(String repoId) {
String role = null;
2021-07-21 13:51:18 +02:00
String prefix = (production ? "" : "beta.") + "datasource";
if (repoId != null) {
2023-02-17 15:17:37 +01:00
role = createRole(prefix, repoId);
2021-07-21 13:51:18 +02:00
}
2023-02-17 15:17:37 +01:00
return role;
2021-07-21 13:51:18 +02:00
}
@Override
2023-02-17 15:17:37 +01:00
public Collection<String> getRoles(Collection<String> repoIds) {
2021-07-21 13:51:18 +02:00
return repoIds
.stream()
2023-02-17 15:17:37 +01:00
.map(this::getRole)
2021-07-21 13:51:18 +02:00
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
@Override
2023-02-17 15:17:37 +01:00
public String authorityToRepositoryId(GrantedAuthority authority) {
String repo = null;
String auth = null;
try {
auth = URLDecoder.decode(authority.getAuthority(), "UTF-8").toLowerCase();
} catch (UnsupportedEncodingException e) {
logger.error("", e);
}
if (auth != null && roleActive(auth)) {
repo = auth
.replaceFirst(".*datasource\\_", "")
2021-07-21 13:51:18 +02:00
.replace("$", ":")
.toLowerCase();
}
return repo;
}
@Override
2023-02-17 15:17:37 +01:00
public GrantedAuthority repositoryIdToAuthority(String repoId) {
String role = null;
try {
role = URLEncoder.encode(convertRepoIdToAuthorityId(repoId), "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("", e);
}
return new SimpleGrantedAuthority(role);
2021-07-21 13:51:18 +02:00
}
2023-02-17 15:17:37 +01:00
private String createRole(String prefix, String repoId) {
return prefix + "." + repoId.replace(":", "$");
}
private boolean roleActive(String roleId) {
return (production && !roleId.toLowerCase().startsWith("beta"))
|| (!production && roleId.toLowerCase().startsWith("beta"));
}
private String convertRepoIdToAuthorityId(String repoId) {
2021-07-21 13:51:18 +02:00
StringBuilder roleBuilder = new StringBuilder();
String role = "";
if (repoId != null) {
roleBuilder.append(production ? "" : "beta.");
roleBuilder.append("datasource.");
roleBuilder.append(repoId.replace(":", "$"));
role = roleBuilder.toString().replace(".", "_").toUpperCase();
}
return role;
}
}