This commit is contained in:
Lucio Lelii 2016-09-07 12:11:39 +00:00
parent 90edc18075
commit 8fd89e4942
4 changed files with 44 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import javax.xml.bind.JAXBException;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.Policies;
import org.gcube.common.authorization.library.QualifiersList;
import org.gcube.common.authorization.library.policies.Policy;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.ServiceInfo;
@ -17,7 +18,7 @@ public class Binder {
public static JAXBContext getContext() throws JAXBException{
if (context==null)
context = JAXBContext.newInstance(AuthorizationEntry.class, ClientInfo.class, UserInfo.class,
context = JAXBContext.newInstance(QualifiersList.class, AuthorizationEntry.class, ClientInfo.class, UserInfo.class,
ServiceInfo.class, Policies.class, Policy.class);
return context;
}

View File

@ -33,5 +33,7 @@ public interface AuthorizationProxy {
throws Exception;
String requestActivation(ContainerInfo container, String context) throws Exception;
public Map<String, String> retrieveApiKeys() throws Exception;
}

View File

@ -20,6 +20,7 @@ import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.Policies;
import org.gcube.common.authorization.library.QualifiersList;
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint;
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner;
import org.gcube.common.authorization.library.policies.Policy;
@ -151,6 +152,34 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
return token;
}
@Override
/**
* return a map with key qualifier and value token
*/
public Map<String, String> retrieveApiKeys() throws Exception{
String methodPath = "/apikey/";
int infrastructureHash = getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
System.out.println(callUrl);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", true);
connection.setDoInput(true);
connection.setDoOutput(true);
if (connection.getResponseCode()!=200) throw new Exception("error retrieving keys (error code is "+connection.getResponseCode()+")");
if (connection.getContentLengthLong()<=0) return Collections.emptyMap();
try(InputStream stream = (InputStream)connection.getContent();){
QualifiersList entries = (QualifiersList)Binder.getContext().createUnmarshaller().unmarshal(stream);
//cache.put(token, new AuthorizationEntryCache(entry));
return entries.getQualifiers();
}
}
private int getInfrastructureHashfromContext(String context) {
try{

View File

@ -4,6 +4,7 @@ import static org.gcube.common.authorization.client.Constants.authorizationServi
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.policies.Action;
@ -24,7 +25,7 @@ public class CallTest {
@Test
public void requestNodeToken() throws Exception {
String token = authorizationService().requestActivation(new ContainerInfo("dlib29.isti.cnr.it",8080), "/gcube");
String token = authorizationService().requestActivation(new ContainerInfo("dlib29.isti.cnr.it",8080), "/gcube/devNext");
System.out.println(token);
}
@ -63,6 +64,15 @@ public class CallTest {
System.out.println(resolveToken(key));
}
@Test
public void retrieveApiKeys() throws Exception {
String token = requestTestToken("/gcube/devNext");
SecurityTokenProvider.instance.set(token);
Map<String, String> keys = authorizationService().retrieveApiKeys();
System.out.println("keys : "+keys);
}
@Test
public void createTestToken() throws Exception {
System.out.println(requestTestToken("/gcube/devsec"));