Implemented getEndpoint method

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-access/gcube-geoserver-connector@149097 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
pasquale.vitale 2017-05-25 16:03:00 +00:00
parent 161d45c40e
commit 976b9bcef7
1 changed files with 108 additions and 55 deletions

View File

@ -1,27 +1,41 @@
package org.gcube.data.access.connector;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.codec.binary.Base64;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.access.connector.rest.GCubeRestClient;
import org.gcube.data.access.connector.rest.entity.AccessibleCredentialsEntity;
import org.gcube.data.access.connector.utils.AuthenticationUtils;
import org.gcube.data.access.connector.utils.GCubeCache;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.smartgears.handlers.application.RequestEvent;
import org.gcube.smartgears.handlers.application.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;;
import org.springframework.util.StringUtils;
import static org.gcube.common.authorization.client.Constants.authorizationService;
@XmlRootElement (name= GeoServerConnectorRequestHandler.REQUEST_HANDLER_NAME)
@XmlRootElement(name = GeoServerConnectorRequestHandler.REQUEST_HANDLER_NAME)
public class GeoServerConnectorRequestHandler extends RequestHandler {
protected static final String REQUEST_HANDLER_NAME = "authentication-filter";
private Logger logger;
private GCubeCache<String, String> gCubeCache;
public GeoServerConnectorRequestHandler() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.gCubeCache = new GCubeCache<>(AuthenticationUtils.TIME_TO_LIVE, AuthenticationUtils.TIMER_INTERVAL,
AuthenticationUtils.MAX_ITEMS_CACHE);
}
@Override
@ -31,76 +45,115 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
@Override
public void handleRequest(RequestEvent e) {
this.logger.debug("Handling request");
System.out.println("Handling request");
HttpServletRequest httpServletRequest = e.request();
// get host from ApplicationContext
String host = e.context().container().configuration().hostname();
// get token from request
String token = gCubeCache.get(AuthenticationUtils.TOKEN_CACHE);
System.out.println("Get Token from cache " + token);
if (!StringUtils.hasText(token)) {
System.out.println("Retrieve Token");
token = getToken(httpServletRequest);
System.out.println("Put Token in the cache");
gCubeCache.put(AuthenticationUtils.TOKEN_CACHE, token);
}
System.out.println("Token: " + token);
//get host from ApplicationContext
String host = e.context().container().configuration().hostname();
//get token from request
String token = getToken(httpServletRequest);
//get endpoint
String endpoint = getEndpoint();
//correct URL
//https://sdi-d-d4s.d4science.org/sdi-service/gcube/service/GeoServer/credentials/geoserver1-d-d4s.d4science.org?gcube-token=feda0617-cd9d-4841-b6f0-e047da5d32ed-98187548
System.out.println("Call to REST client");
this.logger.info("Call to REST client");
// get endpoint
String endpoint = getEndpoint(token) + "/GeoServer/credentials/";
System.out.println("endpoint: " + endpoint);
GCubeRestClient restClient = new GCubeRestClient();
AccessibleCredentialsEntity accessibleCredentials = restClient.getAccessibleCredentials(endpoint, host, token);
System.out.println("accessibleCredentials: " + accessibleCredentials.getUsername() + " " + accessibleCredentials.getPassword());
//TODO bypass username/password - I'm waiting they update the geoserver credentials
httpServletRequest.setAttribute(AuthenticationUtils.USERNAME, "admin");
httpServletRequest.setAttribute(AuthenticationUtils.PASSWORD, "geoserver");
// httpServletRequest.setAttribute(AuthenticationUtils.USERNAME, accessibleCredentials.getUsername());
// httpServletRequest.setAttribute(AuthenticationUtils.PASSWORD, accessibleCredentials.getPassword());
String url = endpoint + host + "?" + AuthenticationUtils.GCUBE_QUERY_STRING + "=" + token;
AccessibleCredentialsEntity accessibleCredentials = restClient.getAccessibleCredentials(url);
System.out.println("accessibleCredentials: " + accessibleCredentials.getUsername() + " " + accessibleCredentials.getPassword());
httpServletRequest.setAttribute(AuthenticationUtils.USERNAME, accessibleCredentials.getUsername());
httpServletRequest.setAttribute(AuthenticationUtils.PASSWORD, accessibleCredentials.getPassword());
// httpServletRequest.setAttribute(AuthenticationUtils.USERNAME, "admin");
// httpServletRequest.setAttribute(AuthenticationUtils.PASSWORD, "geoserver");
}
@Override
public String toString() {
return getName();
}
// retrieve the Token from request
private String getToken(HttpServletRequest httpServletRequest) {
//case 1 - get token from gcube-token query-string
String gCubeToken = httpServletRequest.getParameter(AuthenticationUtils.GCUBE_TOKEN);
if (StringUtils.hasText(gCubeToken)){
// case 1 - get token from gcube-token query-string
String gCubeToken = httpServletRequest.getParameter(AuthenticationUtils.GCUBE_QUERY_STRING);
if (StringUtils.hasText(gCubeToken)) {
System.out.println("Get token from query-string");
return gCubeToken;
}
//case 2 - get token from gcube-token header
gCubeToken = httpServletRequest.getHeader(AuthenticationUtils.GCUBE_TOKEN);
if (StringUtils.hasText(gCubeToken)){
// case 2 - get token from gcube-token header
gCubeToken = httpServletRequest.getHeader(AuthenticationUtils.GCUBE_QUERY_STRING);
if (StringUtils.hasText(gCubeToken)) {
System.out.println("Get token from gcube-token header");
return gCubeToken;
}
//case 3 - get token from Authorization header
String authorization = httpServletRequest.getHeader(AuthenticationUtils.AUTHORIZATION);
if (StringUtils.hasText(authorization) && StringUtils.startsWithIgnoreCase(authorization, AuthenticationUtils.BASIC)) {
System.out.println("Get token from authorization header");
// header = Authorization: Basic base64credentials
String base64Credentials = StringUtils.delete(authorization, AuthenticationUtils.BASIC);
String credentials = new String(Base64.decodeBase64(StringUtils.trimWhitespace(base64Credentials)));
// credentials = username:token
final String[] values = credentials.split(":",2);
return values[1];
}
System.out.println("Token not found in gcube-token query string, in gcube-token header and in basic authorization header");
// case 3 - get token from basic authorization header
String authorization = httpServletRequest.getHeader(AuthenticationUtils.AUTHORIZATION);
if (StringUtils.hasText(authorization)
&& StringUtils.startsWithIgnoreCase(authorization, AuthenticationUtils.BASIC)) {
System.out.println("Get token from basic authorization header");
// header = Authorization: Basic base64credentials
String base64Credentials = StringUtils.delete(authorization, AuthenticationUtils.BASIC);
String credentials = new String(Base64.decodeBase64(StringUtils.trimWhitespace(base64Credentials)));
// credentials = username:token
final String[] values = credentials.split(":", 2);
return values[1];
}
System.out.println("Token not found in gcube-token query-string, in gcube-token header and in basic authorization header");
return null;
}
private String getEndpoint() {
return "https://sdi-d-d4s.d4science.org/sdi-service/gcube/service/GeoServer/credentials/";
private String getEndpoint(String token) {
try {
AuthorizationEntry authorizationEntry = authorizationService().get(token);
String scope = authorizationEntry.getContext();
ScopeProvider.instance.set(scope);
System.out.println("Set scope to " + ScopeProvider.instance.get());
SecurityTokenProvider.instance.set(token);
String serviceClass = String.format("$resource/Profile/ServiceClass/text() eq '%s'", "SDI");
String serviceName = String.format("$resource/Profile/ServiceName/text() eq '%s'", "sdi-service");
SimpleQuery query = ICFactory.queryFor(GCoreEndpoint.class).addCondition(serviceClass).addCondition(serviceName);
DiscoveryClient<GCoreEndpoint> client = ICFactory.clientFor(GCoreEndpoint.class);
List<GCoreEndpoint> gCoreEndpoints = client.submit(query);
if (gCoreEndpoints.size() > 1) {
System.out.println("SIZE = " + gCoreEndpoints.size());
GCoreEndpoint gCoreEndpoint = gCoreEndpoints.get(gCoreEndpoints.size()-1);
// Map<String, Endpoint> map = gCoreEndpoint.profile().endpointMap();
// return map.get("org.gcube.spatial.data.sdi.SDIService").uri().toString();
return gCoreEndpoint.profile().endpointMap().get("org.gcube.spatial.data.sdi.SDIService").uri().toString();
}
} catch (Exception ex) {
System.out.println("Error in getEndpoint: " + ex.getMessage());
}
//return "https://sdi-d-d4s.d4science.org/sdi-service/gcube/service/GeoServer/credentials/";
return null;
}
}