191 lines
6.8 KiB
Java
191 lines
6.8 KiB
Java
package org.gcube.common.authorization.client.proxy;
|
|
|
|
import static org.gcube.common.authorization.client.Constants.CONTEXT_PARAM;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
import org.gcube.common.authorization.client.Binder;
|
|
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.enpoints.AuthorizationEndpoint;
|
|
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner;
|
|
import org.gcube.common.authorization.library.policies.Policy;
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
|
import org.gcube.common.authorization.library.provider.ServiceInfo;
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
|
import org.gcube.common.encryption.StringEncrypter;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(AuthorizationProxy.class);
|
|
|
|
private static Map<String, AuthorizationEntryCache> cache = new HashMap<String, AuthorizationEntryCache>();
|
|
|
|
private static JAXBContext jaxbContext;
|
|
|
|
private List<AuthorizationEndpoint> endpoints;
|
|
|
|
public DefaultAuthorizationProxy() {
|
|
try {
|
|
jaxbContext = JAXBContext.newInstance(ClientInfo.class, UserInfo.class, ServiceInfo.class, Policies.class);
|
|
} catch (JAXBException e) {
|
|
log.error("error creating jaxb context",e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
endpoints = AuthorizationEndpointScanner.endpoints();
|
|
this.setEndpoint(endpoints.get(0));
|
|
}
|
|
|
|
private String getInternalEnpoint(){
|
|
StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":")
|
|
.append(getEndpoint().getPort()).append("/authorization-service/gcube/service");
|
|
return endpoint.toString();
|
|
|
|
}
|
|
|
|
@Override
|
|
public String generateToken(ClientInfo client, String context) throws Exception {
|
|
|
|
String methodPath = "/generate/";
|
|
|
|
if (client instanceof UserInfo)
|
|
methodPath+="user";
|
|
else methodPath+="service";
|
|
|
|
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);
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
jaxbContext.createMarshaller().marshal(client, os);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
public AuthorizationEntry get(String token) throws ObjectNotFound, Exception{
|
|
final String methodPath = "/retrieve/";
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token);
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
HttpURLConnection connection = makeRequest(url, "GET");
|
|
connection.setDoInput(true);
|
|
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);
|
|
cache.put(token, new AuthorizationEntryCache(entry));
|
|
return entry;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void addPolicies(List<Policy> policies) throws Exception {
|
|
final String methodPath = "/policyManager";
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath);
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
HttpURLConnection connection = makeRequest(url, "POST");
|
|
connection.setDoOutput(true);
|
|
|
|
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
|
|
jaxbContext.createMarshaller().marshal(new Policies(policies), os);
|
|
}
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error adding policies");
|
|
|
|
}
|
|
|
|
@Override
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
public List<Policy> getPolicies(String context) throws Exception{
|
|
final String methodPath = "/policyManager/";
|
|
|
|
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context);
|
|
|
|
URL url = new URL(callUrl.toString());
|
|
HttpURLConnection connection = makeRequest(url, "GET");
|
|
|
|
if (connection.getResponseCode()!=200) throw new Exception("error retrieving policies");
|
|
if (connection.getContentLengthLong()<=0) return null;
|
|
|
|
try(InputStream stream = (InputStream)connection.getContent()){
|
|
Policies policies = (Policies)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
return policies.getPolicies();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private HttpURLConnection makeRequest(URL url, String method) throws Exception{
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
connection.setRequestProperty(Constants.SCOPE_HEADER_ENTRY, ScopeProvider.instance.get());
|
|
connection.setRequestMethod(method);
|
|
return connection;
|
|
}
|
|
|
|
@Override
|
|
public AuthorizationEndpoint getEndpoint() {
|
|
return this.endpoints.get(0);
|
|
}
|
|
|
|
@Override
|
|
public void setEndpoint(AuthorizationEndpoint authEndpoint) {
|
|
this.endpoints = Collections.singletonList(authEndpoint);
|
|
}
|
|
|
|
|
|
}
|