1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.sso.opensso;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.BaseFilter;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PrefsPropsUtil;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.util.CookieUtil;
36  
37  import java.io.IOException;
38  
39  import javax.servlet.FilterChain;
40  import javax.servlet.ServletException;
41  import javax.servlet.ServletRequest;
42  import javax.servlet.ServletResponse;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  import javax.servlet.http.HttpSession;
46  
47  /**
48   * <a href="OpenSSOFilter.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Prashant Dighe
51   * @author Brian Wing Shun Chan
52   * @author Raymond Augé
53   *
54   */
55  public class OpenSSOFilter extends BaseFilter {
56  
57      public void doFilter(
58              ServletRequest req, ServletResponse res, FilterChain chain)
59          throws IOException, ServletException {
60  
61          try {
62              HttpServletRequest httpReq = (HttpServletRequest)req;
63              HttpServletResponse httpRes = (HttpServletResponse)res;
64  
65              long companyId = PortalUtil.getCompanyId(httpReq);
66  
67              boolean enabled = PrefsPropsUtil.getBoolean(
68                  companyId, PropsUtil.OPEN_SSO_AUTH_ENABLED,
69                  PropsValues.OPEN_SSO_AUTH_ENABLED);
70              String loginUrl = PrefsPropsUtil.getString(
71                  companyId, PropsUtil.OPEN_SSO_LOGIN_URL,
72                  PropsValues.OPEN_SSO_LOGIN_URL);
73              String logoutUrl = PrefsPropsUtil.getString(
74                  companyId, PropsUtil.OPEN_SSO_LOGOUT_URL,
75                  PropsValues.OPEN_SSO_LOGOUT_URL);
76              String serviceUrl = PrefsPropsUtil.getString(
77                  companyId, PropsUtil.OPEN_SSO_SERVICE_URL,
78                  PropsValues.OPEN_SSO_SERVICE_URL);
79              String cookieName = PrefsPropsUtil.getString(
80                  companyId, PropsUtil.OPEN_SSO_SUBJECT_COOKIE_NAME,
81                  PropsValues.OPEN_SSO_SUBJECT_COOKIE_NAME);
82  
83              if (!enabled || Validator.isNull(loginUrl) ||
84                  Validator.isNull(logoutUrl) || Validator.isNull(serviceUrl) ||
85                  Validator.isNull(cookieName)) {
86  
87                  doFilter(OpenSSOFilter.class, req, res, chain);
88  
89                  return;
90              }
91  
92              String requestURI = GetterUtil.getString(httpReq.getRequestURI());
93  
94              if (requestURI.endsWith("/portal/logout")) {
95                  HttpSession httpSes = httpReq.getSession();
96  
97                  httpSes.invalidate();
98  
99                  httpRes.sendRedirect(logoutUrl);
100             }
101             else {
102                 if (isAuthenticated(httpReq, cookieName)) {
103                     doFilter(OpenSSOFilter.class, req, res, chain);
104                 }
105                 else {
106                     httpRes.sendRedirect(loginUrl);
107                 }
108             }
109         }
110         catch (Exception e) {
111             _log.error(e, e);
112         }
113     }
114 
115     protected boolean isAuthenticated(
116         HttpServletRequest req, String cookieName) {
117 
118         String cookieValue = CookieUtil.get(req, cookieName);
119 
120         if (Validator.isNotNull(cookieValue)) {
121             HttpSession ses = req.getSession();
122 
123             ses.setAttribute(WebKeys.OPEN_SSO_LOGIN, cookieValue);
124 
125             return true;
126         }
127         else {
128             return false;
129         }
130     }
131 
132     private static Log _log = LogFactoryUtil.getLog(OpenSSOFilter.class);
133 
134 }