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;
|
|
|
|
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;
|
|
|
|
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-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;
|
|
|
|
PortalContext context = PortalContext.getConfiguration();
|
|
|
|
|
|
|
|
String scope = context.getCurrentScope(request);
|
|
|
|
String username = getCurrentUsername(request);
|
|
|
|
if (scope != null && username != null) {
|
|
|
|
try {
|
|
|
|
String userToken = getAuthorizationToken(username, scope);
|
|
|
|
SecurityTokenProvider.instance.set(userToken);
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("Something went wrong in generating token for " + username + " in scope " + scope);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-11-11 18:36:09 +01:00
|
|
|
//_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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param username
|
|
|
|
* @param scope
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private static String getAuthorizationToken(String username, String scope) throws Exception {
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
List<String> userRoles = new ArrayList<>();
|
|
|
|
userRoles.add(DEFAULT_ROLE);
|
|
|
|
String token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
if (userIdNo != null) {
|
|
|
|
long userId = -1;
|
|
|
|
try {
|
|
|
|
userId = Long.parseLong(userIdNo);
|
|
|
|
return UserLocalServiceUtil.getUser(userId).getScreenName();
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
_log.error("The userId is not a number -> " + userId);
|
2016-11-14 17:32:21 +01:00
|
|
|
return null;
|
2016-11-10 15:10:01 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("The userId does not belong to any user -> " + userId);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|