2016-05-26 17:50:06 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.widgets.githubconnector.server;
|
|
|
|
|
2016-11-24 15:52:28 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2016-05-26 17:50:06 +02:00
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
2017-02-07 11:51:14 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-11-24 15:52:28 +01:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
2017-02-07 11:51:14 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-11-24 15:52:28 +01:00
|
|
|
import org.gcube.portlets.widgets.githubconnector.server.util.ServiceCredentials;
|
2016-05-26 17:50:06 +02:00
|
|
|
import org.gcube.portlets.widgets.githubconnector.shared.Constants;
|
|
|
|
import org.gcube.portlets.widgets.githubconnector.shared.exception.ServiceException;
|
2017-02-07 11:51:14 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
2016-05-26 17:50:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-05-12 17:38:47 +02:00
|
|
|
* @author Giancarlo Panichi
|
|
|
|
*
|
2016-05-26 17:50:06 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class SessionUtil {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(SessionUtil.class);
|
|
|
|
|
2017-02-07 11:51:14 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param httpServletRequest
|
2017-05-12 17:38:47 +02:00
|
|
|
* http servlet request
|
|
|
|
* @return service credentials
|
|
|
|
* @throws ServiceException
|
|
|
|
* service exception
|
2017-02-07 11:51:14 +01:00
|
|
|
*/
|
2017-05-12 17:38:47 +02:00
|
|
|
public static ServiceCredentials getServiceCredentials(HttpServletRequest httpServletRequest)
|
2017-02-07 11:51:14 +01:00
|
|
|
throws ServiceException {
|
|
|
|
return getServiceCredentials(httpServletRequest, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param httpServletRequest
|
2017-05-12 17:38:47 +02:00
|
|
|
* http service request
|
2017-02-07 11:51:14 +01:00
|
|
|
* @param scopeGroupId
|
2017-05-12 17:38:47 +02:00
|
|
|
* scope group id
|
|
|
|
* @return service credentials
|
|
|
|
* @throws ServiceException
|
|
|
|
* service exception
|
2017-02-07 11:51:14 +01:00
|
|
|
*/
|
2017-05-12 17:38:47 +02:00
|
|
|
public static ServiceCredentials getServiceCredentials(HttpServletRequest httpServletRequest, String scopeGroupId)
|
2017-02-07 11:51:14 +01:00
|
|
|
throws ServiceException {
|
|
|
|
|
2016-11-24 15:52:28 +01:00
|
|
|
ServiceCredentials sCredentials = null;
|
|
|
|
String userName = null;
|
|
|
|
String scope = null;
|
|
|
|
String token = null;
|
2017-02-07 11:51:14 +01:00
|
|
|
String groupId = null;
|
|
|
|
String groupName = null;
|
2016-11-24 15:52:28 +01:00
|
|
|
|
|
|
|
if (Constants.DEBUG_MODE) {
|
|
|
|
logger.info("No credential found in session, use test user!");
|
2017-02-07 11:51:14 +01:00
|
|
|
/*
|
|
|
|
* InfoLocale infoLocale = getInfoLocale(httpServletRequest, null);
|
|
|
|
* Locale locale = new Locale(infoLocale.getLanguage());
|
|
|
|
*
|
|
|
|
* ResourceBundle messages = ResourceBundle.getBundle(
|
|
|
|
* StatAlgoImporterServiceMessagesConstants.TDGWTServiceMessages,
|
|
|
|
* locale);
|
|
|
|
*/
|
2016-11-24 15:52:28 +01:00
|
|
|
userName = Constants.DEFAULT_USER;
|
|
|
|
scope = Constants.DEFAULT_SCOPE;
|
|
|
|
token = Constants.DEFAULT_TOKEN;
|
2016-05-26 17:50:06 +02:00
|
|
|
|
2017-02-07 11:51:14 +01:00
|
|
|
logger.info("Set SecurityToken: " + token);
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
logger.info("Set ScopeProvider: " + scope);
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
2016-11-24 15:52:28 +01:00
|
|
|
sCredentials = new ServiceCredentials(userName, scope, token);
|
2016-05-26 17:50:06 +02:00
|
|
|
|
|
|
|
} else {
|
2016-11-24 15:52:28 +01:00
|
|
|
logger.info("Retrieving credential in session!");
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
2017-02-07 11:51:14 +01:00
|
|
|
boolean hasScopeGroupId = false;
|
|
|
|
|
|
|
|
if (scopeGroupId != null && !scopeGroupId.isEmpty()) {
|
|
|
|
hasScopeGroupId = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
hasScopeGroupId = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasScopeGroupId) {
|
|
|
|
scope = pContext.getCurrentScope(scopeGroupId);
|
|
|
|
} else {
|
|
|
|
scope = pContext.getCurrentScope(httpServletRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scope == null || scope.isEmpty()) {
|
|
|
|
String error = "Error retrieving scope: " + scope;
|
|
|
|
logger.error(error);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest);
|
|
|
|
|
|
|
|
if (gCubeUser == null) {
|
2017-05-12 17:38:47 +02:00
|
|
|
String error = "Error retrieving gCubeUser in scope " + scope + ": " + gCubeUser;
|
2017-02-07 11:51:14 +01:00
|
|
|
logger.error(error);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
userName = gCubeUser.getUsername();
|
|
|
|
|
|
|
|
if (userName == null || userName.isEmpty()) {
|
2017-05-12 17:38:47 +02:00
|
|
|
String error = "Error retrieving username in scope " + scope + ": " + userName;
|
2017-02-07 11:51:14 +01:00
|
|
|
logger.error(error);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
token = pContext.getCurrentUserToken(scope, userName);
|
|
|
|
|
|
|
|
if (token == null || token.isEmpty()) {
|
2017-05-12 17:38:47 +02:00
|
|
|
String error = "Error retrieving token for " + userName + " in " + scope + ": " + token;
|
2017-02-07 11:51:14 +01:00
|
|
|
logger.error(error);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
String name = gCubeUser.getFirstName();
|
|
|
|
String lastName = gCubeUser.getLastName();
|
|
|
|
String fullName = gCubeUser.getFullname();
|
|
|
|
|
|
|
|
String userAvatarURL = gCubeUser.getUserAvatarURL();
|
|
|
|
|
|
|
|
String email = gCubeUser.getEmail();
|
|
|
|
|
|
|
|
if (hasScopeGroupId) {
|
|
|
|
logger.info("Set SecurityToken: " + token);
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
logger.info("Set ScopeProvider: " + scope);
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
|
|
|
groupId = scopeGroupId;
|
|
|
|
|
|
|
|
long gId;
|
|
|
|
|
|
|
|
try {
|
|
|
|
gId = Long.parseLong(scopeGroupId);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
String error = "Error retrieving groupId: " + scopeGroupId;
|
|
|
|
logger.error(error, e);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
GCubeGroup group;
|
|
|
|
try {
|
|
|
|
group = new LiferayGroupManager().getGroup(gId);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
String error = "Error retrieving group: " + groupName;
|
|
|
|
logger.error(error);
|
|
|
|
throw new ServiceException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
groupName = group.getGroupName();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-05-12 17:38:47 +02:00
|
|
|
groupId = String.valueOf(pContext.getCurrentGroupId(httpServletRequest));
|
2017-02-07 11:51:14 +01:00
|
|
|
|
|
|
|
groupName = pContext.getCurrentGroupName(httpServletRequest);
|
|
|
|
|
|
|
|
}
|
2016-05-26 17:50:06 +02:00
|
|
|
|
2017-05-12 17:38:47 +02:00
|
|
|
sCredentials = new ServiceCredentials(userName, fullName, name, lastName, email, scope, groupId, groupName,
|
|
|
|
userAvatarURL, token);
|
2016-05-26 17:50:06 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 11:51:14 +01:00
|
|
|
logger.info("ServiceCredentials: " + sCredentials);
|
2016-05-26 17:50:06 +02:00
|
|
|
|
2016-11-24 15:52:28 +01:00
|
|
|
return sCredentials;
|
2016-05-26 17:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|