|
|
|
@ -36,7 +36,9 @@ import org.gcube.common.authorization.library.provider.ContainerInfo;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.ServiceInfo;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
|
|
|
|
import org.gcube.common.authorization.library.utils.AuthorizationEntryList;
|
|
|
|
|
import org.gcube.common.authorization.library.utils.ListMapper;
|
|
|
|
|
import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
@ -55,10 +57,15 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
|
|
|
|
|
private String getInternalEnpoint(int infrastructureHash){
|
|
|
|
|
AuthorizationEndpoint ae = getEndpoint(infrastructureHash);
|
|
|
|
|
return getInternalEnpoint(ae);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getInternalEnpoint(AuthorizationEndpoint ae){
|
|
|
|
|
StringBuilder endpoint = new StringBuilder(ae.isSecureConnection()?"https://":"http://").append(ae.getHost()).append(":")
|
|
|
|
|
.append(ae.getPort()).append("/authorization-service/gcube/service");
|
|
|
|
|
return endpoint.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateServiceToken(ServiceInfo client) throws Exception {
|
|
|
|
@ -74,7 +81,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
|
|
|
Binder.getContext().createMarshaller().marshal(client, os);
|
|
|
|
|
}
|
|
|
|
@ -94,6 +101,44 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> generateServiceToken(ServiceInfo client, List<String> containerTokens) throws Exception {
|
|
|
|
|
|
|
|
|
|
String methodPath = "/token/service/bunch";
|
|
|
|
|
|
|
|
|
|
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure());
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "PUT", true);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
|
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
|
|
|
Binder.getContext().createMarshaller().marshal(new MultiServiceTokenRequest(containerTokens, client), os);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
|
|
|
|
|
|
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
|
|
|
|
|
List<String> tokensToReturn = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
try(InputStream stream = (InputStream)connection.getContent();){
|
|
|
|
|
ListMapper entries = (ListMapper)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
|
|
|
|
|
|
|
|
for (String token: entries.getList())
|
|
|
|
|
tokensToReturn.add(Utils.addInfrastructureHashToToken(token, infrastructureHash));
|
|
|
|
|
return tokensToReturn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateExternalServiceToken(String serviceId) throws Exception {
|
|
|
|
|
|
|
|
|
@ -122,7 +167,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
|
|
|
|
|
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String resolveTokenByUserAndContext(String user, String context) throws ObjectNotFound, Exception {
|
|
|
|
|
|
|
|
|
@ -134,7 +179,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "GET", false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
|
|
|
|
|
|
|
|
|
|
if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found");
|
|
|
|
@ -151,7 +196,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
|
|
|
|
|
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateUserToken(UserInfo client, String context) throws Exception {
|
|
|
|
|
|
|
|
|
@ -186,7 +231,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
|
|
|
|
|
return Utils.addInfrastructureHashToToken(token, infrastructureHash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setTokenRoles(String token, List<String> roles) throws Exception {
|
|
|
|
|
|
|
|
|
@ -194,7 +239,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
String methodPath = String.format("/token/user/%s/roles",realToken);
|
|
|
|
|
|
|
|
|
|
int infrastructureHash = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
@ -202,9 +247,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
|
|
|
|
|
ListMapper listmapper = new ListMapper();
|
|
|
|
|
listmapper.setList(roles);
|
|
|
|
|
|
|
|
|
|
ListMapper listmapper = new ListMapper(roles);
|
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
|
|
|
Binder.getContext().createMarshaller().marshal(listmapper, os);
|
|
|
|
|
}
|
|
|
|
@ -212,17 +256,17 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
|
|
|
|
|
|
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeAllReleatedToken(String clientId, String context) throws Exception{
|
|
|
|
|
String methodPath = "/token/user";
|
|
|
|
|
int infrastructureHash = Utils.getInfrastructureHashfromContext(context);
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?")
|
|
|
|
|
.append(CONTEXT_PARAM).append("=").append(context).append("&").append(CLIENT_ID_PARAM).append("=").append(clientId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "DELETE", false);
|
|
|
|
|
//connection.setDoOutput(false);
|
|
|
|
@ -231,7 +275,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
|
|
|
|
|
|
|
|
|
|
if (connection.getResponseCode()!=200 && connection.getResponseCode()!=204) throw new Exception("error contacting authorization service");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -292,16 +336,16 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
QualifiersList entries = (QualifiersList)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
|
|
|
tokensQulifiersMap = entries.getQualifiers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tokensQulifiersMap!=null && !tokensQulifiersMap.isEmpty()){
|
|
|
|
|
Map<String, String> toReturnMap = new HashMap<String, String>();
|
|
|
|
|
for (Entry<String, String> entry: tokensQulifiersMap.entrySet())
|
|
|
|
|
toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash));
|
|
|
|
|
return toReturnMap;
|
|
|
|
|
} else return Collections.emptyMap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
/**
|
|
|
|
|
* return a map with key external service id and value token
|
|
|
|
@ -327,14 +371,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
ExternalServiceList entries = (ExternalServiceList)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
|
|
|
externalServiceMap = entries.getExternalServiceMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (externalServiceMap!=null && !externalServiceMap.isEmpty()){
|
|
|
|
|
Map<String, String> toReturnMap = new HashMap<String, String>();
|
|
|
|
|
for (Entry<String, String> entry: externalServiceMap.entrySet())
|
|
|
|
|
toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash));
|
|
|
|
|
return toReturnMap;
|
|
|
|
|
} else return Collections.emptyMap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -424,7 +468,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
final String methodPath = "/token/";
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHashFromToken))
|
|
|
|
|
.append(methodPath).append(realToken);
|
|
|
|
|
.append(methodPath).append(realToken);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "GET", false);
|
|
|
|
@ -441,6 +485,50 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<AuthorizationEntry> get(List<String> tokens) throws ObjectNotFound, Exception {
|
|
|
|
|
|
|
|
|
|
List<String> realTokens = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
List<AuthorizationEntry> toReturn = new ArrayList<AuthorizationEntry>();
|
|
|
|
|
|
|
|
|
|
AuthorizationEndpoint endpoint = null;
|
|
|
|
|
|
|
|
|
|
for (String token : tokens) {
|
|
|
|
|
String realToken = Utils.getRealToken(token);
|
|
|
|
|
|
|
|
|
|
if (cache.containsKey(realToken) && cache.get(realToken).isValid(endpoint.getClientCacheValidity()))
|
|
|
|
|
toReturn.add(cache.get(realToken).getEntry());
|
|
|
|
|
else realTokens.add(realToken);
|
|
|
|
|
|
|
|
|
|
if (endpoint==null) {
|
|
|
|
|
int infrastructureHashFromToken = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure());
|
|
|
|
|
endpoint = getEndpoint(infrastructureHashFromToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final String methodPath = "/resolve/?";
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(endpoint))
|
|
|
|
|
.append(methodPath);
|
|
|
|
|
|
|
|
|
|
for (String toAppend : realTokens)
|
|
|
|
|
callUrl= callUrl.append("token=").append(toAppend);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "GET", false);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
|
|
|
|
|
if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found");
|
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service (error code is "+connection.getResponseCode()+")");
|
|
|
|
|
if (connection.getContentLengthLong()==0) return null;
|
|
|
|
|
|
|
|
|
|
try(InputStream stream = (InputStream)connection.getContent();){
|
|
|
|
|
AuthorizationEntryList entries = (AuthorizationEntryList)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
|
|
|
return entries.getEntries();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addPolicies(List<Policy> policies) throws Exception {
|
|
|
|
@ -503,7 +591,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
final String methodPath = "/symmKey/";
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure())))
|
|
|
|
|
.append(methodPath);
|
|
|
|
|
.append(methodPath);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
|
HttpURLConnection connection = makeRequest(url, "GET", true);
|
|
|
|
|