From 49ba67fc4ef46a37cd2c47a0eb13191d90ddec15 Mon Sep 17 00:00:00 2001 From: "lucio.lelii" Date: Mon, 18 May 2015 17:15:11 +0000 Subject: [PATCH] 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-library@114785 82a268e6-3cf1-43bd-a215-b396298e98cf --- .settings/org.eclipse.core.resources.prefs | 6 ++ .settings/org.eclipse.jdt.core.prefs | 12 +++ .settings/org.eclipse.m2e.core.prefs | 4 + distro/INSTALL | 1 + distro/LICENSE | 6 ++ distro/MAINTAINERS | 1 + distro/README | 38 ++++++++++ distro/changelog.xml | 5 ++ distro/descriptor.xml | 42 ++++++++++ distro/profile.xml | 26 +++++++ distro/svnpath.txt | 1 + pom.xml | 69 +++++++++++++++++ .../library/AuthorizationEntry.java | 44 +++++++++++ .../library/AuthorizationToken.java | 76 +++++++++++++++++++ .../provider/AuthorizationProvider.java | 27 +++++++ .../library/provider/UserInfo.java | 71 +++++++++++++++++ 16 files changed, 429 insertions(+) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 distro/INSTALL create mode 100644 distro/LICENSE create mode 100644 distro/MAINTAINERS create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/profile.xml create mode 100644 distro/svnpath.txt create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/common/authorization/library/AuthorizationEntry.java create mode 100644 src/main/java/org/gcube/common/authorization/library/AuthorizationToken.java create mode 100644 src/main/java/org/gcube/common/authorization/library/provider/AuthorizationProvider.java create mode 100644 src/main/java/org/gcube/common/authorization/library/provider/UserInfo.java diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6249222 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/INSTALL b/distro/INSTALL new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/distro/INSTALL @@ -0,0 +1 @@ + diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..630ba97 --- /dev/null +++ b/distro/LICENSE @@ -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. diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS new file mode 100644 index 0000000..6e1540b --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1 @@ +* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..6b65683 --- /dev/null +++ b/distro/README @@ -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. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..c9bca04 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,5 @@ + + + First Release + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..21d8c88 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,42 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + INSTALL + MAINTAINERS + changelog.xml + + 755 + true + + + + + ${distroDirectory}/profile.xml + / + true + + + target/${build.finalName}.jar + /${artifactId} + + + ${distroDirectory}/svnpath.txt + /${artifactId} + true + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..91c49e4 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,26 @@ + + + + Service + + ${description} + Common + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + + diff --git a/distro/svnpath.txt b/distro/svnpath.txt new file mode 100644 index 0000000..f416f9d --- /dev/null +++ b/distro/svnpath.txt @@ -0,0 +1 @@ +${scm.url} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..effb698 --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + 4.0.0 + org.gcube.common + authorization-library + 1.0.0-SNAPSHOT + authorization service common library + + + maven-parent + org.gcube.tools + 1.0.0 + + + + distro + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/common/authorization/library/AuthorizationEntry.java b/src/main/java/org/gcube/common/authorization/library/AuthorizationEntry.java new file mode 100644 index 0000000..4dda367 --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/AuthorizationEntry.java @@ -0,0 +1,44 @@ +package org.gcube.common.authorization.library; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class AuthorizationEntry { + + private String userName; + private String role; + private String scope; + + protected AuthorizationEntry(){} + + public AuthorizationEntry(String userName, String role, String scope) { + super(); + this.userName = userName; + this.role = role; + this.scope = scope; + } + + public String getUserName() { + return userName; + } + + public String getRole() { + return role; + } + + public String getScope() { + return scope; + } + + @Override + public String toString() { + return "AuthorizationEntry [userName=" + userName + ", role=" + role + + ", scope=" + scope + "]"; + } + + + +} diff --git a/src/main/java/org/gcube/common/authorization/library/AuthorizationToken.java b/src/main/java/org/gcube/common/authorization/library/AuthorizationToken.java new file mode 100644 index 0000000..1e59eab --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/AuthorizationToken.java @@ -0,0 +1,76 @@ +package org.gcube.common.authorization.library; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.HexBinaryAdapter; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class AuthorizationToken implements Serializable { + + private static HexBinaryAdapter hexAdapter = new HexBinaryAdapter(); + + /** + * + */ + private static final long serialVersionUID = 1L; + + private String user; + private String group = null; + + protected AuthorizationToken(){} + + public AuthorizationToken(String user){ + this.user = user; + } + + public AuthorizationToken(String user, String group){ + this.user = user; + this.group = group; + } + + /** + * @return the user + */ + public String getUser() { + return user; + } + + public String getGroup() { + return group; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "AuthorizationToken [user=" + user + "]"; + } + + public static String marshal(AuthorizationToken v) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(v); + oos.close(); + byte[] serializedBytes = baos.toByteArray(); + return hexAdapter.marshal(serializedBytes); + } + + public static AuthorizationToken unmarshal(String v) throws Exception { + byte[] serializedBytes = hexAdapter.unmarshal(v); + ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes); + ObjectInputStream ois = new ObjectInputStream(bais); + AuthorizationToken result = (AuthorizationToken) ois.readObject(); + bais.close(); + return result; + } + +} diff --git a/src/main/java/org/gcube/common/authorization/library/provider/AuthorizationProvider.java b/src/main/java/org/gcube/common/authorization/library/provider/AuthorizationProvider.java new file mode 100644 index 0000000..35e4eb3 --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/provider/AuthorizationProvider.java @@ -0,0 +1,27 @@ +package org.gcube.common.authorization.library.provider; + +public class AuthorizationProvider { + + public static AuthorizationProvider instance = new AuthorizationProvider(); + + // Thread local variable containing each thread's ID + private static final ThreadLocal threadAuth = + new ThreadLocal() { + + @Override protected UserInfo initialValue() { + return null; + } + + }; + + private AuthorizationProvider(){} + + public UserInfo get(){ + return threadAuth.get(); + } + + public void set(UserInfo authorizationToken){ + threadAuth.set(authorizationToken); + } + +} diff --git a/src/main/java/org/gcube/common/authorization/library/provider/UserInfo.java b/src/main/java/org/gcube/common/authorization/library/provider/UserInfo.java new file mode 100644 index 0000000..8d39216 --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/library/provider/UserInfo.java @@ -0,0 +1,71 @@ +package org.gcube.common.authorization.library.provider; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class UserInfo { + + private String userName; + private String role; + + protected UserInfo(){} + + public UserInfo(String userName, String role) { + super(); + this.userName = userName; + this.role = role; + } + + public String getUserName() { + return userName; + } + public String getRole() { + return role; + } + + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((role == null) ? 0 : role.hashCode()); + result = prime * result + + ((userName == null) ? 0 : userName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserInfo other = (UserInfo) obj; + if (role == null) { + if (other.role != null) + return false; + } else if (!role.equals(other.role)) + return false; + if (userName == null) { + if (other.userName != null) + return false; + } else if (!userName.equals(other.userName)) + return false; + return true; + } + + @Override + public String toString() { + return "UserInfo [userName=" + userName + ", role=" + role + "]"; + } + + + +}