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.cas;
24  
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.PrefsPropsUtil;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.util.CollectionFactory;
29  import com.liferay.util.servlet.filters.DynamicFilterConfig;
30  
31  import java.io.IOException;
32  
33  import java.util.Map;
34  
35  import javax.servlet.Filter;
36  import javax.servlet.FilterChain;
37  import javax.servlet.FilterConfig;
38  import javax.servlet.ServletContext;
39  import javax.servlet.ServletException;
40  import javax.servlet.ServletRequest;
41  import javax.servlet.ServletResponse;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.http.HttpSession;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  /**
50   * <a href="CASFilter.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Michael Young
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class CASFilter implements Filter {
57  
58      public static void reload(long companyId) {
59          _casFilters.remove(new Long(companyId));
60      }
61  
62      public void init(FilterConfig config) throws ServletException {
63      }
64  
65      public void doFilter(
66              ServletRequest req, ServletResponse res, FilterChain chain)
67          throws IOException, ServletException {
68  
69          try {
70              HttpServletRequest httpReq = (HttpServletRequest)req;
71  
72              long companyId = PortalUtil.getCompanyId(httpReq);
73  
74              if (PrefsPropsUtil.getBoolean(
75                      companyId, PropsUtil.CAS_AUTH_ENABLED)) {
76  
77                  String pathInfo = httpReq.getPathInfo();
78  
79                  if (pathInfo.indexOf("/portal/logout") != -1) {
80                      HttpServletResponse httpRes = (HttpServletResponse)res;
81                      HttpSession httpSes = httpReq.getSession();
82  
83                      httpSes.invalidate();
84  
85                      String logoutUrl = PrefsPropsUtil.getString(
86                          companyId, PropsUtil.CAS_LOGOUT_URL);
87  
88                      httpRes.sendRedirect(logoutUrl);
89                  }
90                  else {
91                      Filter casFilter = getCASFilter(companyId);
92  
93                      casFilter.doFilter(req, res, chain);
94                  }
95              }
96              else {
97                  chain.doFilter(req, res);
98              }
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102         }
103     }
104 
105     public void destroy() {
106     }
107 
108     protected Filter getCASFilter(long companyId) throws Exception {
109         Long companyIdObj = new Long(companyId);
110 
111         edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
112             (edu.yale.its.tp.cas.client.filter.CASFilter)_casFilters.get(
113                 companyIdObj);
114 
115         if (casFilter == null) {
116             casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
117 
118             DynamicFilterConfig config = new DynamicFilterConfig(
119                 _filterName, _ctx);
120 
121             config.addInitParameter(
122                 edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
123                 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_LOGIN_URL));
124             config.addInitParameter(
125                 edu.yale.its.tp.cas.client.filter.CASFilter.SERVICE_INIT_PARAM,
126                 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_SERVICE_URL));
127             config.addInitParameter(
128                 edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
129                 PrefsPropsUtil.getString(
130                     companyId, PropsUtil.CAS_VALIDATE_URL));
131 
132             casFilter.init(config);
133 
134             _casFilters.put(companyIdObj, casFilter);
135         }
136 
137         return casFilter;
138     }
139 
140     private static Log _log = LogFactory.getLog(CASFilter.class);
141 
142     private static Map _casFilters = CollectionFactory.getSyncHashMap();
143 
144     private String _filterName;
145     private ServletContext _ctx;
146 
147 }