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.servlet.filters.DynamicFilterConfig;
34  
35  import java.io.IOException;
36  
37  import java.util.Map;
38  import java.util.concurrent.ConcurrentHashMap;
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(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         edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
107             _casFilters.get(companyId);
108 
109         if (casFilter == null) {
110             casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
111 
112             DynamicFilterConfig config = new DynamicFilterConfig(
113                 _filterName, _ctx);
114 
115             String serverName = PrefsPropsUtil.getString(
116                 companyId, PropsUtil.CAS_SERVER_NAME);
117             String serviceUrl = PrefsPropsUtil.getString(
118                 companyId, PropsUtil.CAS_SERVICE_URL);
119 
120             config.addInitParameter(
121                 edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
122                 PrefsPropsUtil.getString(companyId, PropsUtil.CAS_LOGIN_URL));
123 
124             if (Validator.isNotNull(serviceUrl)) {
125                 config.addInitParameter(
126                     edu.yale.its.tp.cas.client.filter.CASFilter.
127                         SERVICE_INIT_PARAM,
128                     serviceUrl);
129             }
130             else {
131                 config.addInitParameter(
132                     edu.yale.its.tp.cas.client.filter.CASFilter.
133                         SERVERNAME_INIT_PARAM,
134                     serverName);
135             }
136 
137             config.addInitParameter(
138                 edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
139                 PrefsPropsUtil.getString(
140                     companyId, PropsUtil.CAS_VALIDATE_URL));
141 
142             casFilter.init(config);
143 
144             _casFilters.put(companyId, casFilter);
145         }
146 
147         return casFilter;
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
151 
152     private static Map<Long, edu.yale.its.tp.cas.client.filter.CASFilter>
153         _casFilters = new ConcurrentHashMap
154             <Long, edu.yale.its.tp.cas.client.filter.CASFilter>();
155 
156     private String _filterName;
157     private ServletContext _ctx;
158 
159 }