Lucio Lelii 2015-11-27 17:06:23 +00:00
parent a1d1770cb2
commit 839a4e8403
6 changed files with 233 additions and 2 deletions

View File

@ -22,6 +22,11 @@
<artifactId>common-scope</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-configuration-scanner</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -7,6 +7,8 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.scope.api.ServiceMap;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorizationEntry {
@ -14,6 +16,7 @@ public class AuthorizationEntry {
private String clientId;
private List<String> roles;
private String context;
private ServiceMap map;
private List<CalledService> bannedServices = new ArrayList<CalledService>();
protected AuthorizationEntry(){}
@ -25,8 +28,8 @@ public class AuthorizationEntry {
this.context = context;
}
public AuthorizationEntry(String userName, List<String> roles, String scope, List<CalledService> bannedServices) {
this(userName, roles, scope);
public AuthorizationEntry(String userName, List<String> roles, String context , List<CalledService> bannedServices) {
this(userName, roles, context);
this.bannedServices = bannedServices;
}
@ -41,7 +44,17 @@ public class AuthorizationEntry {
public String getContext() {
return context;
}
public ServiceMap getMap() {
return map;
}
public void setMap(ServiceMap map) {
this.map = map;
}
public List<CalledService> getBannedServices() {
return bannedServices;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1 @@
<auhtorization-endpoint priority="10"><host>146.48.85.179</host><port>8080</port></auhtorization-endpoint>

View File

@ -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);
}
}