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.PropsKeys;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.util.servlet.filters.DynamicFilterConfig;
34  
35  import java.util.Map;
36  import java.util.concurrent.ConcurrentHashMap;
37  
38  import javax.servlet.Filter;
39  import javax.servlet.FilterChain;
40  import javax.servlet.ServletContext;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import javax.servlet.http.HttpSession;
44  
45  /**
46   * <a href="CASFilter.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Michael Young
49   * @author Brian Wing Shun Chan
50   * @author Raymond Augé
51   *
52   */
53  public class CASFilter extends BaseFilter {
54  
55      public static void reload(long companyId) {
56          _casFilters.remove(companyId);
57      }
58  
59      protected Filter getCASFilter(long companyId) throws Exception {
60          edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
61              _casFilters.get(companyId);
62  
63          if (casFilter == null) {
64              casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
65  
66              DynamicFilterConfig config = new DynamicFilterConfig(
67                  _filterName, _servletContext);
68  
69              String serverName = PrefsPropsUtil.getString(
70                  companyId, PropsKeys.CAS_SERVER_NAME);
71              String serviceUrl = PrefsPropsUtil.getString(
72                  companyId, PropsKeys.CAS_SERVICE_URL);
73  
74              config.addInitParameter(
75                  edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
76                  PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGIN_URL));
77  
78              if (Validator.isNotNull(serviceUrl)) {
79                  config.addInitParameter(
80                      edu.yale.its.tp.cas.client.filter.CASFilter.
81                          SERVICE_INIT_PARAM,
82                      serviceUrl);
83              }
84              else {
85                  config.addInitParameter(
86                      edu.yale.its.tp.cas.client.filter.CASFilter.
87                          SERVERNAME_INIT_PARAM,
88                      serverName);
89              }
90  
91              config.addInitParameter(
92                  edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
93                  PrefsPropsUtil.getString(
94                      companyId, PropsKeys.CAS_VALIDATE_URL));
95  
96              casFilter.init(config);
97  
98              _casFilters.put(companyId, casFilter);
99          }
100 
101         return casFilter;
102     }
103 
104     protected Log getLog() {
105         return _log;
106     }
107 
108     protected void processFilter(
109         HttpServletRequest request, HttpServletResponse response,
110         FilterChain filterChain) {
111 
112         try {
113             long companyId = PortalUtil.getCompanyId(request);
114 
115             if (PrefsPropsUtil.getBoolean(
116                     companyId, PropsKeys.CAS_AUTH_ENABLED,
117                     PropsValues.CAS_AUTH_ENABLED)) {
118 
119                 String pathInfo = request.getPathInfo();
120 
121                 if (pathInfo.indexOf("/portal/logout") != -1) {
122                     HttpSession session = request.getSession();
123 
124                     session.invalidate();
125 
126                     String logoutUrl = PrefsPropsUtil.getString(
127                         companyId, PropsKeys.CAS_LOGOUT_URL);
128 
129                     response.sendRedirect(logoutUrl);
130                 }
131                 else {
132                     Filter casFilter = getCASFilter(companyId);
133 
134                     casFilter.doFilter(request, response, filterChain);
135                 }
136             }
137             else {
138                 processFilter(CASFilter.class, request, response, filterChain);
139             }
140         }
141         catch (Exception e) {
142             _log.error(e, e);
143         }
144     }
145 
146     private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
147 
148     private static Map<Long, edu.yale.its.tp.cas.client.filter.CASFilter>
149         _casFilters = new ConcurrentHashMap
150             <Long, edu.yale.its.tp.cas.client.filter.CASFilter>();
151 
152     private String _filterName;
153     private ServletContext _servletContext;
154 
155 }