git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/authorization-common-library@120459 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
a1d1770cb2
commit
839a4e8403
5
pom.xml
5
pom.xml
|
@ -22,6 +22,11 @@
|
||||||
<artifactId>common-scope</artifactId>
|
<artifactId>common-scope</artifactId>
|
||||||
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.core</groupId>
|
||||||
|
<artifactId>common-configuration-scanner</artifactId>
|
||||||
|
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|
|
@ -7,6 +7,8 @@ import javax.xml.bind.annotation.XmlAccessType;
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
import org.gcube.common.scope.api.ServiceMap;
|
||||||
|
|
||||||
@XmlRootElement
|
@XmlRootElement
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
public class AuthorizationEntry {
|
public class AuthorizationEntry {
|
||||||
|
@ -14,6 +16,7 @@ public class AuthorizationEntry {
|
||||||
private String clientId;
|
private String clientId;
|
||||||
private List<String> roles;
|
private List<String> roles;
|
||||||
private String context;
|
private String context;
|
||||||
|
private ServiceMap map;
|
||||||
private List<CalledService> bannedServices = new ArrayList<CalledService>();
|
private List<CalledService> bannedServices = new ArrayList<CalledService>();
|
||||||
|
|
||||||
protected AuthorizationEntry(){}
|
protected AuthorizationEntry(){}
|
||||||
|
@ -25,8 +28,8 @@ public class AuthorizationEntry {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthorizationEntry(String userName, List<String> roles, String scope, List<CalledService> bannedServices) {
|
public AuthorizationEntry(String userName, List<String> roles, String context , List<CalledService> bannedServices) {
|
||||||
this(userName, roles, scope);
|
this(userName, roles, context);
|
||||||
this.bannedServices = bannedServices;
|
this.bannedServices = bannedServices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +45,16 @@ public class AuthorizationEntry {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ServiceMap getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void setMap(ServiceMap map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
public List<CalledService> getBannedServices() {
|
public List<CalledService> getBannedServices() {
|
||||||
return bannedServices;
|
return bannedServices;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package org.gcube.common.authorization.library.enpoints;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlAccessType;
|
||||||
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name="auhtorization-endpoint")
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
public final class AuthorizationEndpoint implements Comparable<AuthorizationEndpoint>{
|
||||||
|
|
||||||
|
@XmlAttribute
|
||||||
|
private int priority = 0;
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
protected AuthorizationEndpoint() {}
|
||||||
|
|
||||||
|
public AuthorizationEndpoint(int priority, String host, int port) {
|
||||||
|
super();
|
||||||
|
this.host = host;
|
||||||
|
this.priority = priority;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(AuthorizationEndpoint o) {
|
||||||
|
return this.priority-o.priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "AuthorizationEndpoint [priority=" + priority + ", host=" + host
|
||||||
|
+ ", port=" + port + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((host == null) ? 0 : host.hashCode());
|
||||||
|
result = prime * result + port;
|
||||||
|
result = prime * result + priority;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
AuthorizationEndpoint other = (AuthorizationEndpoint) obj;
|
||||||
|
if (host == null) {
|
||||||
|
if (other.host != null)
|
||||||
|
return false;
|
||||||
|
} else if (!host.equals(other.host))
|
||||||
|
return false;
|
||||||
|
if (port != other.port)
|
||||||
|
return false;
|
||||||
|
if (priority != other.priority)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package org.gcube.common.authorization.library.enpoints;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
|
import org.gcube.common.scan.ClasspathScanner;
|
||||||
|
import org.gcube.common.scan.ClasspathScannerFactory;
|
||||||
|
import org.gcube.common.scan.matchers.NameMatcher;
|
||||||
|
import org.gcube.common.scan.resources.ClasspathResource;
|
||||||
|
import org.gcube.common.scope.api.ServiceMap;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AuthorizationEndpointScanner {
|
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(AuthorizationEndpointScanner.class);
|
||||||
|
|
||||||
|
private static List<AuthorizationEndpoint> endpoints;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path used to find service map configuration files.
|
||||||
|
*/
|
||||||
|
static final String configurationPattern = "authorization-endpoint.xml";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scans the classpath for {@link ServiceMap}s.
|
||||||
|
*/
|
||||||
|
public static synchronized List<AuthorizationEndpoint> endpoints() {
|
||||||
|
|
||||||
|
if (endpoints==null || endpoints.size()==0){
|
||||||
|
endpoints = new ArrayList<AuthorizationEndpoint>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
JAXBContext context = JAXBContext.newInstance(AuthorizationEndpoint.class);
|
||||||
|
Unmarshaller um = context.createUnmarshaller();
|
||||||
|
|
||||||
|
ClasspathScanner scanner = ClasspathScannerFactory.scanner();
|
||||||
|
for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern))){
|
||||||
|
AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(r.stream());
|
||||||
|
endpoints.add(endpoint);
|
||||||
|
log.info("loaded endpoint {} ",endpoint.toString());
|
||||||
|
}
|
||||||
|
if (endpoints.size()==0)
|
||||||
|
throw new Exception("no endpoints retreived");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("could not load authorization endpoints", e);
|
||||||
|
}
|
||||||
|
Collections.sort(endpoints);
|
||||||
|
}
|
||||||
|
return endpoints;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
<auhtorization-endpoint priority="10"><host>146.48.85.179</host><port>8080</port></auhtorization-endpoint>
|
|
@ -0,0 +1,64 @@
|
||||||
|
package org.gcube.common.authorization.library.binder;
|
||||||
|
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint;
|
||||||
|
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EndpointBinder {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static JAXBContext getContext() throws JAXBException{
|
||||||
|
return JAXBContext.newInstance(AuthorizationEndpoint.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bind() throws Exception{
|
||||||
|
JAXBContext context = getContext();
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
AuthorizationEndpoint ae1 = new AuthorizationEndpoint(2, "146.48.85.179", 8080);
|
||||||
|
context.createMarshaller().marshal(ae1, sw);
|
||||||
|
AuthorizationEndpoint ae2 = (AuthorizationEndpoint)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));
|
||||||
|
Assert.assertEquals(ae1, ae2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void compare(){
|
||||||
|
AuthorizationEndpoint ae1 = new AuthorizationEndpoint(2, "146.48.85.179", 8080);
|
||||||
|
AuthorizationEndpoint ae2 = new AuthorizationEndpoint(1, "146.48.85.179", 8080);
|
||||||
|
|
||||||
|
Assert.assertTrue(ae1.compareTo(ae2)>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void order(){
|
||||||
|
AuthorizationEndpoint ae1 = new AuthorizationEndpoint(2, "146.48.85.179", 8080);
|
||||||
|
AuthorizationEndpoint ae2 = new AuthorizationEndpoint(1, "146.48.85.179", 8080);
|
||||||
|
AuthorizationEndpoint ae3 = new AuthorizationEndpoint(3, "146.48.85.179", 8080);
|
||||||
|
|
||||||
|
AuthorizationEndpoint[] arr1 = new AuthorizationEndpoint[]{ae1, ae2, ae3};
|
||||||
|
|
||||||
|
Arrays.sort(arr1);
|
||||||
|
|
||||||
|
AuthorizationEndpoint[] arr2 = new AuthorizationEndpoint[]{ae2, ae1, ae3};
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(arr1, arr2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void scan(){
|
||||||
|
List<AuthorizationEndpoint> endpoints = AuthorizationEndpointScanner.endpoints();
|
||||||
|
System.out.println(endpoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue