124 lines
3.7 KiB
Java
124 lines
3.7 KiB
Java
package org.gcube.dataharvest.utils;
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
import java.io.File;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class ContextAuthorization {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ContextAuthorization.class);
|
|
|
|
protected Properties properties;
|
|
|
|
public static final String VO_FILE = "VO_FILE";
|
|
public static final String DEFAULT_VO_FILENAME = "scopedata.xml";
|
|
|
|
public static final String USERNAME = "USERNAME";
|
|
public static final String DEFAULT_USERNAME = "luca.frosini";
|
|
|
|
public static final String SERVICE_NAME = "SERVICE_NAME";
|
|
public static final String DEFAULT_SERVICE_NAME = "accounting-harvester";
|
|
|
|
/**
|
|
* Contains Context full name as key and Token as Value
|
|
*/
|
|
protected Map<String, String> contextToToken;
|
|
|
|
/**
|
|
* Contains Token as key and Context full name as Value
|
|
*/
|
|
protected Map<String, String> tokenToContext;
|
|
|
|
/**
|
|
* Contains Properties used to generate tokens
|
|
*/
|
|
|
|
public ContextAuthorization(Properties properties) throws Exception {
|
|
this.contextToToken = new HashMap<>();
|
|
this.tokenToContext = new HashMap<>();
|
|
if(properties!=null) {
|
|
this.properties = properties;
|
|
}else {
|
|
this.properties = new Properties();
|
|
}
|
|
//retrieveContextsAndTokens();
|
|
}
|
|
|
|
public File getVOFile() {
|
|
try {
|
|
String voFileName = properties.getProperty(VO_FILE, DEFAULT_VO_FILENAME);
|
|
URL url = ContextAuthorization.class.getClassLoader().getResource(voFileName);
|
|
File voFile = new File(url.toURI());
|
|
logger.trace("VO file is {}", voFile);
|
|
if(!voFile.exists()) {
|
|
throw new Exception("No VO file found. Unable to continue without it");
|
|
}
|
|
return voFile;
|
|
}catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
protected void retrieveContextsAndTokens() throws Exception {
|
|
|
|
String initialToken = SecurityTokenProvider.instance.get();
|
|
|
|
try {
|
|
|
|
LinkedHashMap<String, ScopeBean> map = ScopeManager.readScopes(getVOFile().getAbsolutePath());
|
|
for(String scope : map.keySet()) {
|
|
try {
|
|
String context = map.get(scope).toString();
|
|
System.out.println("Going to generate Token for Context " + context);
|
|
logger.info("Going to generate Token for Context {}", context);
|
|
UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME), new ArrayList<>());
|
|
String userToken = authorizationService().generateUserToken(userInfo, context);
|
|
SecurityTokenProvider.instance.set(userToken);
|
|
String generatedToken = authorizationService().generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME));
|
|
|
|
logger.trace("Token for Context {} is {}", context, generatedToken);
|
|
|
|
contextToToken.put(context, generatedToken);
|
|
tokenToContext.put(generatedToken, context);
|
|
|
|
}catch (Exception e) {
|
|
logger.error("Error while elaborating {}", scope, e);
|
|
throw e;
|
|
} finally {
|
|
SecurityTokenProvider.instance.reset();
|
|
}
|
|
|
|
}
|
|
} catch (Exception ex) {
|
|
throw ex;
|
|
} finally {
|
|
SecurityTokenProvider.instance.set(initialToken);
|
|
}
|
|
}
|
|
|
|
public String getTokenForContext(String contextFullName) {
|
|
return contextToToken.get(contextFullName);
|
|
}
|
|
|
|
public String getContextFromToken(String token) {
|
|
return tokenToContext.get(token);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|