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.cas;
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.Validator;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PrefsPropsUtil;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.util.CollectionFactory;
34  import com.liferay.util.servlet.filters.DynamicFilterConfig;
35  
36  import java.io.IOException;
37  
38  import java.util.Map;
39  
40  import javax.servlet.Filter;
41  import javax.servlet.FilterChain;
42  import javax.servlet.ServletContext;
43  import javax.servlet.ServletException;
44  import javax.servlet.ServletRequest;
45  import javax.servlet.ServletResponse;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import javax.servlet.http.HttpSession;
49  
50  /**
51   * <a href="CASFilter.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Michael Young
54   * @author Brian Wing Shun Chan
55   * @author Raymond Augé
56   *
57   */
58  public class CASFilter extends BaseFilter {
59  
60      public static void reload(long companyId) {
61          _casFilters.remove(new Long(companyId));
62      }
63  
64      public void doFilter(
65              ServletRequest req, ServletResponse res, FilterChain chain)
66          throws IOException, ServletException {
67  
68          try {
69              HttpServletRequest httpReq = (HttpServletRequest)req;
70  
71              long companyId = PortalUtil.getCompanyId(httpReq);
72  
73              if (PrefsPropsUtil.getBoolean(
74                      companyId, PropsUtil.CAS_AUTH_ENABLED,
75                      PropsValues.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                  doFilter(CASFilter.class, req, res, chain);
98              }
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102         }
103     }
104 
105     protected Filter getCASFilter(long companyId) throws Exception {
106         Long companyIdObj = new Long(companyId);
107 
108         edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
109             (edu.yale.its.tp.cas.client.filter.CASFilter)_casFilters.get(
110                 companyIdObj);
111 
112         if (casFilter == null) {
113             casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
114 
115             DynamicFilterConfig config = new DynamicFilterConfig(
116                 _filterName, _ctx);
117 
118             String serverName = PrefsPropsUtil.getString(
119                 companyId, PropsUtil.CAS_SERVER_NAME);
120             String serviceUrl = PrefsPropsUtil.getString(
121                 companyId, PropsUtil.CAS_SERVICE_URL);
122 
123             config.addInitParameter(
124                 edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
125                 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_LOGIN_URL));
126 
127             if (Validator.isNotNull(serviceUrl)) {
128                 config.addInitParameter(
129                     edu.yale.its.tp.cas.client.filter.CASFilter.
130                         SERVICE_INIT_PARAM,
131                     serviceUrl);
132             }
133             else {
134                 config.addInitParameter(
135                     edu.yale.its.tp.cas.client.filter.CASFilter.
136                         SERVERNAME_INIT_PARAM,
137                     serverName);
138             }
139 
140             config.addInitParameter(
141                 edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
142                 PrefsPropsUtil.getString(
143                     companyId, PropsUtil.CAS_VALIDATE_URL));
144 
145             casFilter.init(config);
146 
147             _casFilters.put(companyIdObj, casFilter);
148         }
149 
150         return casFilter;
151     }
152 
153     private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
154 
155     private static Map _casFilters = CollectionFactory.getSyncHashMap();
156 
157     private String _filterName;
158     private ServletContext _ctx;
159 
160 }