/
CASFilter.java
214 lines (158 loc) · 5.87 KB
/
CASFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.servlet.filters.sso.cas;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.HttpUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.servlet.filters.BasePortalFilter;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsValues;
import com.liferay.portal.util.WebKeys;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.CommonUtils;
import org.jasig.cas.client.validation.Assertion;
import org.jasig.cas.client.validation.Cas20ProxyTicketValidator;
import org.jasig.cas.client.validation.TicketValidator;
/**
* @author Michael Young
* @author Brian Wing Shun Chan
* @author Raymond Augé
* @author Tina Tian
* @author Zsolt Balogh
*/
public class CASFilter extends BasePortalFilter {
public static void reload(long companyId) {
_ticketValidators.remove(companyId);
}
@Override
public boolean isFilterEnabled(
HttpServletRequest request, HttpServletResponse response) {
try {
long companyId = PortalUtil.getCompanyId(request);
if (PrefsPropsUtil.getBoolean(
companyId, PropsKeys.CAS_AUTH_ENABLED,
PropsValues.CAS_AUTH_ENABLED)) {
return true;
}
}
catch (Exception e) {
_log.error(e, e);
}
return false;
}
@Override
protected Log getLog() {
return _log;
}
protected TicketValidator getTicketValidator(long companyId)
throws Exception {
TicketValidator ticketValidator = _ticketValidators.get(companyId);
if (ticketValidator != null) {
return ticketValidator;
}
String serverName = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_SERVER_NAME, PropsValues.CAS_SERVER_NAME);
String serverUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_SERVER_URL, PropsValues.CAS_SERVER_URL);
String loginUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_LOGIN_URL, PropsValues.CAS_LOGIN_URL);
Cas20ProxyTicketValidator cas20ProxyTicketValidator =
new Cas20ProxyTicketValidator(serverUrl);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("serverName", serverName);
parameters.put("casServerUrlPrefix", serverUrl);
parameters.put("casServerLoginUrl", loginUrl);
parameters.put("redirectAfterValidation", "false");
cas20ProxyTicketValidator.setCustomParameters(parameters);
_ticketValidators.put(companyId, cas20ProxyTicketValidator);
return cas20ProxyTicketValidator;
}
@Override
protected void processFilter(
HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain)
throws Exception {
HttpSession session = request.getSession();
long companyId = PortalUtil.getCompanyId(request);
String pathInfo = request.getPathInfo();
Object forceLogout = session.getAttribute(WebKeys.CAS_FORCE_LOGOUT);
if (forceLogout != null) {
session.removeAttribute(WebKeys.CAS_FORCE_LOGOUT);
String logoutUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_LOGOUT_URL,
PropsValues.CAS_LOGOUT_URL);
response.sendRedirect(logoutUrl);
return;
}
if (Validator.isNotNull(pathInfo) &&
pathInfo.contains("/portal/logout")) {
session.invalidate();
String logoutUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_LOGOUT_URL,
PropsValues.CAS_LOGOUT_URL);
response.sendRedirect(logoutUrl);
return;
}
else {
String login = (String)session.getAttribute(WebKeys.CAS_LOGIN);
if (Validator.isNotNull(login)) {
processFilter(CASFilter.class, request, response, filterChain);
return;
}
String serverName = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_SERVER_NAME,
PropsValues.CAS_SERVER_NAME);
String serviceUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_SERVICE_URL,
PropsValues.CAS_SERVICE_URL);
if (Validator.isNull(serviceUrl)) {
serviceUrl = CommonUtils.constructServiceUrl(
request, response, serviceUrl, serverName, "ticket", false);
}
String ticket = ParamUtil.getString(request, "ticket");
if (Validator.isNull(ticket)) {
String loginUrl = PrefsPropsUtil.getString(
companyId, PropsKeys.CAS_LOGIN_URL,
PropsValues.CAS_LOGIN_URL);
loginUrl = HttpUtil.addParameter(
loginUrl, "service", serviceUrl);
response.sendRedirect(loginUrl);
return;
}
TicketValidator ticketValidator = getTicketValidator(companyId);
Assertion assertion = ticketValidator.validate(ticket, serviceUrl);
if (assertion != null) {
AttributePrincipal attributePrincipal =
assertion.getPrincipal();
login = attributePrincipal.getName();
session.setAttribute(WebKeys.CAS_LOGIN, login);
}
}
processFilter(CASFilter.class, request, response, filterChain);
}
private static Log _log = LogFactoryUtil.getLog(CASFilter.class);
private static Map<Long, TicketValidator> _ticketValidators =
new ConcurrentHashMap<Long, TicketValidator>();
}