Fixed corner case for a old authz token which is not an user

This commit is contained in:
Luca Frosini 2022-01-24 15:34:09 +01:00
parent 624174b7c0
commit 8e49a9be04
2 changed files with 35 additions and 1 deletions

View File

@ -1,16 +1,19 @@
package org.gcube.common.authorization.utils.secret;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Pattern;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.exception.AuthorizationException;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.authorization.utils.socialservice.SocialService;
import org.gcube.common.authorization.utils.user.GCubeUser;
import org.gcube.common.authorization.utils.user.User;
/**
@ -89,7 +92,23 @@ public class GCubeSecret extends Secret {
public User getUser() {
if(user==null) {
try {
user = SocialService.getSocialService().getUser(this);
ClientInfo clientInfo = getClientInfo();
ClientType clientType = clientInfo.getType();
switch (clientType) {
case USER:
user = SocialService.getSocialService().getUser(this);
break;
default:
// The client is not an user. Trying to do the best
GCubeUser gCubeUser = new GCubeUser();
gCubeUser.setRoles(new HashSet<>(clientInfo.getRoles()));
gCubeUser.setUsername(clientInfo.getId());
user = gCubeUser;
break;
}
} catch (Exception e) {
throw new RuntimeException();
}

View File

@ -23,6 +23,14 @@ public class GCubeUser implements User {
public GCubeUser() {
this.additionalProperties = new HashMap<>();
// This info are not always present. Setting an empty string to avoid null
this.givenName = "";
this.familyName = "";
this.eMail = "";
this.jobTitle = "";
this.picture = "";
this.middleName = "";
}
@JsonProperty("id")
@ -49,11 +57,18 @@ public class GCubeUser implements User {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public Collection<String> getRoles() {
return roles;
}
public void setRoles(Set<String> roles) {
this.roles = roles;
}
@Override
public String getGivenName() {
return givenName;