2015-05-18 19:15:58 +02:00
|
|
|
package org.gcube.common.authorization.client.proxy;
|
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
import static org.gcube.common.authorization.client.Constants.CONTEXT_PARAM;
|
2015-05-18 19:15:58 +02:00
|
|
|
|
2016-01-22 17:28:17 +01:00
|
|
|
import java.io.BufferedOutputStream;
|
2015-05-18 19:15:58 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2016-01-22 17:28:17 +01:00
|
|
|
import java.io.OutputStream;
|
2015-05-18 19:15:58 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2016-01-22 17:28:17 +01:00
|
|
|
import java.util.ArrayList;
|
2015-11-27 18:06:28 +01:00
|
|
|
import java.util.Collections;
|
2015-06-04 18:49:59 +02:00
|
|
|
import java.util.List;
|
2016-05-13 17:00:54 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-02-01 15:14:44 +01:00
|
|
|
|
2015-05-18 19:15:58 +02:00
|
|
|
import org.gcube.common.authorization.client.Binder;
|
2015-07-30 17:33:48 +02:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2015-05-18 19:15:58 +02:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
2016-01-22 17:28:17 +01:00
|
|
|
import org.gcube.common.authorization.library.Policies;
|
2015-11-27 18:06:28 +01:00
|
|
|
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint;
|
|
|
|
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner;
|
2016-01-22 17:28:17 +01:00
|
|
|
import org.gcube.common.authorization.library.policies.Policy;
|
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
2016-06-15 13:47:22 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.ServiceInfo;
|
2016-01-22 17:28:17 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
2015-11-24 19:26:37 +01:00
|
|
|
import org.gcube.common.encryption.StringEncrypter;
|
2015-12-22 19:38:51 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-18 19:15:58 +02:00
|
|
|
|
|
|
|
public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
|
2015-12-22 19:38:51 +01:00
|
|
|
private static Logger log = LoggerFactory.getLogger(AuthorizationProxy.class);
|
|
|
|
|
2016-05-13 17:00:54 +02:00
|
|
|
private static Map<String, AuthorizationEntryCache> cache = new ConcurrentHashMap<String, AuthorizationEntryCache>();
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2015-11-27 18:06:28 +01:00
|
|
|
private List<AuthorizationEndpoint> endpoints;
|
2015-05-18 19:15:58 +02:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
public DefaultAuthorizationProxy() {
|
2016-01-22 17:28:17 +01:00
|
|
|
endpoints = AuthorizationEndpointScanner.endpoints();
|
|
|
|
this.setEndpoint(endpoints.get(0));
|
2015-11-24 19:26:37 +01:00
|
|
|
}
|
2015-05-21 16:19:37 +02:00
|
|
|
|
2015-11-27 18:06:28 +01:00
|
|
|
private String getInternalEnpoint(){
|
|
|
|
StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":")
|
|
|
|
.append(getEndpoint().getPort()).append("/authorization-service/gcube/service");
|
|
|
|
return endpoint.toString();
|
|
|
|
}
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2015-11-27 18:06:28 +01:00
|
|
|
@Override
|
2016-01-22 17:28:17 +01:00
|
|
|
public String generateToken(ClientInfo client, String context) throws Exception {
|
|
|
|
|
|
|
|
String methodPath = "/generate/";
|
|
|
|
|
|
|
|
if (client instanceof UserInfo)
|
|
|
|
methodPath+="user";
|
|
|
|
else methodPath+="service";
|
|
|
|
|
2015-11-27 18:06:28 +01:00
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?")
|
|
|
|
.append(CONTEXT_PARAM).append("=").append(context);
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
2016-01-22 17:28:17 +01:00
|
|
|
connection.setRequestMethod("PUT");
|
|
|
|
connection.setDoOutput(true);
|
2016-01-26 18:45:27 +01:00
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
2016-01-22 17:28:17 +01:00
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
2016-01-26 18:45:27 +01:00
|
|
|
Binder.getContext().createMarshaller().marshal(client, os);
|
2016-01-22 17:28:17 +01:00
|
|
|
}
|
2016-01-26 18:45:27 +01:00
|
|
|
|
2016-06-15 13:47:22 +02:00
|
|
|
log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage());
|
2016-01-26 18:45:27 +01:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
|
|
|
|
String encryptedToken= "";
|
2016-01-22 17:28:17 +01:00
|
|
|
try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){
|
2015-11-24 19:26:37 +01:00
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while((line = reader.readLine()) != null)
|
|
|
|
result.append(line);
|
|
|
|
encryptedToken = result.toString();
|
2015-05-18 19:15:58 +02:00
|
|
|
}
|
2015-12-22 19:38:51 +01:00
|
|
|
|
|
|
|
return StringEncrypter.getEncrypter().decrypt(encryptedToken, context);
|
2015-05-18 19:15:58 +02:00
|
|
|
}
|
2016-06-15 13:47:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String requestActivation(ServiceInfo container, String context) throws Exception {
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2016-06-15 13:47:22 +02:00
|
|
|
String methodPath = "/generate/node";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?")
|
|
|
|
.append(CONTEXT_PARAM).append("=").append(context);
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
|
|
connection.setRequestMethod("PUT");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
|
|
Binder.getContext().createMarshaller().marshal(container, os);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("response code is "+connection.getResponseCode());
|
|
|
|
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
|
|
|
|
String encryptedToken= "";
|
|
|
|
try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while((line = reader.readLine()) != null)
|
|
|
|
result.append(line);
|
|
|
|
encryptedToken = result.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringEncrypter.getEncrypter().decrypt(encryptedToken, context);
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:32:43 +02:00
|
|
|
@Override
|
2016-01-22 17:28:17 +01:00
|
|
|
public AuthorizationEntry get(String token) throws ObjectNotFound, Exception{
|
2016-05-13 17:00:54 +02:00
|
|
|
if (cache.containsKey(token) && cache.get(token).isValid())
|
|
|
|
return cache.get(token).getEntry();
|
2016-02-01 15:14:44 +01:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
final String methodPath = "/retrieve/";
|
|
|
|
|
2015-11-27 18:06:28 +01:00
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token);
|
2015-12-22 19:38:51 +01:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
URL url = new URL(callUrl.toString());
|
|
|
|
HttpURLConnection connection = makeRequest(url, "GET");
|
2016-01-22 17:28:17 +01:00
|
|
|
connection.setDoInput(true);
|
2015-11-24 19:26:37 +01:00
|
|
|
if (connection.getResponseCode()==404) throw new ObjectNotFound("token "+token+" not found");
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
|
|
|
|
if (connection.getContentLengthLong()<=0) return null;
|
|
|
|
|
|
|
|
try(InputStream stream = (InputStream)connection.getContent();){
|
|
|
|
AuthorizationEntry entry = (AuthorizationEntry)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
2016-05-02 18:43:20 +02:00
|
|
|
//cache.put(token, new AuthorizationEntryCache(entry));
|
2015-11-24 19:26:37 +01:00
|
|
|
return entry;
|
2015-05-29 18:32:43 +02:00
|
|
|
}
|
2015-11-24 19:26:37 +01:00
|
|
|
|
2015-05-29 18:32:43 +02:00
|
|
|
}
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2015-05-29 18:32:43 +02:00
|
|
|
@Override
|
2016-01-22 17:28:17 +01:00
|
|
|
public void addPolicies(List<Policy> policies) throws Exception {
|
|
|
|
final String methodPath = "/policyManager";
|
2015-11-24 19:26:37 +01:00
|
|
|
|
2016-01-22 17:28:17 +01:00
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath);
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
2015-11-24 19:26:37 +01:00
|
|
|
HttpURLConnection connection = makeRequest(url, "POST");
|
2016-01-22 17:28:17 +01:00
|
|
|
connection.setDoOutput(true);
|
2016-01-26 18:45:27 +01:00
|
|
|
connection.setRequestProperty("Content-type", "application/xml");
|
|
|
|
|
2016-01-22 17:28:17 +01:00
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
2016-01-26 18:45:27 +01:00
|
|
|
Binder.getContext().createMarshaller().marshal(new Policies(policies), os);
|
2015-05-29 18:32:43 +02:00
|
|
|
}
|
2016-01-22 17:28:17 +01:00
|
|
|
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error adding policies");
|
|
|
|
|
2015-05-29 18:32:43 +02:00
|
|
|
}
|
2015-05-18 19:15:58 +02:00
|
|
|
|
2015-06-04 18:49:59 +02:00
|
|
|
@Override
|
2016-01-22 17:28:17 +01:00
|
|
|
public void removePolicies(long... ids) throws Exception {
|
|
|
|
final String methodPath = "/policyManager/";
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath);
|
|
|
|
List<Long> errorIds = new ArrayList<Long>();
|
|
|
|
for (long id: ids){
|
|
|
|
URL url = new URL(callUrl.toString()+id);
|
|
|
|
HttpURLConnection connection = makeRequest(url, "DELETE");
|
|
|
|
if (connection.getResponseCode()!=200) errorIds.add(id);
|
|
|
|
}
|
|
|
|
if (!errorIds.isEmpty())
|
|
|
|
throw new Exception("error removing policies with ids: "+errorIds);
|
2015-11-24 19:26:37 +01:00
|
|
|
}
|
2015-06-04 18:49:59 +02:00
|
|
|
|
2015-11-24 19:26:37 +01:00
|
|
|
@Override
|
2016-01-22 17:28:17 +01:00
|
|
|
public List<Policy> getPolicies(String context) throws Exception{
|
|
|
|
final String methodPath = "/policyManager/";
|
2015-06-04 18:49:59 +02:00
|
|
|
|
2016-01-22 17:28:17 +01:00
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context);
|
|
|
|
|
|
|
|
URL url = new URL(callUrl.toString());
|
2015-11-24 19:26:37 +01:00
|
|
|
HttpURLConnection connection = makeRequest(url, "GET");
|
2016-01-26 18:45:27 +01:00
|
|
|
connection.setDoInput(true);
|
2016-01-22 17:28:17 +01:00
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error retrieving policies");
|
2016-01-26 18:45:27 +01:00
|
|
|
if (connection.getContentLengthLong()<=0) return Collections.emptyList();
|
2015-06-04 18:49:59 +02:00
|
|
|
|
2016-01-26 18:45:27 +01:00
|
|
|
try(InputStreamReader stream = new InputStreamReader((InputStream)connection.getContent())){
|
2016-01-22 17:28:17 +01:00
|
|
|
Policies policies = (Policies)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
|
|
return policies.getPolicies();
|
2015-06-04 18:49:59 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-22 17:28:17 +01:00
|
|
|
|
2015-06-04 18:49:59 +02:00
|
|
|
private HttpURLConnection makeRequest(URL url, String method) throws Exception{
|
|
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
2016-02-10 15:06:51 +01:00
|
|
|
//connection.setRequestProperty(Constants.SCOPE_HEADER_ENTRY, ScopeProvider.instance.get());
|
2015-06-04 18:49:59 +02:00
|
|
|
connection.setRequestMethod(method);
|
|
|
|
return connection;
|
|
|
|
}
|
2015-11-27 18:06:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public AuthorizationEndpoint getEndpoint() {
|
|
|
|
return this.endpoints.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEndpoint(AuthorizationEndpoint authEndpoint) {
|
|
|
|
this.endpoints = Collections.singletonList(authEndpoint);
|
|
|
|
}
|
2016-01-22 17:28:17 +01:00
|
|
|
|
|
|
|
|
2015-05-18 19:15:58 +02:00
|
|
|
}
|