1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.filters.sso.cas;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.servlet.BaseFilter;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.PrefsPropsUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsValues;
30  import com.liferay.util.servlet.filters.DynamicFilterConfig;
31  
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import javax.servlet.Filter;
36  import javax.servlet.FilterChain;
37  import javax.servlet.ServletContext;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.http.HttpSession;
41  
42  /**
43   * <a href="CASFilter.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Michael Young
46   * @author Brian Wing Shun Chan
47   * @author Raymond Augé
48   *
49   */
50  public class CASFilter extends BaseFilter {
51  
52      public static void reload(long companyId) {
53          _casFilters.remove(companyId);
54      }
55  
56      protected Filter getCASFilter(long companyId) throws Exception {
57          edu.yale.its.tp.cas.client.filter.CASFilter casFilter =
58              _casFilters.get(companyId);
59  
60          if (casFilter == null) {
61              casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();
62  
63              DynamicFilterConfig config = new DynamicFilterConfig(
64                  _filterName, _servletContext);
65  
66              String serverName = PrefsPropsUtil.getString(
67                  companyId, PropsKeys.CAS_SERVER_NAME,
68                  PropsValues.CAS_SERVER_NAME);
69              String serviceUrl = PrefsPropsUtil.getString(
70                  companyId, PropsKeys.CAS_SERVICE_URL,
71                  PropsValues.CAS_SERVICE_URL);
72  
73              config.addInitParameter(
74                  edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
75                  PrefsPropsUtil.getString(
76                      companyId, PropsKeys.CAS_LOGIN_URL,
77                      PropsValues.CAS_LOGIN_URL));
78  
79              if (Validator.isNotNull(serviceUrl)) {
80                  config.addInitParameter(
81                      edu.yale.its.tp.cas.client.filter.CASFilter.
82                          SERVICE_INIT_PARAM,
83                      serviceUrl);
84              }
85              else {
86                  config.addInitParameter(
87                      edu.yale.its.tp.cas.client.filter.CASFilter.
88                          SERVERNAME_INIT_PARAM,
89                      serverName);
90              }
91  
92              config.addInitParameter(
93                  edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
94                  PrefsPropsUtil.getString(
95                      companyId, PropsKeys.CAS_VALIDATE_URL,
96                      PropsValues.CAS_VALIDATE_URL));
97  
98              casFilter.init(config);
99  
100             _casFilters.put(companyId, casFilter);
101         }
102 
103         return casFilter;
104     }
105 
106     protected Log getLog() {
107         return _log;
108     }
109 
110     protected void processFilter(
111         HttpServletRequest request, HttpServletResponse response,
112         FilterChain filterChain) {
113 
114         try {
115             long companyId = PortalUtil.getCompanyId(request);
116 
117             if (PrefsPropsUtil.getBoolean(
118                     companyId, PropsKeys.CAS_AUTH_ENABLED,
119                     PropsValues.CAS_AUTH_ENABLED)) {
120 
121                 String pathInfo = request.getPathInfo();
122 
123                 if (pathInfo.indexOf("/portal/logout") != -1) {
124                     HttpSession session = request.getSession();
125 
126                     session.invalidate();
127 
128                     String logoutUrl = PrefsPropsUtil.getString(
129                         companyId, PropsKeys.CAS_LOGOUT_URL,
130                         PropsValues.CAS_LOGOUT_URL);
131 
132                     response.sendRedirect(logoutUrl);
133                 }
134                 else {
135                     Filter casFilter = getCASFilter(companyId);
136 
137                     casFilter.doFilter(request, response, filterChain);
138                 }
139             }
140             else {
141                 processFilter(CASFilter.class, request, response, filterChain);
142             }
143         }
144         catch (Exception e) {
145             _log.error(e, e);
146         }
147     }
148 
149     private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
150 
151     private static Map<Long, edu.yale.its.tp.cas.client.filter.CASFilter>
152         _casFilters = new ConcurrentHashMap
153             <Long, edu.yale.its.tp.cas.client.filter.CASFilter>();
154 
155     private String _filterName;
156     private ServletContext _servletContext;
157 
158 }