From 462a1e7446a5adadc0bbc8f73501a5fb7c826f93 Mon Sep 17 00:00:00 2001 From: Salvatore La Manna Date: Fri, 31 Jul 2020 17:29:29 +0200 Subject: [PATCH] Plugin autenticazione --- .../hooks/sicilia-login-hook/.classpath | 27 ++++ .../hooks/sicilia-login-hook/.project | 37 +++++ .../sicilia-login-hook/.settings/.jsdtscope | 12 ++ .../.settings/org.eclipse.jdt.core.prefs | 7 + .../org.eclipse.wst.common.component | 8 ++ ...se.wst.common.project.facet.core.prefs.xml | 7 + ....eclipse.wst.common.project.facet.core.xml | 11 ++ ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + .../hooks/sicilia-login-hook/build.xml | 6 + .../docroot/META-INF/MANIFEST.MF | 3 + .../docroot/WEB-INF/.gitignore | 1 + .../docroot/WEB-INF/liferay-hook.xml | 16 +++ .../WEB-INF/liferay-plugin-package.properties | 11 ++ .../mwg/sicilia/constants/ApiConstants.java | 46 +++++++ .../mwg/sicilia/login/SiciliaAutoLogin.java | 113 ++++++++++++++++ .../mwg/sicilia/login/SiciliaLoginFilter.java | 76 +++++++++++ .../src/it/mwg/sicilia/util/ApiUtil.java | 126 ++++++++++++++++++ .../docroot/WEB-INF/src/portal.properties | 2 + .../docroot/WEB-INF/web.xml | 5 + .../hooks/sicilia-login-hook/ivy.xml | 18 +++ .../hooks/sicilia-login-hook/ivy.xml.MD5 | 1 + .../test/integration/arquillian.xml | 15 +++ .../org.eclipse.wst.common.component | 4 +- .../org.eclipse.wst.common.component | 3 + .../org.eclipse.wst.common.component | 6 + .../org.eclipse.wst.common.component | 3 + .../webs/portos-kaleo-web/.classpath | 11 +- .../.settings/org.eclipse.jdt.core.prefs | 7 + .../org.eclipse.wst.common.component | 14 ++ ...se.wst.common.project.facet.core.prefs.xml | 7 + ....eclipse.wst.common.project.facet.core.xml | 9 ++ 32 files changed, 607 insertions(+), 7 deletions(-) create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.classpath create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.project create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/.jsdtscope create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.jdt.core.prefs create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.component create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/build.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/META-INF/MANIFEST.MF create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/.gitignore create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-hook.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-plugin-package.properties create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/constants/ApiConstants.java create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaAutoLogin.java create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaLoginFilter.java create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/util/ApiUtil.java create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/portal.properties create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/web.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml.MD5 create mode 100644 liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/test/integration/arquillian.xml create mode 100644 liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.jdt.core.prefs create mode 100644 liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.component create mode 100644 liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml create mode 100644 liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.xml diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.classpath b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.classpath new file mode 100644 index 00000000..81819043 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.project b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.project new file mode 100644 index 00000000..c5c939f3 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.project @@ -0,0 +1,37 @@ + + + sicilia-login-hook + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.apache.ivyde.eclipse.ivynature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/.jsdtscope b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/.jsdtscope new file mode 100644 index 00000000..840a01d5 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.jdt.core.prefs b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..f42de363 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.component new file mode 100644 index 00000000..bacfbb69 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml new file mode 100644 index 00000000..1f9ee534 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 00000000..02ccc82b --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.container b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 00000000..3bd5d0a4 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.name b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 00000000..05bd71b6 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/build.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/build.xml new file mode 100644 index 00000000..c2d19b37 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/build.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/META-INF/MANIFEST.MF b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/META-INF/MANIFEST.MF new file mode 100644 index 00000000..5e949512 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/.gitignore b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/.gitignore new file mode 100644 index 00000000..840e7d31 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/.gitignore @@ -0,0 +1 @@ +/classes/ diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-hook.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-hook.xml new file mode 100644 index 00000000..4ba27c04 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-hook.xml @@ -0,0 +1,16 @@ + + + + + portal.properties + + Sicilia SSO + it.mwg.sicilia.login.SiciliaLoginFilter + + + Sicilia SSO + /c/portal/login + FORWARD + REQUEST + + \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-plugin-package.properties b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-plugin-package.properties new file mode 100644 index 00000000..fca51bc8 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/liferay-plugin-package.properties @@ -0,0 +1,11 @@ +name=Sicilia Login Hook +module-group-id=liferay +module-incremental-version=1 +tags= +short-description= +long-description= +change-log= +page-url=http://www.liferay.com +author=Liferay, Inc. +licenses=LGPL +liferay-versions=6.2.0+ \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/constants/ApiConstants.java b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/constants/ApiConstants.java new file mode 100644 index 00000000..a2e1c097 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/constants/ApiConstants.java @@ -0,0 +1,46 @@ +package it.mwg.sicilia.constants; + +import java.util.HashMap; +import java.util.Map; + +public class ApiConstants { + public static final String LABEL_ERROR_CODE = "code"; + public static final String LABEL_DESCRIPTION = "description"; + public static final String LABEL_STATUS = "status"; + + public static final int ERROR_OK = 0; + public static final int ERROR_INVALID_AUTH = 1; + public static final int ERROR_INVALID_LOGIN = 2; + public static final int ERROR_INVALID_TOKEN = 3; + public static final int ERROR_INVALID_INPUT = 4; + public static final int ERROR_MISSING_PARAMETER = 5; + public static final int ERROR_INVALID_PASSWORD = 6; + + public static final String STATUS_OK = "success"; + public static final String STATUS_ERROR = "error"; + + public static final String API_USERNAME = "sismica"; + public static final String API_PASSWORD = "4Tr-D%fO!sli5"; + + public static final String PARAMETER_AUTH = "auth"; + public static final String PARAMETER_CALLBACK_URL = "callback"; + public static final String PARAMETER_TOKEN = "token"; + public static final String PARAMETER_USERNAME = "username"; + public static final String PARAMETER_PASSWORD = "password"; + public static final String PARAMETER_FIRST_NAME = "nome"; + public static final String PARAMETER_LAST_NAME = "cognome"; + public static final String PARAMETER_CODICE_FISCALE = "codice_fiscale"; + + private static final String SESSION_BASE = "SICILIA_HOOK_"; + public static final String SESSION_AUTH = SESSION_BASE + PARAMETER_AUTH.toUpperCase(); + public static final String SESSION_TOKEN = SESSION_BASE + PARAMETER_TOKEN.toUpperCase(); + public static final String SESSION_USER_DATA = SESSION_BASE + "USER_DATA"; + + public static final String LOGIN_SERVER = "https://auth.mwg.it/"; + private static final String ENDPOINT_BASE = LOGIN_SERVER + "api/v1/"; + public static final String ENDPOINT_LOGIN = ENDPOINT_BASE + "login.cgi"; + public static final String ENDPOINT_GET_TOKEN = ENDPOINT_BASE + "get_token.cgi"; + public static final String ENDPOINT_GET_USER = ENDPOINT_BASE + "get_user.cgi"; + + public static final String HTTP_USER_AGENT = "Sicilia Login like Mozilla/5.0"; +} diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaAutoLogin.java b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaAutoLogin.java new file mode 100644 index 00000000..0c2074e0 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaAutoLogin.java @@ -0,0 +1,113 @@ +package it.mwg.sicilia.login; + +import it.mwg.sicilia.constants.ApiConstants; + +import java.util.Date; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.security.SecureRandom; +import com.liferay.portal.kernel.util.StringPool; +import com.liferay.portal.kernel.util.Validator; +import com.liferay.portal.model.User; +import com.liferay.portal.security.auth.AutoLogin; +import com.liferay.portal.security.auth.AutoLoginException; +import com.liferay.portal.service.ServiceContext; +import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.util.PortalUtil; + +/** + * @author Manifattura Web Group per Regione Siciliana + * + */ +public class SiciliaAutoLogin implements AutoLogin { + private static final Log _log = LogFactoryUtil.getLog(SiciliaAutoLogin.class); + + @Override + public String[] login(HttpServletRequest request, HttpServletResponse response) throws AutoLoginException { + User user = null; + HttpSession session = request.getSession(); + Map userData = (Map) session + .getAttribute(ApiConstants.SESSION_USER_DATA); + if (Validator.isNotNull(userData)) { + long companyId = PortalUtil.getCompanyId(request); + user = logUser(companyId, userData.get(ApiConstants.PARAMETER_CODICE_FISCALE), + userData.get(ApiConstants.PARAMETER_FIRST_NAME), + userData.get(ApiConstants.PARAMETER_LAST_NAME)); + if (Validator.isNotNull(user)) { + return new String[] { String.valueOf(user.getUserId()), user.getPassword(), + String.valueOf(user.isPasswordEncrypted()) }; + } + } + return null; + } + + @Override + public String[] handleException(HttpServletRequest request, HttpServletResponse response, Exception e) + throws AutoLoginException { + _log.error(e); + return null; + } + + private User logUser(long companyId, String screenName, String firstName, String lastName) { + _log.info("Cerco l'utente " + screenName + " (" + firstName + " " + lastName + ")"); + User user = null; + try { + user = UserLocalServiceUtil.fetchUserByScreenName(companyId, screenName); + + if (Validator.isNull(user)) { + long creatorUserId = UserLocalServiceUtil.getDefaultUserId(companyId); + boolean autoPassword = true; + String password = randomPassword(24); + boolean autoScreenName = false; + String emailAddress = screenName + "@no-email.regione.sicilia.it"; + long facebookId = 0; + String openId = StringPool.BLANK; + Locale locale = Locale.ITALY; + String middleName = StringPool.BLANK; + int prefixId = 0; + int suffixId = 0; + boolean male = true; + int birthdayMonth = 0; + int birthdayDay = 1; + int birthdayYear = 1970; + String jobTitle = StringPool.BLANK; + long[] groupIds = null; + long[] organizationIds = null; + long[] roleIds = null; + long[] userGroupIds = null; + boolean sendEmail = false; + ServiceContext serviceContext = new ServiceContext(); + user = UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password, + password, autoScreenName, screenName, emailAddress, facebookId, openId, locale, + firstName, middleName, lastName, prefixId, suffixId, male, birthdayMonth, + birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds, roleIds, + userGroupIds, sendEmail, serviceContext); + user.setPasswordModified(true); + user.setPasswordModifiedDate(new Date()); + user.setPasswordReset(false); + UserLocalServiceUtil.updateUser(user); + } + } catch (SystemException | PortalException e) { + _log.error("Errore durante l'autenticazione/creazione dell'utente " + screenName, e); + } + return user; + } + + private String randomPassword(int length) { + SecureRandom rnd = new SecureRandom(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + sb.append((char) (32 + rnd.nextInt(95))); + } + return sb.toString(); + } +} diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaLoginFilter.java b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaLoginFilter.java new file mode 100644 index 00000000..b94c6ef9 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/login/SiciliaLoginFilter.java @@ -0,0 +1,76 @@ +package it.mwg.sicilia.login; + +import it.mwg.sicilia.constants.ApiConstants; +import it.mwg.sicilia.util.ApiUtil; + +import java.io.IOException; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.util.StringPool; +import com.liferay.portal.kernel.util.Validator; +import com.liferay.portal.kernel.util.WebKeys; +import com.liferay.portal.model.User; +import com.liferay.portal.service.ServiceContext; +import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.util.PortalUtil; + +public class SiciliaLoginFilter implements Filter { + public static final Log _log = LogFactoryUtil.getLog(SiciliaLoginFilter.class); + + @Override + public void destroy() { + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) + throws IOException, ServletException { + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + String url = httpServletRequest.getRequestURL().toString(); + String qs = httpServletRequest.getQueryString(); + if (Validator.isNotNull(qs)) { + url += "?" + qs; + } + _log.info("Filtro: " + url); + HttpSession session = httpServletRequest.getSession(); + String token = (String) session.getAttribute(ApiConstants.SESSION_TOKEN); + if (Validator.isNull(token)) { + token = ApiUtil.getToken(url); + _log.info("Ho creato il token " + token + ". Eseguo redirect"); + session.setAttribute(ApiConstants.SESSION_TOKEN, token); + ((HttpServletResponse) servletResponse).sendRedirect(ApiConstants.LOGIN_SERVER + "?t=" + token); + return; + } else { + Map userData = ApiUtil.getUserInfo(token); + if (Validator.isNull(userData)) { + _log.info("Il token potrebbe essere scaduto. Lo ricreo ed eseguo redirect."); + token = ApiUtil.getToken(url); + session.setAttribute(ApiConstants.SESSION_TOKEN, token); + ((HttpServletResponse) servletResponse).sendRedirect(ApiConstants.LOGIN_SERVER + "?t=" + + token); + return; + } else { + session.setAttribute(ApiConstants.SESSION_USER_DATA, userData); + } + } + chain.doFilter(servletRequest, servletResponse); + } + + @Override + public void init(FilterConfig config) throws ServletException { + } +} diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/util/ApiUtil.java b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/util/ApiUtil.java new file mode 100644 index 00000000..15f04d44 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/it/mwg/sicilia/util/ApiUtil.java @@ -0,0 +1,126 @@ +package it.mwg.sicilia.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import it.mwg.sicilia.constants.ApiConstants; + +import com.liferay.portal.kernel.json.JSONException; +import com.liferay.portal.kernel.json.JSONFactoryUtil; +import com.liferay.portal.kernel.json.JSONObject; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.util.Validator; + +public class ApiUtil { + private static final Log _log = LogFactoryUtil.getLog(ApiUtil.class); + private static String auth = null; + + public static String getToken(String callbackURL) { + if (Validator.isNull(auth)) { + login(); + } + for (int i = 0; i < 10; i++) { + if (Validator.isNotNull(auth)) { + JSONObject map = JSONFactoryUtil.createJSONObject(); + map.put(ApiConstants.PARAMETER_AUTH, auth).put(ApiConstants.PARAMETER_CALLBACK_URL, + callbackURL); + map = post(ApiConstants.ENDPOINT_GET_TOKEN, map); + if (Validator.isNotNull(map)) { + switch (map.getInt(ApiConstants.LABEL_ERROR_CODE)) { + case ApiConstants.ERROR_OK: + return map.getString(ApiConstants.PARAMETER_TOKEN); + case ApiConstants.ERROR_INVALID_AUTH: + _log.info("Auth scaduto. Eseguo nuovo login"); + login(); + break; + default: + _log.error("Errore durante la creazione del token: " + map.toString()); + return null; + } + } else { + _log.error("Errore inatteso durante la creazione del token"); + } + } + } + return null; + } + + public static Map getUserInfo(String token) { + _log.info("Recupero informazioni sull'utente con il token " + token + ", auth " + auth); + JSONObject map = JSONFactoryUtil.createJSONObject(); + map.put(ApiConstants.PARAMETER_AUTH, auth).put(ApiConstants.PARAMETER_TOKEN, token); + map = post(ApiConstants.ENDPOINT_GET_USER, map); + if (Validator.isNotNull(map)) { + switch (map.getInt(ApiConstants.LABEL_ERROR_CODE)) { + case ApiConstants.ERROR_OK: + _log.info("Ho i dati dell'utente"); + Map ret = new HashMap(); + ret.put(ApiConstants.PARAMETER_FIRST_NAME, map.getString(ApiConstants.PARAMETER_FIRST_NAME)); + ret.put(ApiConstants.PARAMETER_LAST_NAME, map.getString(ApiConstants.PARAMETER_LAST_NAME)); + ret.put(ApiConstants.PARAMETER_CODICE_FISCALE, + map.getString(ApiConstants.PARAMETER_CODICE_FISCALE)); + return ret; + default: + _log.error("Errore durante il recupero dei dati utente: " + map.toString()); + } + } + return null; + } + + private static void login() { + JSONObject map = JSONFactoryUtil.createJSONObject(); + map.put(ApiConstants.PARAMETER_USERNAME, ApiConstants.API_USERNAME).put( + ApiConstants.PARAMETER_PASSWORD, ApiConstants.API_PASSWORD); + map = post(ApiConstants.ENDPOINT_LOGIN, map); + if (Validator.isNotNull(map)) { + if (ApiConstants.ERROR_OK == map.getInt(ApiConstants.LABEL_ERROR_CODE)) { + auth = map.getString(ApiConstants.PARAMETER_AUTH); + } else { + _log.error("Errore durante il login: " + map.toString()); + } + } + } + + private static JSONObject post(String endPoint, JSONObject json) { + try { + URL url = new URL(endPoint); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("User-Agent", ApiConstants.HTTP_USER_AGENT); + connection.setDoOutput(true); + OutputStream stream = connection.getOutputStream(); + _log.info("Invio: " + json.toString() + " (" + endPoint + ")"); + stream.write(json.toString().getBytes()); + stream.flush(); + stream.close(); + int code = connection.getResponseCode(); + if (code == HttpURLConnection.HTTP_OK) { + String response = readResponse(connection); + _log.info("Ricevo: " + response); + JSONObject ret = JSONFactoryUtil.createJSONObject(response); + return ret; + } + } catch (IOException | JSONException e) { + _log.error(e); + } + return null; + } + + private static String readResponse(HttpURLConnection connection) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder builder = new StringBuilder(); + String inputLine; + while ((inputLine = reader.readLine()) != null) { + builder.append(inputLine); + } + reader.close(); + + return builder.toString(); + } +} diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/portal.properties b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/portal.properties new file mode 100644 index 00000000..37b77654 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/src/portal.properties @@ -0,0 +1,2 @@ +#auth.pipeline.enable.liferay.check=false +auto.login.hooks=it.mwg.sicilia.login.SiciliaAutoLogin \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/web.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/web.xml new file mode 100644 index 00000000..ff1320bc --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/docroot/WEB-INF/web.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml new file mode 100644 index 00000000..abdd7ea2 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml.MD5 b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml.MD5 new file mode 100644 index 00000000..29d34b16 --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/ivy.xml.MD5 @@ -0,0 +1 @@ +f1563bf5bac3cf93500d1510b72dd9da diff --git a/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/test/integration/arquillian.xml b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/test/integration/arquillian.xml new file mode 100644 index 00000000..9413609b --- /dev/null +++ b/liferay-plugins-sdk-6.2/hooks/sicilia-login-hook/test/integration/arquillian.xml @@ -0,0 +1,15 @@ + + + + + + 8099 + tomcat + tomcat + + + \ No newline at end of file diff --git a/liferay-plugins-sdk-6.2/portlets/portos-fe-portlet/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/portlets/portos-fe-portlet/.settings/org.eclipse.wst.common.component index 26b0e067..e1d846ac 100644 --- a/liferay-plugins-sdk-6.2/portlets/portos-fe-portlet/.settings/org.eclipse.wst.common.component +++ b/liferay-plugins-sdk-6.2/portlets/portos-fe-portlet/.settings/org.eclipse.wst.common.component @@ -2,10 +2,10 @@ - + uses - + uses diff --git a/liferay-plugins-sdk-6.2/portlets/portos-kaleo-controller-portlet/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/portlets/portos-kaleo-controller-portlet/.settings/org.eclipse.wst.common.component index d5f205ee..5042658c 100644 --- a/liferay-plugins-sdk-6.2/portlets/portos-kaleo-controller-portlet/.settings/org.eclipse.wst.common.component +++ b/liferay-plugins-sdk-6.2/portlets/portos-kaleo-controller-portlet/.settings/org.eclipse.wst.common.component @@ -5,6 +5,9 @@ uses + + uses + diff --git a/liferay-plugins-sdk-6.2/portlets/portos-notification-portlet/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/portlets/portos-notification-portlet/.settings/org.eclipse.wst.common.component index 2a18a0ee..aae30de9 100644 --- a/liferay-plugins-sdk-6.2/portlets/portos-notification-portlet/.settings/org.eclipse.wst.common.component +++ b/liferay-plugins-sdk-6.2/portlets/portos-notification-portlet/.settings/org.eclipse.wst.common.component @@ -8,6 +8,12 @@ uses + + uses + + + uses + diff --git a/liferay-plugins-sdk-6.2/portlets/portos-report-engine-portlet/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/portlets/portos-report-engine-portlet/.settings/org.eclipse.wst.common.component index 39a7eafb..e8f246da 100644 --- a/liferay-plugins-sdk-6.2/portlets/portos-report-engine-portlet/.settings/org.eclipse.wst.common.component +++ b/liferay-plugins-sdk-6.2/portlets/portos-report-engine-portlet/.settings/org.eclipse.wst.common.component @@ -5,6 +5,9 @@ uses + + uses + diff --git a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.classpath b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.classpath index bbfce019..ca4bcf17 100644 --- a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.classpath +++ b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.classpath @@ -6,16 +6,17 @@ - - - - - + + + + + + diff --git a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.jdt.core.prefs b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..c537b630 --- /dev/null +++ b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.component b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.component new file mode 100644 index 00000000..d93ec973 --- /dev/null +++ b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.component @@ -0,0 +1,14 @@ + + + + + + uses + + + uses + + + + + diff --git a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml new file mode 100644 index 00000000..50b8c1ac --- /dev/null +++ b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.xml b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 00000000..c71dbf00 --- /dev/null +++ b/liferay-plugins-sdk-6.2/webs/portos-kaleo-web/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,9 @@ + + + + + + + + +