completed implementation for Feature #5143: smartgears service authentication portlet

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/my-vres@141536 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-01-12 17:48:06 +00:00
parent 2e7e33d4be
commit dd69469e4e
9 changed files with 309 additions and 79 deletions

View File

@ -0,0 +1,32 @@
package org.gcube.portlet.user.my_vres.client;
public class GetParameters {
String redirectURI;
String state;
String context;
public GetParameters(String redirectURI, String state, String context) {
super();
this.redirectURI = redirectURI;
this.state = state;
this.context = context;
}
public String getRedirectURI() {
return redirectURI;
}
public String getState() {
return state;
}
public String getContext() {
return context;
}
@Override
public String toString() {
return "GetParameters [redirectURI=" + redirectURI + ", state=" + state + ", context=" + context + "]";
}
}

View File

@ -1,6 +1,13 @@
package org.gcube.portlet.user.my_vres.client;
import org.gcube.portlet.user.my_vres.shared.AuthorizationBean;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
/**
@ -9,12 +16,64 @@ import com.google.gwt.user.client.ui.RootPanel;
*
*/
public class MyVREs implements EntryPoint {
public static final String GET_REDIRECTURI_PARAMETER = "redirect_uri";
public static final String GET_STATE_PARAMETER = "state";
public static final String GET_CONTEXT_PARAMETER = "context";
public static final String GET_AUTH_TOKEN_PARAMETER = "gcube-token";
private final MyVREsServiceAsync myVREsService = GWT.create(MyVREsService.class);
/**
* This is the entry point method.
*/
public void onModuleLoad() {
RootPanel.get("myVREsDIV").add(new VresPanel());
//if no redirectUri is present acts normally
if (Window.Location.getParameter(GET_REDIRECTURI_PARAMETER) == null)
RootPanel.get("myVREsDIV").add(new VresPanel(null));
else {
handleAuthorisation();
}
}
/**
* if the context is present proceed with the call and redirect otherwise display the VREs for scope selection
*/
private void handleAuthorisation() {
final GetParameters params = getParameters();
if (! params.redirectURI.startsWith("https")) {
RootPanel.get("myVREsDIV").add(new HTML("ERROR: the redirectURI parameter must use HTTP Secure protocol (https)"));
return;
}
if (params.context == null || params.context.compareTo("") == 0) {
RootPanel.get("myVREsDIV").add(new VresPanel(params));
}
else {
myVREsService.getUserToken(params.context, params.state, new AsyncCallback<AuthorizationBean>() {
@Override
public void onSuccess(AuthorizationBean result) {
if (result.isSuccess()) {
Location.assign(params.redirectURI+"?"+GET_AUTH_TOKEN_PARAMETER+"="+result.getToken()+"&"+GET_STATE_PARAMETER+"="+result.getState());
} else {
RootPanel.get("myVREsDIV").add(new HTML("There were issues in managing this request: " + result.getErrorDescription()));
}
}
@Override
public void onFailure(Throwable caught) {
RootPanel.get("myVREsDIV").add(new HTML("An error occurred in the server: " + caught.getMessage()));
}
});
}
}
/**
* check if it has to show just one feed
* @return
*/
private GetParameters getParameters() {
String redirectURI = Window.Location.getParameter(GET_REDIRECTURI_PARAMETER);
String state = Window.Location.getParameter(GET_STATE_PARAMETER);
String context = Window.Location.getParameter(GET_CONTEXT_PARAMETER);
return new GetParameters(redirectURI, state, context);
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.portlet.user.my_vres.client;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.gcube.portlet.user.my_vres.shared.AuthorizationBean;
import org.gcube.portlet.user.my_vres.shared.VRE;
import com.google.gwt.user.client.rpc.RemoteService;
@ -15,6 +16,7 @@ import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
public interface MyVREsService extends RemoteService {
LinkedHashMap<String, ArrayList<VRE>> getUserVREs();
String getSiteLandingPagePath();
AuthorizationBean getUserToken(String context, String state);
}

View File

@ -3,6 +3,7 @@ package org.gcube.portlet.user.my_vres.client;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.gcube.portlet.user.my_vres.shared.AuthorizationBean;
import org.gcube.portlet.user.my_vres.shared.VRE;
import com.google.gwt.user.client.rpc.AsyncCallback;
@ -14,4 +15,6 @@ public interface MyVREsServiceAsync {
void getSiteLandingPagePath(AsyncCallback<String> callback);
void getUserToken(String context, String state, AsyncCallback<AuthorizationBean> callback);
}

View File

@ -34,12 +34,10 @@ public class VresPanel extends Composite {
private Image loadingImage = new Image(loading);
private LinkedHashMap<String, ArrayList<VRE>> cachedVREs = null;
boolean hasVres = false;
public VresPanel() {
super();
public VresPanel(GetParameters params) {
super();
loadingImage.getElement().getStyle().setMarginTop(59, Unit.PX);
mainPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
mainPanel.setWidth("100%");
@ -48,11 +46,12 @@ public class VresPanel extends Composite {
flowPanel.setWidth("100%");
flowPanel.setStyleName("flowPanel");
catPanel.setWidth("95%");
loadVREs();
loadVREs(params);
initWidget(mainPanel);
}
private void loadVREs() {
private void loadVREs(final GetParameters params) {
mainPanel.add(loadingImage);
myVREsService.getUserVREs(new AsyncCallback<LinkedHashMap<String,ArrayList<VRE>>>() {
@ -64,7 +63,7 @@ public class VresPanel extends Composite {
@Override
public void onSuccess(LinkedHashMap<String, ArrayList<VRE>> result) {
cachedVREs = result;
showIconView();
showIconView(params);
}
});
@ -72,7 +71,7 @@ public class VresPanel extends Composite {
}
private void showIconView() {
private void showIconView(final GetParameters params) {
boolean hasVREs = false;
mainPanel.clear();
mainPanel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
@ -91,7 +90,7 @@ public class VresPanel extends Composite {
for (VRE vre: cachedVREs.get(cat)) {
if (vre.getName().compareTo("")!= 0)
hasVREs = true;
ClickableVRE vreButton = new ClickableVRE(vre, (i < LOAD_MAX_IMAGE_NO));
ClickableVRE vreButton = new ClickableVRE(myVREsService, vre, (i < LOAD_MAX_IMAGE_NO), params);
flowPanel.add(vreButton);
i++;
}

View File

@ -1,5 +1,9 @@
package org.gcube.portlet.user.my_vres.client.widgets;
import org.gcube.portlet.user.my_vres.client.GetParameters;
import org.gcube.portlet.user.my_vres.client.MyVREs;
import org.gcube.portlet.user.my_vres.client.MyVREsServiceAsync;
import org.gcube.portlet.user.my_vres.shared.AuthorizationBean;
import org.gcube.portlet.user.my_vres.shared.VRE;
import com.google.gwt.core.client.GWT;
@ -9,7 +13,9 @@ import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
/**
*
@ -30,11 +36,7 @@ public class ClickableVRE extends HTML {
private String html = "";
public ClickableVRE() {
super();
}
public ClickableVRE(final VRE vre, final boolean showImage) {
public ClickableVRE(final MyVREsServiceAsync myVREsService, final VRE vre, final boolean showImage, final GetParameters params) {
super.setPixelSize(WIDTH, HEIGHT);
setPixelSize(WIDTH, HEIGHT);
if (vre.getName() == null || vre.getName().compareTo("") == 0) {
@ -56,22 +58,46 @@ public class ClickableVRE extends HTML {
setHTML(html);
setStyleName("vreButton");
if (params != null) {
addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
myVREsService.getUserToken(vre.getContext(), params.getState(), new AsyncCallback<AuthorizationBean>() {
@Override
public void onSuccess(AuthorizationBean result) {
if (result.isSuccess()) {
Location.assign(params.getRedirectURI()+"?"+MyVREs.GET_AUTH_TOKEN_PARAMETER+"="+result.getToken()+"&"+MyVREs.GET_STATE_PARAMETER+"="+result.getState());
} else {
RootPanel.get("myVREsDIV").add(new HTML("There were issues in managing this request: " + result.getErrorDescription()));
}
}
addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String html = "<div style=\"display: table; text-align:center; width: 100%; height: 75px;\">" +
"<span style=\"vertical-align:middle; display: table-cell;\">redirecting ...</span>" +
"</div>";
setHTML(html);
Timer timer = new Timer() {
@Override
public void run() {
Location.assign(vre.getFriendlyURL());
}
};
timer.schedule(50);
}
});
@Override
public void onFailure(Throwable caught) {
RootPanel.get("myVREsDIV").add(new HTML("An error occurred in the server: " + caught.getMessage()));
}
});
}
});
}
else {
addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String html = "<div style=\"display: table; text-align:center; width: 100%; height: 75px;\">" +
"<span style=\"vertical-align:middle; display: table-cell;\">redirecting ...</span>" +
"</div>";
setHTML(html);
Timer timer = new Timer() {
@Override
public void run() {
Location.assign(vre.getFriendlyURL());
}
};
timer.schedule(50);
}
});
}
addMouseOverHandler(new MouseOverHandler() {
@Override

View File

@ -10,9 +10,13 @@ import javax.servlet.http.HttpServletRequest;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.common.portal.PortalContext;
import org.gcube.portlet.user.my_vres.client.MyVREsService;
import org.gcube.portlet.user.my_vres.shared.AuthorizationBean;
import org.gcube.portlet.user.my_vres.shared.UserBelonging;
import org.gcube.portlet.user.my_vres.shared.VRE;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
@ -37,11 +41,11 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
*
*/
public static final String CACHED_VOS = "CACHED_VRES";
public static final String ADD_MORE_CATEGORY = "Add More";
public static final String ADD_MORE_IMAGE_PATH= "images/More.png";
@Override
public String getSiteLandingPagePath() {
String toReturn = PortalContext.getConfiguration().getSiteLandingPagePath(getThreadLocalRequest())+GCubePortalConstants.VRES_EXPLORE_FRIENDLY_URL;
@ -77,7 +81,7 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
LinkedHashMap<String, ArrayList<VRE>> toReturn = new LinkedHashMap<String, ArrayList<VRE>>();
List<VirtualGroup> currentSiteVGroups = gm.getVirtualGroups(ManagementUtils.getSiteGroupIdFromServletRequest(getThreadLocalRequest().getServerName()));
for (VirtualGroup vg : currentSiteVGroups) {
String gName = vg.getName();
@ -85,8 +89,8 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
String cat = gName;
toReturn.put(cat, toCreate);
}
GCubeGroup rootGroupVO = gm.getRootVO();
try {
_log.debug("root: " + rootGroupVO.getGroupName() );
@ -94,25 +98,25 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
_log.error("Cannot find root organziation, please check gcube-data.properties file in $CATALINA_HOME/conf folder, unless your installing the Bundle");
return toReturn;
}
//for each root sub organizations (VO)
for (GCubeGroup vOrg : rootGroupVO.getChildren()) {
for (GCubeGroup vre : vOrg.getChildren()) {
VRE vreToAdd = new VRE();
vreToAdd.setName(vre.getGroupName());
vreToAdd.setGroupName(gm.getInfrastructureScope(vre.getGroupId()));
vreToAdd.setContext(gm.getInfrastructureScope(vre.getGroupId()));
long logoId = vre.getLogoId();
String logoURL = gm.getGroupLogoURL(logoId);
vreToAdd.setImageURL(logoURL);
String vreUrl = GCubePortalConstants.PREFIX_GROUP_URL+vre.getFriendlyURL();
vreToAdd.setFriendlyURL(vreUrl);
vreToAdd.setUserBelonging(UserBelonging.NOT_BELONGING);
GCubeUser currUser = new LiferayUserManager().getUserByUsername(username);
if (gm.listGroupsByUser(currUser.getUserId()).contains(vre)) {
vreToAdd.setUserBelonging(UserBelonging.BELONGING);
List<VirtualGroup> vreGroups = gm.getVirtualGroups(vre.getGroupId());
for (VirtualGroup vreGroup : vreGroups) {
for (String category : toReturn.keySet()) {
@ -123,14 +127,14 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
}
}
}
}
}
}
//sort the vres in the groups
for (String cat : toReturn.keySet()) {
ArrayList<VRE> toSort = toReturn.get(cat);
@ -144,8 +148,8 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
ArrayList<VRE> addMoreVREs = new ArrayList<VRE>();
addMoreVREs.add(addMore);
toReturn.put(ADD_MORE_CATEGORY, addMoreVREs);
return toReturn;
}
@ -160,23 +164,23 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
*/
protected static LinkedHashMap<String, ArrayList<VRE>> getFakeVREs() {
LinkedHashMap<String, ArrayList<VRE>> toReturn = new LinkedHashMap<String, ArrayList<VRE>>();
final String categoryNameOne = "gCubeApps";
final String categoryNameTwo = "BlueBRIDGE";
final String categoryNameThree = "GEMex";
//
VRE cool_EM_VRE = new VRE();
cool_EM_VRE.setName("BiodiversityResearchEnvironment");
cool_EM_VRE.setGroupName("/d4science.research-infrastructures.eu/EM/COOLEMVRE");
cool_EM_VRE.setContext("/d4science.research-infrastructures.eu/EM/COOLEMVRE");
cool_EM_VRE.setDescription("cool_EM_VRE VRE Description<br />"+
"This Virtual Research Environment is for cool authors, managers and researchers who produce reports containing cool data.");
cool_EM_VRE.setImageURL("https://placehold.it/150x150");
cool_EM_VRE.setUserBelonging(UserBelonging.BELONGING);
VRE cool_EM_VRE2 = new VRE();
cool_EM_VRE2.setName("COOL VRE 2");
cool_EM_VRE2.setGroupName("/d4science.research-infrastructures.eu/EM/COOLEMVRE2");
cool_EM_VRE2.setContext("/d4science.research-infrastructures.eu/EM/COOLEMVRE2");
cool_EM_VRE2.setDescription("Cool VRE Description<br />"+
"This Virtual Research Environment is for cool authors, managers and researchers who produce reports containing cool data.");
@ -185,7 +189,7 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
VRE cool_EM_VRE3 = new VRE();
cool_EM_VRE3.setName("COOL EM VRE TRE");
cool_EM_VRE3.setGroupName("/d4science.research-infrastructures.eu/EM/COOlVRE3");
cool_EM_VRE3.setContext("/d4science.research-infrastructures.eu/EM/COOlVRE3");
cool_EM_VRE3.setDescription("Cool VRE Description<br />"+
"This Virtual Research Environment is for cool authors, managers and researchers who produce reports containing cool data.");
@ -199,11 +203,11 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
toAdd.add(cool_EM_VRE);
toAdd.add(cool_EM_VRE2);
toAdd.add(cool_EM_VRE3);
VRE demo = new VRE();
demo.setName("Demo");
demo.setGroupName("/d4science.research-infrastructures.eu/EM/Demo");
demo.setContext("/d4science.research-infrastructures.eu/EM/Demo");
demo.setDescription("Cool VRE Description<br />"+
"This Virtual Research Environment is for cool authors, managers and researchers who produce reports containing cool data.");
@ -212,7 +216,7 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
VRE vreGCM = new VRE();
vreGCM.setName("GCM");
vreGCM.setGroupName("/d4science.research-infrastructures.eu/EM/GCM");
vreGCM.setContext("/d4science.research-infrastructures.eu/EM/GCM");
vreGCM.setDescription("Global Ocean Chlorophyll Monitoring (GCM) Virtual Research Environment<br />"
+ "The phytoplankton plays a similar role to terrestrial green plants in the photosynthetic process and are credited with removing as much carbon dioxide from the atmosphere as their earthbound counterparts, making it important to monitor and model plankton into calculations of future climate change.");
vreGCM.setImageURL("https://placehold.it/150x150");
@ -225,7 +229,7 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
toAdd2.add(cool_EM_VRE);
toAdd2.add(cool_EM_VRE2);
toAdd2.add(cool_EM_VRE3);
ArrayList<VRE> toAdd3 = new ArrayList<VRE>();
toAdd3.add(demo);
toAdd3.add(vreGCM);
@ -234,13 +238,55 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
toAdd3.add(cool_EM_VRE);
toAdd3.add(cool_EM_VRE2);
toAdd3.add(cool_EM_VRE3);
toReturn.put(categoryNameOne, toAdd);
toReturn.put(categoryNameTwo, toAdd2);
toReturn.put(categoryNameThree, toAdd3);
return toReturn;
}
@Override
public AuthorizationBean getUserToken( String context, String state) {
if (state == null || state.compareTo("")== 0) {
return new AuthorizationBean(null, null, false, "State is null, please use a unique string value of your choice that is hard to guess (e.g. state=7d12bf13-111c-4f46-ab06-9e9e08ad377b). Used to prevent CSRF attacks");
}
if (context == null || context.compareTo("")== 0) {
return new AuthorizationBean(null, null, false, "Infrastructure Context is null");
}
PortalContext pContext = PortalContext.getConfiguration();
GCubeUser currentUser = pContext.getCurrentUser(getThreadLocalRequest());
String username = currentUser.getUsername();
long userId = currentUser.getUserId();
_log.debug("Creating AuthorizationBean for user " + username);
if (isWithinPortal()) {
GroupManager gm = new LiferayGroupManager();
try {
long groupId = gm.getGroupIdFromInfrastructureScope(context);
_log.debug("Verifying user permission for scope " + context);
if (! gm.listGroupsByUser(userId).contains(gm.getGroup(groupId))) {
return new AuthorizationBean(null, null, false, "User having username: " + username + " is not authorised in context: " + context);
}
} catch (IllegalArgumentException | UserManagementSystemException | GroupRetrievalFault e) {
_log.error("Something wrong in the Context parameter: " + e.getMessage());
return new AuthorizationBean(null, null, false, "Something wrong in the Context parameter: " + e.getMessage());
} catch (UserRetrievalFault e) {
return new AuthorizationBean(null, null, false, "Something wrong in the user retrieval " + e.getMessage());
}
}
if (username == null) {
_log.error("Something wrong in retrieving the user");
return new AuthorizationBean(null, null, false, "Something wrong in retrieving the logged in user, is session expired?");
}
String token = pContext.getCurrentUserToken(context, username);
if (token == null) {
_log.error("Something wrong in retrieving the user token in this context: " + context + " username="+username);
return new AuthorizationBean(null, null, false, "Something wrong in retrieving the user token in this context: " + context + " username="+username);
}
_log.debug("Authorisation OAUTH returning user token in this context: " + context + " username="+username);
return new AuthorizationBean(token, state, true, null);
}
}

View File

@ -0,0 +1,63 @@
package org.gcube.portlet.user.my_vres.shared;
import java.io.Serializable;
@SuppressWarnings("serial")
public class AuthorizationBean implements Serializable {
private String token;
private String state;
private boolean success;
private String errorDescription;
public AuthorizationBean() {
super();
// TODO Auto-generated constructor stub
}
public AuthorizationBean(String token, String state, boolean success, String errorDescription) {
super();
this.token = token;
this.state = state;
this.success = success;
this.errorDescription = errorDescription;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getErrorDescription() {
return errorDescription;
}
public void setErrorDescription(String errorDescription) {
this.errorDescription = errorDescription;
}
@Override
public String toString() {
return "AuthorizationBean [token=" + token + ", state=" + state + ", success=" + success + ", errorDescription="
+ errorDescription + "]";
}
}

View File

@ -2,10 +2,7 @@ package org.gcube.portlet.user.my_vres.shared;
import java.io.Serializable;
/**
*
* @author Massimiliano Assante ISTI-CNR
*
* @version 2.0 Jan 10th 2012
*/
@SuppressWarnings("serial")
public class ResearchEnvironment implements Serializable{
@ -14,8 +11,8 @@ public class ResearchEnvironment implements Serializable{
private String description;
private String imageURL;
private String groupName;
//the infrastructure scope
private String context;
private String friendlyURL;
@ -26,13 +23,13 @@ public class ResearchEnvironment implements Serializable{
}
public ResearchEnvironment(String name, String description,
String imageURL, String groupName, String friendlyURL,
String imageURL, String context, String friendlyURL,
UserBelonging userBelonging) {
super();
this.name = name;
this.description = description;
this.imageURL = imageURL;
this.groupName = groupName;
this.context = context;
this.friendlyURL = friendlyURL;
this.userBelonging = userBelonging;
}
@ -60,13 +57,16 @@ public class ResearchEnvironment implements Serializable{
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getGroupName() {
return groupName;
/**
*
* @return the infrastructure scope
*/
public String getContext() {
return context;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
public void setContext(String context) {
this.context = context;
}
public String getFriendlyURL() {