119 lines
4.3 KiB
Java
119 lines
4.3 KiB
Java
package org.gcube.portal.auth.library;
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
|
import org.gcube.common.resources.gcore.utils.Group;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
*
|
|
* @author Massimiliano Assante, CNR-ISTI
|
|
*
|
|
*/
|
|
public class AuthUtil {
|
|
private static final Logger _log = LoggerFactory.getLogger(AuthUtil.class);
|
|
|
|
final String REDIRECT_URL = "RedirectURL";
|
|
final String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
|
|
final String LOGOURL_ATTR = "Logo";
|
|
|
|
public List<ServiceEndpoint> getPortalConfigurationFromIS(String infrastructureName, String clientId)
|
|
throws Exception {
|
|
String scope = "/" + infrastructureName;
|
|
String currScope = ScopeProvider.instance.get();
|
|
ScopeProvider.instance.set(scope);
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
|
query.addCondition("$resource/Profile/Category/text() eq '" + SERVICE_ENDPOINT_CATEGORY + "'");
|
|
query.addCondition("$resource/Profile/Name/text() eq '" + clientId + "'");
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
List<ServiceEndpoint> toReturn = client.submit(query);
|
|
ScopeProvider.instance.set(currScope);
|
|
return toReturn;
|
|
}
|
|
|
|
/**
|
|
* look for the clientId AccessEndpoint passes as parameter
|
|
* @param clientId
|
|
* @return a <code>RequestingApp</code> contanining the application name, the description and the application logo URL if any, or null if non existent
|
|
*/
|
|
private RequestingApp getAuthorisedApplicationInfoFromIs(String clientId) {
|
|
RequestingApp toReturn = new RequestingApp();
|
|
String infraName = PortalContext.getConfiguration().getInfrastructureName();
|
|
System.out.println("infraName="+infraName);
|
|
|
|
try {
|
|
List<ServiceEndpoint> list = getPortalConfigurationFromIS(infraName, clientId);
|
|
if (list.size() > 1) {
|
|
_log.error("Too many Service Endpoints having name " + clientId +" in this scope having Category " + SERVICE_ENDPOINT_CATEGORY);
|
|
return null;
|
|
} else if (list.size() == 0) {
|
|
_log.warn("There is no Service Endpoint having name " + clientId +" and Category " + SERVICE_ENDPOINT_CATEGORY + " in this scope: /" + infraName);
|
|
return null;
|
|
} else {
|
|
for (ServiceEndpoint res : list) {
|
|
toReturn.setApplicationId(res.profile().name());
|
|
Group<AccessPoint> apGroup = res.profile().accessPoints();
|
|
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
|
|
AccessPoint found = accessPoints[0];
|
|
for (Property prop : found.properties()) {
|
|
if (prop.name().compareTo(LOGOURL_ATTR) == 0) {
|
|
toReturn.setLogoURL(prop.value());
|
|
return toReturn;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
return toReturn;
|
|
}
|
|
|
|
/**
|
|
* construct a map for getting attributes from the quiery string
|
|
* @param redirectionURL the string in redirect
|
|
* @return a map containing the attributes, takes into account escaped characters
|
|
*/
|
|
private static Map<String, String> getQueryMap(String redirectionURL) {
|
|
String decodedURL = "";
|
|
try {
|
|
decodedURL = java.net.URLDecoder.decode(redirectionURL, "UTF-8");
|
|
} catch (UnsupportedEncodingException e) {
|
|
System.out.println("UnsupportedEncodingException=" + e.getMessage());
|
|
return new HashMap<String, String>();
|
|
}
|
|
String[] url = decodedURL.split("\\?");
|
|
if (url == null || url.length < 2)
|
|
return new HashMap<String, String>();
|
|
final String query = url[1];
|
|
String[] params = query.split("&");
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
for (String param : params) {
|
|
String name = param.split("=")[0];
|
|
String value = param.split("=")[1];
|
|
map.put(name, value);
|
|
}
|
|
return map;
|
|
}
|
|
}
|