This commit is contained in:
Lucio Lelii 2015-12-22 18:38:51 +00:00
parent 428424f82b
commit 41ab4bf99f
5 changed files with 37 additions and 34 deletions

View File

@ -29,12 +29,6 @@
<version>[1.0.2-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -12,6 +12,7 @@ public interface AuthorizationProxy {
void setEndpoint(AuthorizationEndpoint endpoint);
@Deprecated
String generate(String userName, List<String> roles) throws Exception;
AuthorizationEntry get(String token) throws ObjectNotFound, Exception;

View File

@ -8,7 +8,9 @@ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -22,29 +24,45 @@ import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint;
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner;
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 List<AuthorizationEndpoint> endpoints;
public DefaultAuthorizationProxy() {
endpoints = AuthorizationEndpointScanner.endpoints();
AuthorizationEndpoint ae = endpoints.get(0);
try{
InetAddress addr = InetAddress.getByName(ae.getHost());
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()){
ae.setHost("localhost");
this.setEndpoint(ae);
}
log.debug("endpoint set to localhost");
}catch(UnknownHostException e){
log.warn("unknown host", e);
}
}
private String getInternalEnpoint(){
StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":")
.append(getEndpoint().getPort()).append("/authorization-service/gcube/service");
return endpoint.toString();
}
@Deprecated
@Override
public String generate(String clientId, List<String> roles) throws Exception {
final String context = ScopeProvider.instance.get();
return this.generate(clientId, context , roles);
}
@ -67,8 +85,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
URL url = new URL(callUrl.toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service");
String encryptedToken= "";
try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()));){
@ -78,21 +96,24 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
result.append(line);
encryptedToken = result.toString();
}
return StringEncrypter.getEncrypter().decrypt(encryptedToken);
return StringEncrypter.getEncrypter().decrypt(encryptedToken, context);
}
@Override
public AuthorizationEntry get(final String token) throws ObjectNotFound, Exception{
final String methodPath = "/retrieve/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token);
log.debug("call uri "+callUrl.toString());
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET");
log.debug("response code is "+connection.getResponseCode());
log.debug("response message is "+connection.getResponseMessage());
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;

View File

@ -13,7 +13,8 @@ public class CallTest {
@Test
public void call() throws Exception{
try{
System.out.println(authorizationService().get("a00affeb-0b75-4152-a134-e5c432a9a70a"));
//devsec cec80de1-0e1a-47be-81cd-e8534753bff7
System.out.println(authorizationService().get("870f409b-df3c-4c12-8063-6f9b0f414751"));
}catch(ObjectNotFound onf){
onf.printStackTrace();
}
@ -22,8 +23,8 @@ public class CallTest {
@Test
public void requestToken() throws Exception {
ScopeProvider.instance.set("/gcube");
String token = authorizationService().generate("fabio.sinibaldi", new ArrayList<String>());
//ScopeProvider.instance.set("/gcube/devsec");
String token = authorizationService().generate("lucio.lelii", "/gcube", new ArrayList<String>());
System.out.println("token is: "+token);
}

View File

@ -1,14 +0,0 @@
package org.gcube.common.authorizationservice.cl;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.Arrays;
public class MainCall {
public static void main(String... args) throws Exception{
String token = authorizationService().generate("fabio.sinibaldi", Arrays.asList("User"));
System.out.println("token is: "+token);
}
}