gcube-cms-suite/cms-plugin-framework/src/main/java/org/gcube/application/cms/implementations/utils/UserUtils.java

103 lines
3.0 KiB
Java
Raw Normal View History

2023-01-10 15:57:40 +01:00
package org.gcube.application.cms.implementations.utils;
2021-11-15 16:27:21 +01:00
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
2022-02-14 12:23:13 +01:00
import org.gcube.application.geoportal.common.model.document.accounting.AccountingInfo;
import org.gcube.application.geoportal.common.model.document.accounting.Context;
import org.gcube.application.geoportal.common.model.document.accounting.User;
2021-11-15 16:27:21 +01:00
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
2022-10-18 15:09:49 +02:00
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
2021-11-15 16:27:21 +01:00
2021-12-07 16:12:43 +01:00
import java.time.LocalDateTime;
2022-09-26 16:11:20 +02:00
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
2021-12-07 16:12:43 +01:00
2021-11-15 16:27:21 +01:00
@Slf4j
public class UserUtils {
2022-04-01 19:11:11 +02:00
public static List<String> DEFAULT_ROLES=new ArrayList<>();
2021-11-24 14:47:59 +01:00
public static AuthenticatedUser getCurrent() throws SecurityException {
2022-10-18 15:09:49 +02:00
log.debug("Loading caller info..");
2021-11-15 16:27:21 +01:00
2022-10-18 15:09:49 +02:00
SecretManager cm = SecretManagerProvider.instance.get();
String context = cm.getContext();
if(context==null) throw new SecurityException("Cannot determine context");
org.gcube.common.authorization.utils.user.User user = cm.getUser();
log.info("Identified caller {} in context {}",user.getUsername(),context);
2021-11-15 16:27:21 +01:00
2022-05-06 12:45:18 +02:00
Set<String> roles=new HashSet<>();
2022-10-18 15:09:49 +02:00
roles.addAll(user.getRoles());
2022-05-06 12:45:18 +02:00
2022-03-24 17:44:00 +01:00
AuthenticatedUser toReturn =
2022-10-18 15:09:49 +02:00
new AuthenticatedUser(user,roles, AccessTokenProvider.instance.get(),SecurityTokenProvider.instance.get(),context);
2021-11-15 16:27:21 +01:00
log.info("Current User is {} ",toReturn);
return toReturn;
}
@AllArgsConstructor
@Getter
2021-11-24 14:47:59 +01:00
public static class AuthenticatedUser {
2022-10-18 15:09:49 +02:00
private org.gcube.common.authorization.utils.user.User user;
2022-03-24 17:44:00 +01:00
private Set<String> roles;
2021-11-15 16:27:21 +01:00
private String uma_token;
private String gcube_token;
private String context;
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("User [user=");
builder.append(user);
builder.append(", uma_token=");
builder.append(uma_token==null?uma_token:"***");
builder.append(", gcube_token=");
builder.append(gcube_token==null?gcube_token:"***");
2022-03-24 17:44:00 +01:00
builder.append(", roles=");
builder.append(roles);
2021-11-15 16:27:21 +01:00
builder.append(", context=");
builder.append(context);
builder.append("]");
return builder.toString();
}
2021-12-07 16:12:43 +01:00
public AccountingInfo asInfo(){
AccountingInfo info=new AccountingInfo();
User user = new User();
2022-01-12 18:42:22 +01:00
try{
2022-10-18 15:09:49 +02:00
user.setUsername(this.getUser().getUsername());
2022-03-24 17:44:00 +01:00
user.setRoles(roles);
2022-01-12 18:42:22 +01:00
}catch(Exception e){
2022-04-08 11:13:25 +02:00
log.warn("Unable to determine user id, using FAKE",e);
2022-10-18 15:09:49 +02:00
user.setUsername("FAKE");
2022-03-30 18:39:10 +02:00
user.setRoles(new HashSet<>());
2022-04-01 19:11:11 +02:00
user.getRoles().addAll(DEFAULT_ROLES);
2022-01-12 18:42:22 +01:00
}
2021-12-07 16:12:43 +01:00
info.setUser(user);
info.setInstant(LocalDateTime.now());
Context c=new Context();
c.setId(this.context);
c.setName(context.contains("/")?context.substring(context.lastIndexOf("/")):context);
info.setContext(c);
return info;
}
2021-11-15 16:27:21 +01:00
}
}