2016-05-02 16:52:47 +02:00
|
|
|
package org.gcube.portal.threadlocalexec;
|
|
|
|
|
2016-11-10 15:10:01 +01:00
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2016-05-02 16:52:47 +02:00
|
|
|
import java.io.IOException;
|
2016-11-10 15:10:01 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-05-02 16:52:47 +02:00
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
2016-11-10 15:10:01 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2016-05-02 16:52:47 +02:00
|
|
|
|
|
|
|
import org.apache.catalina.connector.Request;
|
|
|
|
import org.apache.catalina.connector.Response;
|
|
|
|
import org.apache.catalina.valves.ValveBase;
|
2016-11-17 11:19:01 +01:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2016-05-02 16:52:47 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-11-10 15:10:01 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
2016-05-02 16:52:47 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2020-03-26 09:52:37 +01:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
2016-05-02 16:52:47 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2016-11-10 15:10:01 +01:00
|
|
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
2016-05-02 16:52:47 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Massimiliano Assante, CNR ISTI
|
|
|
|
* @author Lucio Lelii, CNR ISTI
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class SmartGearsPortalValve extends ValveBase {
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(SmartGearsPortalValve.class);
|
2016-11-10 15:10:01 +01:00
|
|
|
private final static String DEFAULT_ROLE = "OrganizationMember";
|
2016-11-15 17:42:45 +01:00
|
|
|
private final static String LIFERAY_POLLER_CONTEXT = "poller/receive";
|
2016-11-10 15:10:01 +01:00
|
|
|
|
2016-05-02 16:52:47 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invoke(Request req, Response resp) throws IOException, ServletException {
|
|
|
|
SecurityTokenProvider.instance.reset();
|
|
|
|
ScopeProvider.instance.reset();
|
|
|
|
AuthorizationProvider.instance.reset();
|
2016-11-11 18:36:09 +01:00
|
|
|
//_log.trace("SmartGearsPortalValve SecurityTokenProvider and AuthorizationProvider reset OK");
|
2016-11-10 15:10:01 +01:00
|
|
|
if (req instanceof HttpServletRequest) {
|
|
|
|
HttpServletRequest request = (HttpServletRequest) req;
|
2016-11-15 17:42:45 +01:00
|
|
|
if (!req.getRequestURL().toString().endsWith(LIFERAY_POLLER_CONTEXT)) { //avoid calling gCube auth service for liferay internal poller
|
|
|
|
PortalContext context = PortalContext.getConfiguration();
|
|
|
|
String scope = context.getCurrentScope(request);
|
|
|
|
String username = getCurrentUsername(request);
|
2020-03-26 09:52:37 +01:00
|
|
|
if (scope != null && username != null && validateContext(scope)) {
|
2016-11-17 11:19:01 +01:00
|
|
|
String userToken = null;
|
2016-11-15 17:42:45 +01:00
|
|
|
try {
|
2016-11-17 11:19:01 +01:00
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
userToken = authorizationService().resolveTokenByUserAndContext(username, scope);
|
2016-11-15 17:42:45 +01:00
|
|
|
SecurityTokenProvider.instance.set(userToken);
|
2016-11-17 11:19:01 +01:00
|
|
|
}
|
|
|
|
catch (ObjectNotFound ex) {
|
|
|
|
userToken = generateAuthorizationToken(username, scope);
|
|
|
|
SecurityTokenProvider.instance.set(userToken);
|
|
|
|
_log.debug("generateAuthorizationToken OK for " + username + " in scope " + scope);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2016-11-15 17:42:45 +01:00
|
|
|
_log.error("Something went wrong in generating token for " + username + " in scope " + scope);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
//_log.trace("Security token set OK for " + username + " in scope " + scope);
|
2016-11-10 15:10:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 16:52:47 +02:00
|
|
|
getNext().invoke(req, resp);
|
2016-11-10 15:10:01 +01:00
|
|
|
}
|
2020-03-26 09:52:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return true if is the context is syntactically valid
|
|
|
|
*/
|
|
|
|
private static boolean validateContext(String context) {
|
|
|
|
String separator = "/";
|
|
|
|
if (!context.matches("\\S+"))
|
|
|
|
return false;
|
|
|
|
String[] components=context.split(separator);
|
|
|
|
if (components.length<2 || components.length>4)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-10 15:10:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param username
|
|
|
|
* @param scope
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2016-11-17 11:19:01 +01:00
|
|
|
private static String generateAuthorizationToken(String username, String scope) {
|
2016-11-10 15:10:01 +01:00
|
|
|
List<String> userRoles = new ArrayList<>();
|
|
|
|
userRoles.add(DEFAULT_ROLE);
|
2016-11-17 11:19:01 +01:00
|
|
|
String token;
|
|
|
|
try {
|
|
|
|
token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-10 15:10:01 +01:00
|
|
|
return token;
|
|
|
|
}
|
2016-11-15 17:42:45 +01:00
|
|
|
|
2016-11-10 15:10:01 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the httpServletRequest object
|
|
|
|
* @return the instance of the user
|
|
|
|
* @see GCubeUser
|
|
|
|
*/
|
|
|
|
public static String getCurrentUsername(HttpServletRequest httpServletRequest) {
|
|
|
|
String userIdNo = httpServletRequest.getHeader(PortalContext.USER_ID_ATTR_NAME);
|
2016-11-15 15:25:11 +01:00
|
|
|
if (userIdNo != null && userIdNo.compareTo("undefined") != 0) {
|
2016-11-10 15:10:01 +01:00
|
|
|
long userId = -1;
|
|
|
|
try {
|
|
|
|
userId = Long.parseLong(userIdNo);
|
|
|
|
return UserLocalServiceUtil.getUser(userId).getScreenName();
|
|
|
|
} catch (NumberFormatException e) {
|
2016-11-15 14:46:42 +01:00
|
|
|
_log.error("The userId is not a number -> " + userIdNo);
|
2016-11-14 17:32:21 +01:00
|
|
|
return null;
|
2016-11-10 15:10:01 +01:00
|
|
|
} catch (Exception e) {
|
2016-11-15 14:46:42 +01:00
|
|
|
_log.error("The userId does not belong to any user -> " + userIdNo);
|
2016-11-14 17:32:21 +01:00
|
|
|
return null;
|
2016-11-10 15:10:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2016-05-02 16:52:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|