133 lines
4.0 KiB
Java
133 lines
4.0 KiB
Java
package org.gcube.common.authorization.client.proxy;
|
|
|
|
import static org.gcube.common.clients.exceptions.FaultDSL.again;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.common.authorization.client.Binder;
|
|
import org.gcube.common.authorization.client.Constants;
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
import org.gcube.common.authorization.library.provider.Service;
|
|
import org.gcube.common.clients.Call;
|
|
import org.gcube.common.clients.delegates.ProxyDelegate;
|
|
import org.gcube.common.clients.stubs.jaxws.JAXWSUtils.Empty;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|
|
|
private final ProxyDelegate<String> delegate;
|
|
|
|
public DefaultAuthorizationProxy(ProxyDelegate<String> config){
|
|
this.delegate = config;
|
|
}
|
|
|
|
private static Map<String, AuthorizationEntry> cache = new HashMap<String, AuthorizationEntry>();
|
|
|
|
@Override
|
|
public String generate(final String userName, final String role) {
|
|
Call<String, String> call = new Call<String, String>() {
|
|
|
|
@Override
|
|
public String call(String endpoint) throws Exception {
|
|
URL url = new URL(endpoint+"/generate/"+userName+"/"+role);
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setRequestProperty(Constants.SCOPE_HEADER_ENTRY, ScopeProvider.instance.get());
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()));
|
|
StringBuilder result = new StringBuilder();
|
|
try{
|
|
String line;
|
|
while((line = reader.readLine()) != null)
|
|
result.append(line);
|
|
}finally{
|
|
if (reader!=null)
|
|
reader.close();
|
|
}
|
|
|
|
return result.toString();
|
|
}
|
|
};
|
|
try {
|
|
return delegate.make(call);
|
|
} catch (Exception e) {
|
|
throw again(e).asServiceException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public AuthorizationEntry get(final String token) {
|
|
Call<String, AuthorizationEntry> call = new Call<String, AuthorizationEntry>() {
|
|
|
|
@Override
|
|
public AuthorizationEntry call(String endpoint) throws Exception {
|
|
|
|
URL url = new URL(endpoint+"/retrieve/"+token);
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
if (connection.getContentLengthLong()<=0) return null;
|
|
|
|
try(InputStream stream = (InputStream)connection.getContent();){
|
|
AuthorizationEntry entry = (AuthorizationEntry)Binder.getContext().createUnmarshaller().unmarshal(stream);
|
|
cache.put(token, entry);
|
|
return entry;
|
|
}
|
|
|
|
}
|
|
};
|
|
if (cache.containsKey(token))
|
|
return cache.get(token);
|
|
try {
|
|
return delegate.make(call);
|
|
} catch (Exception e) {
|
|
throw again(e).asServiceException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void deny(final String userName, final Service service) {
|
|
Call<String, Empty> call = new Call<String, Empty>() {
|
|
@Override
|
|
public Empty call(String endpoint) throws Exception {
|
|
URL url = new URL(endpoint+"/deny/add/"+userName+"/"+service.getServiceClass()+"/"+service.getServiceName());
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
return new Empty();
|
|
}
|
|
};
|
|
|
|
try {
|
|
delegate.make(call);
|
|
} catch (Exception e) {
|
|
throw again(e).asServiceException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void allow(final String userName, final Service service) {
|
|
Call<String, Empty> call = new Call<String, Empty>() {
|
|
|
|
@Override
|
|
public Empty call(String endpoint) throws Exception {
|
|
URL url = new URL(endpoint+"/deny/remove/"+userName+"/"+service.getServiceClass()+"/"+service.getServiceName());
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
return new Empty();
|
|
|
|
}
|
|
};
|
|
|
|
try {
|
|
delegate.make(call);
|
|
} catch (Exception e) {
|
|
throw again(e).asServiceException();
|
|
}
|
|
}
|
|
|
|
}
|