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@114787 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Lucio Lelii 2015-05-18 17:15:58 +00:00
parent 0003d28d30
commit cbf6122576
18 changed files with 536 additions and 0 deletions

1
distro/INSTALL Normal file
View File

@ -0,0 +1 @@

6
distro/LICENSE Normal file
View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

1
distro/MAINTAINERS Normal file
View File

@ -0,0 +1 @@
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy

38
distro/README Normal file
View File

@ -0,0 +1,38 @@
The gCube System - ${name}
----------------------
This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2), D4Science (FP7-INFRA-2007-1.2.2),
D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2), and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil).
Authors
-------
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
Version and Release Date
------------------------
${version}
Description
-----------
${description}
Download information
--------------------
Source code is available from SVN:
${scm.url}
Binaries can be downloaded from:
Documentation
-------------
Documentation is available on-line from the Projects Documentation Wiki:
https://gcube.wiki.gcube-system.org/gcube/index.php/....
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

5
distro/changelog.xml Normal file
View File

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="authorization-client-1.0.0" date="2015-05-18">
<Change>First Release</Change>
</Changeset>
</ReleaseNotes>

42
distro/descriptor.xml Normal file
View File

@ -0,0 +1,42 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>INSTALL</include>
<include>MAINTAINERS</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>${distroDirectory}/profile.xml</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>

26
distro/profile.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID />
<Type>Service</Type>
<Profile>
<Description>${description}</Description>
<Class>Common</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

1
distro/svnpath.txt Normal file
View File

@ -0,0 +1 @@
${scm.url}

105
pom.xml Normal file
View File

@ -0,0 +1,105 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>authorization service client library</name>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.0.0</version>
</parent>
<properties>
<distroDirectory>distro</distroDirectory>
</properties>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-library</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,19 @@
package org.gcube.common.authorization.client;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.gcube.common.authorization.library.AuthorizationEntry;
public class Binder {
private static JAXBContext context ;
public static JAXBContext getContext() throws JAXBException{
if (context==null)
context = JAXBContext.newInstance(AuthorizationEntry.class);
return context;
}
}

View File

@ -0,0 +1,33 @@
package org.gcube.common.authorization.client;
import java.util.concurrent.TimeUnit;
import javax.xml.namespace.QName;
import org.gcube.common.authorization.client.plugin.AuthorizationPlugin;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
import org.gcube.common.clients.ProxyBuilder;
import org.gcube.common.clients.ProxyBuilderImpl;
public class Constants {
/** Service name. */
public static final String SERVICE_NAME = "AuthorizationService";
/** Service class. */
public static final String SERVICE_CLASS = "Common";
public static final String CONTEXT_SERVICE_NAME="authorization-service";
public static final int DEFAULT_TIMEOUT= (int) TimeUnit.SECONDS.toMillis(10);
private static final String TNS = "http://gcube-system.org/";
public static final QName AUTHORIZATION_QNAME = new QName(TNS, "authorization-service");
public static final String SCOPE_HEADER_ENTRY = "gcube-scope";
public static ProxyBuilder<AuthorizationProxy> authorizationService() {
return new ProxyBuilderImpl<String,AuthorizationProxy>(new AuthorizationPlugin());
}
}

View File

@ -0,0 +1,60 @@
package org.gcube.common.authorization.client;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.EndpointReference;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class JaxRSEndpointReference {
private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
private static final String addressLocalName = "Address";
//private static final String keyLocalName = "ResourceKey";
String address;
//Element key;
static {
factory.setNamespaceAware(true);
}
public JaxRSEndpointReference(EndpointReference reference) {
this(serialise(reference));
}
public JaxRSEndpointReference(String reference) {
try {
Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(reference)));
NodeList addresses = document.getElementsByTagNameNS("*", addressLocalName);
if (addresses.getLength() == 0)
throw new RuntimeException("reference does not contain an address");
address = addresses.item(0).getTextContent();
} catch (Exception e) {
throw new IllegalArgumentException("reference is not a gCore reference", e);
}
}
@Override
public String toString() {
return address;
}
// helper
private static String serialise(EndpointReference reference) {
StringWriter writer = new StringWriter();
reference.writeTo(new StreamResult(writer));
return writer.toString();
}
}

View File

@ -0,0 +1,9 @@
package org.gcube.common.authorization.client;
public class Utils {
static void notNull(String message,Object o) {
if (o==null)
throw new IllegalArgumentException(o+" cannot be null");
}
}

View File

@ -0,0 +1,37 @@
package org.gcube.common.authorization.client.plugin;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.clients.Plugin;
public abstract class AbstractPlugin<S,P> implements Plugin<S,P> {
public final String name;
public AbstractPlugin(String name) {
this.name=name;
}
@Override
public String serviceClass() {
return Constants.SERVICE_CLASS;
}
@Override
public String serviceName() {
return Constants.SERVICE_NAME;
}
@Override
public String name() {
return name;
}
@Override
public String namespace() {
return "";
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.common.authorization.client.plugin;
import javax.xml.ws.EndpointReference;
import org.gcube.common.authorization.client.JaxRSEndpointReference;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
import org.gcube.common.authorization.client.proxy.DefaultAuthorizationProxy;
import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate;
public class AuthorizationPlugin extends AbstractPlugin<String, AuthorizationProxy>{
public AuthorizationPlugin() {
super("authorization-service/gcube/service");
}
@Override
public Exception convert(Exception fault, ProxyConfig<?, ?> config) {
return fault;
}
@Override
public String resolve(EndpointReference address, ProxyConfig<?, ?> config)
throws Exception {
return new JaxRSEndpointReference(address).toString();
}
@Override
public AuthorizationProxy newProxy(ProxyDelegate<String> delegate) {
return new DefaultAuthorizationProxy(delegate);
}
}

View File

@ -0,0 +1,10 @@
package org.gcube.common.authorization.client.proxy;
import org.gcube.common.authorization.library.AuthorizationEntry;
public interface AuthorizationProxy {
String generate(String userName, String role);
AuthorizationEntry get(String token);
}

View File

@ -0,0 +1,74 @@
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 javax.xml.bind.JAXBContext;
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.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate;
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;
}
@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();
String line;
while((line = reader.readLine()) != null)
result.append(line);
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;
return (AuthorizationEntry)Binder.getContext().createUnmarshaller().unmarshal((InputStream)connection.getContent());
}
};
try {
return delegate.make(call);
} catch (Exception e) {
throw again(e).asServiceException();
}
}
}

View File

@ -0,0 +1,34 @@
package org.gcube.common.authorizationservice.cl;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.Test;
public class CallTest {
@Test
public void call(){
ScopeProvider.instance.set("/gcube/devsec");
String token = authorizationService().build().generate("lucio.lelii", "God");
System.out.println("token is "+token);
AuthorizationEntry entry = authorizationService().build().get(token);
System.out.println("entry is "+entry.toString());
}
@Test
public void requestToken(){
ScopeProvider.instance.set("/gcube/devsec");
String token = authorizationService().build().generate("lucio.lelii", "God");
System.out.println("token is: "+token);
}
}