Switching to new authz

This commit is contained in:
Luca Frosini 2022-01-31 11:59:58 +01:00
parent fab1cb240a
commit 3e1b48d645
4 changed files with 22 additions and 58 deletions

View File

@ -6,8 +6,8 @@ import java.util.Properties;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
@ -18,43 +18,6 @@ public class Constants {
public static final String CATALOGUE_NAME = "gCat";
// private static final String PROPERTY_FILENAME = "config.properties";
// /*
// * Key : Context
// * Value : Application Token
// */
// protected static final Map<String,String> applicationTokens;
//
// static {
// try {
// applicationTokens = new HashMap<>();
// Properties properties = new Properties();
// InputStream input = Constants.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
// // load a properties file
// properties.load(input);
// Enumeration<?> enumeration = properties.propertyNames();
// while(enumeration.hasMoreElements()) {
// String context = (String) enumeration.nextElement();
// String applicationToken = properties.getProperty(context);
// applicationTokens.put(context, applicationToken);
// }
// } catch(Exception e) {
// throw new WebApplicationException(e);
// }
// }
//
// @Deprecated
// private static String getCatalogueApplicationToken() {
// String context = SecretManager.instance.get().getContext();
// try {
// return applicationTokens.get(context);
// } catch(Exception e) {
// throw new InternalServerErrorException(
// "Unable to retrieve Application Token for context " + context, e);
// }
// }
private static final String CLIENT_SECRET_FILENAME = "config.properties";
private static final String CLIENT_ID = "gcat";
protected static String clientSecret;
@ -75,24 +38,16 @@ public class Constants {
}
}
private static String getJWTAccessToken() throws Exception {
private static TokenResponse getJWTAccessToken() throws Exception {
String contextToAuthorise = SecretManager.instance.get().getContext();
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
return tr.getAccessToken();
}
public static String getCatalogueSecurityToken() throws Exception {
try {
return getJWTAccessToken();
}catch (Exception e) {
throw e;
// return getCatalogueApplicationToken();
}
return tr;
}
public static Secret getCatalogueSecret() throws Exception {
String securityToken = getCatalogueSecurityToken();
return SecretUtility.getSecretByTokenString(securityToken);
TokenResponse tr = getJWTAccessToken();
Secret secret = new JWTSecret(tr.getAccessToken());
return secret;
}
}

View File

@ -388,7 +388,7 @@ public class CKANPackageTest extends ContextTest {
}
protected CKANPackage createPackage(ObjectMapper mapper) throws Exception {
protected CKANPackage createPackage(ObjectMapper mapper, Boolean socialPost) throws Exception {
String currentContext = SecretManager.instance.get().getContext();
ScopeBean scopeBean = new ScopeBean(currentContext);
String ckanOrganizationName = CKANPackage.getOrganizationName(scopeBean);
@ -435,6 +435,12 @@ public class CKANPackageTest extends ContextTest {
CKANPackage ckanPackage = new CKANPackage();
ckanPackage.setName(ITEM_NAME_VALUE);
//ckanPackage.setApiKey(CKANUtility.getSysAdminAPI());
MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>();
mvm.add(GCatConstants.SOCIAL_POST_QUERY_PARAMETER, socialPost.toString());
UriInfo uriInfo = getUriInfo(mvm);
ckanPackage.setUriInfo(uriInfo);
String createdItem = ckanPackage.create(mapper.writeValueAsString(itemObjectNode));
logger.debug(createdItem);
@ -444,7 +450,7 @@ public class CKANPackageTest extends ContextTest {
@Test
public void create() throws Exception {
ObjectMapper mapper = new ObjectMapper();
createPackage(mapper);
createPackage(mapper, true);
}
@Test
@ -459,7 +465,7 @@ public class CKANPackageTest extends ContextTest {
@Test
public void createReadUpdateUpdatePurge() throws Exception {
ObjectMapper mapper = new ObjectMapper();
createPackage(mapper);
createPackage(mapper, false);
CKANPackage ckanPackage = new CKANPackage();
ckanPackage.setName(ITEM_NAME_VALUE);
@ -527,6 +533,7 @@ public class CKANPackageTest extends ContextTest {
logger.debug(ret);
}
@Ignore
@Test
//(expected = NotFoundException.class)
public void delete() throws Exception {
@ -573,7 +580,7 @@ public class CKANPackageTest extends ContextTest {
ObjectMapper mapper = new ObjectMapper();
createPackage(mapper);
createPackage(mapper, false);
ContextTest.setContextByName("leonardo.candela_"+VRE);

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.gcat.ContextTest;
import org.gcube.gcat.utils.Constants;
import org.junit.Test;
@ -16,8 +17,9 @@ public class SocialPostTest extends ContextTest {
@Test
public void testToken() throws Exception {
logger.debug("Application Token is {}", Constants.getCatalogueSecurityToken());
ContextTest.setContext(Constants.getCatalogueSecurityToken());
Secret secret = Constants.getCatalogueSecret();
logger.debug("Application Token is {}", secret.getToken());
ContextTest.set(secret);
}
@Test

View File

@ -14,7 +14,7 @@ public class ConstantsTest extends ContextTest {
@Test
public void testGetApplicationToken() throws Exception {
logger.debug("Application token for Context {} is {}", SecretManager.instance.get().getContext(),
Constants.getCatalogueSecurityToken());
Constants.getCatalogueSecret().getToken());
}