70: Authetication Token
Task-Url: https://support.d4science.org/issues/70 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/authorization-common-client@114832 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
cbf6122576
commit
3558ff0bdb
2
pom.xml
2
pom.xml
|
@ -19,7 +19,7 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-library</artifactId>
|
||||
<artifactId>common-authorization</artifactId>
|
||||
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ import java.io.InputStream;
|
|||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.authorization.client.Binder;
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
|
@ -25,6 +25,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|||
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>() {
|
||||
|
@ -37,9 +39,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|||
connection.setRequestProperty(Constants.SCOPE_HEADER_ENTRY, ScopeProvider.instance.get());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while((line = reader.readLine()) != null)
|
||||
result.append(line);
|
||||
try{
|
||||
String line;
|
||||
while((line = reader.readLine()) != null)
|
||||
result.append(line);
|
||||
}finally{
|
||||
if (reader!=null)
|
||||
reader.close();
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
};
|
||||
|
@ -56,14 +63,22 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
|
|||
|
||||
@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;
|
||||
return (AuthorizationEntry)Binder.getContext().createUnmarshaller().unmarshal((InputStream)connection.getContent());
|
||||
|
||||
|
||||
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) {
|
||||
|
|
|
@ -31,4 +31,5 @@ public class CallTest {
|
|||
System.out.println("token is: "+token);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue