Luca Frosini 2018-05-28 14:22:44 +00:00
parent b5ac35f453
commit b91e31853c
1 changed files with 22 additions and 22 deletions

View File

@ -2,6 +2,7 @@ package org.gcube.dataharvest.utils;
import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -21,25 +22,25 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class ContextAuthorization { public class ContextAuthorization {
private static Logger logger = LoggerFactory.getLogger(ContextAuthorization.class); private static Logger logger = LoggerFactory.getLogger(ContextAuthorization.class);
public static final String USERNAME = "USERNAME"; public static final String USERNAME = "USERNAME";
public static final String DEFAULT_USERNAME = "luca.frosini"; public static final String DEFAULT_USERNAME = "luca.frosini";
public static final String SERVICE_NAME = "SERVICE_NAME"; public static final String SERVICE_NAME = "SERVICE_NAME";
public static final String DEFAULT_SERVICE_NAME = "accounting-harvester"; public static final String DEFAULT_SERVICE_NAME = "accounting-harvester";
/** /**
* Contains Context full name as key and Token as Value * Contains Context full name as key and Token as Value
*/ */
protected Map<String,String> contextToToken; protected Map<String,String> contextToToken;
/** /**
* Contains Token as key and Context full name as Value * Contains Token as key and Context full name as Value
*/ */
protected Map<String,String> tokenToContext; protected Map<String,String> tokenToContext;
/** /**
* Contains Properties used to generate tokens * Contains Properties used to generate tokens
*/ */
@ -48,40 +49,39 @@ public class ContextAuthorization {
this.tokenToContext = new HashMap<>(); this.tokenToContext = new HashMap<>();
retrieveContextsAndTokens(); retrieveContextsAndTokens();
} }
protected void retrieveContextsAndTokens() throws Exception { protected void retrieveContextsAndTokens() throws Exception {
String initialToken = SecurityTokenProvider.instance.get(); String initialToken = SecurityTokenProvider.instance.get();
try { try {
Properties properties = AccountingDataHarvesterPlugin.getProperties().get(); Properties properties = AccountingDataHarvesterPlugin.getProperties().get();
LinkedHashMap<String,ScopeBean> map = ContextManager.readContexts(); LinkedHashMap<String,ScopeBean> map = ContextManager.readContexts();
for(String scope : map.keySet()) { for(String scope : map.keySet()) {
try { try {
String context = map.get(scope).toString(); String context = map.get(scope).toString();
logger.info("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), UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME),
// new ArrayList<>()); new ArrayList<>());
UserInfo userInfo = null;
String userToken = authorizationService().generateUserToken(userInfo, context); String userToken = authorizationService().generateUserToken(userInfo, context);
SecurityTokenProvider.instance.set(userToken); SecurityTokenProvider.instance.set(userToken);
String generatedToken = authorizationService() String generatedToken = authorizationService()
.generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME)); .generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME));
logger.trace("Token for Context {} is {}", context, generatedToken); logger.trace("Token for Context {} is {}", context, generatedToken);
contextToToken.put(context, generatedToken); contextToToken.put(context, generatedToken);
tokenToContext.put(generatedToken, context); tokenToContext.put(generatedToken, context);
} catch(Exception e) { } catch(Exception e) {
logger.error("Error while elaborating {}", scope, e); logger.error("Error while elaborating {}", scope, e);
throw e; throw e;
} finally { } finally {
SecurityTokenProvider.instance.reset(); SecurityTokenProvider.instance.reset();
} }
} }
} catch(Exception ex) { } catch(Exception ex) {
throw ex; throw ex;
@ -89,17 +89,17 @@ public class ContextAuthorization {
SecurityTokenProvider.instance.set(initialToken); SecurityTokenProvider.instance.set(initialToken);
} }
} }
public String getTokenForContext(String contextFullName) { public String getTokenForContext(String contextFullName) {
return contextToToken.get(contextFullName); return contextToToken.get(contextFullName);
} }
public String getContextFromToken(String token) { public String getContextFromToken(String token) {
return tokenToContext.get(token); return tokenToContext.get(token);
} }
public SortedSet<String> getContexts() { public SortedSet<String> getContexts() {
return new TreeSet<String>(contextToToken.keySet()); return new TreeSet<String>(contextToToken.keySet());
} }
} }