1   /**
2    * Copyright (c) 2000-2007 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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PrefsPropsUtil;
29  import com.liferay.portal.util.PropsUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.util.CookieUtil;
32  
33  import java.io.IOException;
34  
35  import javax.servlet.Filter;
36  import javax.servlet.FilterChain;
37  import javax.servlet.FilterConfig;
38  import javax.servlet.ServletException;
39  import javax.servlet.ServletRequest;
40  import javax.servlet.ServletResponse;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import javax.servlet.http.HttpSession;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="OpenSSOFilter.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Prashant Dighe
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class OpenSSOFilter implements Filter {
56  
57      public void init(FilterConfig filterConfig) throws ServletException {
58      }
59  
60      public void doFilter(
61              ServletRequest req, ServletResponse res, FilterChain chain)
62          throws IOException, ServletException {
63  
64          try {
65              HttpServletRequest httpReq = (HttpServletRequest)req;
66              HttpServletResponse httpRes = (HttpServletResponse)res;
67  
68              long companyId = PortalUtil.getCompanyId(httpReq);
69  
70              boolean enabled = PrefsPropsUtil.getBoolean(
71                  companyId, PropsUtil.OPEN_SSO_AUTH_ENABLED);
72              String loginUrl = PrefsPropsUtil.getString(
73                  companyId, PropsUtil.OPEN_SSO_LOGIN_URL);
74              String logoutUrl = PrefsPropsUtil.getString(
75                  companyId, PropsUtil.OPEN_SSO_LOGOUT_URL);
76              String serviceUrl = PrefsPropsUtil.getString(
77                  companyId, PropsUtil.OPEN_SSO_SERVICE_URL);
78              String cookieName = PrefsPropsUtil.getString(
79                  companyId, PropsUtil.OPEN_SSO_SUBJECT_COOKIE_NAME);
80  
81              if (!enabled || Validator.isNull(loginUrl) ||
82                  Validator.isNull(logoutUrl) || Validator.isNull(serviceUrl) ||
83                  Validator.isNull(cookieName)) {
84  
85                  chain.doFilter(req, res);
86  
87                  return;
88              }
89  
90              String requestURI = GetterUtil.getString(httpReq.getRequestURI());
91  
92              if (requestURI.endsWith("/portal/logout")) {
93                  HttpSession httpSes = httpReq.getSession();
94  
95                  httpSes.invalidate();
96  
97                  httpRes.sendRedirect(logoutUrl);
98              }
99              else {
100                 if (isAuthenticated(httpReq, cookieName)) {
101                     chain.doFilter(req, res);
102                 }
103                 else {
104                     httpRes.sendRedirect(loginUrl);
105                 }
106             }
107         }
108         catch (Exception e) {
109             _log.error(e, e);
110         }
111     }
112 
113     public void destroy() {
114     }
115 
116     protected boolean isAuthenticated(
117         HttpServletRequest req, String cookieName) {
118 
119         String cookieValue = CookieUtil.get(req.getCookies(), cookieName);
120 
121         if (Validator.isNotNull(cookieValue)) {
122             HttpSession ses = req.getSession();
123 
124             ses.setAttribute(WebKeys.OPEN_SSO_LOGIN, cookieValue);
125 
126             return true;
127         }
128         else {
129             return false;
130         }
131     }
132 
133     private static Log _log = LogFactory.getLog(OpenSSOFilter.class);
134 
135 }