Stated switching to new authz

This commit is contained in:
Luca Frosini 2021-11-30 11:48:35 +01:00
parent 0c718dcee6
commit e1423021d9
5 changed files with 64 additions and 22 deletions

View File

@ -150,6 +150,12 @@
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>keycloak-client</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>io.taliox</groupId>
<artifactId>zulip-java-rest</artifactId>

View File

@ -183,7 +183,7 @@ public class SocialPost extends Thread {
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
gxhttpStringRequest.setSecurityToken(Constants.getCatalogueApplicationToken());
gxhttpStringRequest.setSecurityToken(Constants.getSecurityToken());
gxhttpStringRequest.path(SOCIAL_SERVICE_WRITE_APPLICATION_POST_PATH);
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(objectMapper.writeValueAsString(objectNode));

View File

@ -9,6 +9,9 @@ import java.util.Properties;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@ -17,22 +20,12 @@ 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;
public static String getCatalogueApplicationToken() {
try {
return applicationTokens.get(ContextUtility.getCurrentContext());
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + ContextUtility.getCurrentContext(), e);
}
}
static {
try {
applicationTokens = new HashMap<>();
@ -50,4 +43,50 @@ public class Constants {
throw new WebApplicationException(e);
}
}
@Deprecated
public static String getCatalogueApplicationToken() {
try {
return applicationTokens.get(ContextUtility.getCurrentContext());
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + ContextUtility.getCurrentContext(), e);
}
}
private static final String CLIENT_SECRET_FILENAME = "clientSecret";
private static final String CLIENT_ID = "gcat";
protected static String clientSecret;
private static String getClientSecret(String context) {
try {
if(clientSecret==null) {
Properties properties = new Properties();
InputStream input = Constants.class.getClassLoader().getResourceAsStream(CLIENT_SECRET_FILENAME);
properties.load(input);
String root = context.substring(0, context.indexOf('/', 1));
clientSecret = properties.getProperty(root);
}
return clientSecret;
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + ContextUtility.getCurrentContext(), e);
}
}
public static String getJWTAccessToken() throws Exception {
String contextToAuthorise = ContextUtility.getCurrentContext();
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
return tr.getAccessToken();
}
public static String getSecurityToken() throws Exception {
try {
return getJWTAccessToken();
}catch (Exception e) {
return getCatalogueApplicationToken();
}
}
}

View File

@ -43,7 +43,7 @@ public class CatalogueStorageHubManagement {
}
public URL ensureResourcePersistence(URL persistedURL, String itemID, String resourceID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
try {
applicationMode.start();
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(persistedURL.toString());
@ -68,7 +68,7 @@ public class CatalogueStorageHubManagement {
}
public void deleteResourcePersistence(String itemID, String resourceID, String mimeType) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
try {
applicationMode.start();
storageHubManagement = new StorageHubManagement();
@ -97,7 +97,7 @@ public class CatalogueStorageHubManagement {
}
public void renameFile(String resourceID, String revisionID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
try {
applicationMode.start();
FileContainer createdfile = storageHubManagement.getPersistedFile();
@ -110,7 +110,7 @@ public class CatalogueStorageHubManagement {
}
public void addRevisionID(String resourceID, String revisionID) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
try {
applicationMode.start();
internalAddRevisionID(resourceID, revisionID);
@ -120,7 +120,7 @@ public class CatalogueStorageHubManagement {
}
public FileContainer retrievePersistedFile(String id, String mimeType) throws Exception {
ApplicationMode applicationMode = new ApplicationMode(Constants.getCatalogueApplicationToken());
ApplicationMode applicationMode = new ApplicationMode(Constants.getSecurityToken());
try {
applicationMode.start();
return storageHubManagement.getPersistedFile(id, mimeType);
@ -129,8 +129,8 @@ public class CatalogueStorageHubManagement {
}
}
public FileContainer getPersistedFile() {
return storageHubManagement.getPersistedFile();
}
}

View File

@ -1,7 +1,4 @@
/*.gcubekey
/*.properties
/gCat_zuliprc
/leonardo.candela_zuliprc
/luca.frosini_zuliprc
/pasquale.pagano_zuliprc
/clientID
/*_zuliprc
/clientSecret