diff --git a/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java b/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java index defa7b8..e8e8727 100644 --- a/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java +++ b/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java @@ -2,6 +2,7 @@ package org.gcube.dataharvest.utils; import static org.gcube.common.authorization.client.Constants.authorizationService; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -21,25 +22,25 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) */ public class ContextAuthorization { - + private static Logger logger = LoggerFactory.getLogger(ContextAuthorization.class); - + 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 contextToToken; - + /** * Contains Token as key and Context full name as Value */ protected Map tokenToContext; - + /** * Contains Properties used to generate tokens */ @@ -48,40 +49,39 @@ public class ContextAuthorization { this.tokenToContext = new HashMap<>(); retrieveContextsAndTokens(); } - + protected void retrieveContextsAndTokens() throws Exception { - + String initialToken = SecurityTokenProvider.instance.get(); - + try { - + Properties properties = AccountingDataHarvesterPlugin.getProperties().get(); - + LinkedHashMap map = ContextManager.readContexts(); for(String scope : map.keySet()) { try { String context = map.get(scope).toString(); logger.info("Going to generate Token for Context {}", context); -// UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME), -// new ArrayList<>()); - UserInfo userInfo = null; + 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; @@ -89,17 +89,17 @@ public class ContextAuthorization { SecurityTokenProvider.instance.set(initialToken); } } - + public String getTokenForContext(String contextFullName) { return contextToToken.get(contextFullName); } - + public String getContextFromToken(String token) { return tokenToContext.get(token); } - + public SortedSet getContexts() { return new TreeSet(contextToToken.keySet()); } - + }