/
AuthenticationFilter.java
217 lines (186 loc) · 8.86 KB
/
AuthenticationFilter.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
215
216
217
/**
* Copyright (C) 2022 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* 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
* version 2.1 of the License.
* 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.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.console.common.server.login.filter;
import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.bonitasoft.console.common.server.auth.AuthenticationManager;
import org.bonitasoft.console.common.server.auth.AuthenticationManagerFactory;
import org.bonitasoft.console.common.server.auth.AuthenticationManagerNotFoundException;
import org.bonitasoft.console.common.server.filter.ExcludingPatternFilter;
import org.bonitasoft.console.common.server.login.HttpServletRequestAccessor;
import org.bonitasoft.console.common.server.login.utils.LoginUrl;
import org.bonitasoft.console.common.server.login.utils.RedirectUrl;
import org.bonitasoft.console.common.server.login.utils.RedirectUrlBuilder;
import org.bonitasoft.console.common.server.utils.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Vincent Elcrin
* @author Anthony Birembaut
*/
public class AuthenticationFilter extends ExcludingPatternFilter {
protected static final String AUTHENTICATION_FILTER_EXCLUDED_PAGES_PATTERN = "^/(bonita/)?(login.jsp$)|(apps/.+/API/)|(portal/resource/.+/API/)";
protected static final String REDIRECT_PARAM = "redirectWhenUnauthorized";
protected static final String MAINTENANCE_JSP = "/maintenance.jsp";
protected static final String USER_NOT_FOUND_JSP = "/usernotfound.jsp";
protected boolean redirectWhenUnauthorized;
/**
* Logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class.getName());
private final LinkedList<AuthenticationRule> rules = new LinkedList<>();
public AuthenticationFilter() {
addRules();
}
protected void addRules() {
addRule(new AlreadyLoggedInRule());
}
protected void addRule(final AuthenticationRule rule) {
rules.add(rule);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String redirectInitParam = filterConfig.getInitParameter(REDIRECT_PARAM);
redirectWhenUnauthorized = redirectInitParam != null ? Boolean.parseBoolean(redirectInitParam) : true;
super.init(filterConfig);
}
@Override
public String getDefaultExcludedPages() {
return AUTHENTICATION_FILTER_EXCLUDED_PAGES_PATTERN;
}
@Override
public void proceedWithFiltering(final ServletRequest request, final ServletResponse response,
final FilterChain chain) throws ServletException, IOException {
final HttpServletRequestAccessor requestAccessor = new HttpServletRequestAccessor((HttpServletRequest) request);
doAuthenticationFiltering(requestAccessor, (HttpServletResponse) response, chain);
}
protected void doAuthenticationFiltering(final HttpServletRequestAccessor requestAccessor,
final HttpServletResponse response,
final FilterChain chain) throws ServletException, IOException {
try {
if (!isAuthorized(requestAccessor, response, chain)) {
if (!isAccessingPlatformAPIWithAPISession(requestAccessor)) {
//do not invalidate the API session when trying to access platform API without a platform session
//fix RUNTIME-1562
cleanHttpSession(requestAccessor.getHttpSession());
}
if (redirectWhenUnauthorized && requestAccessor.asHttpServletRequest().getMethod().equals("GET")) {
response.sendRedirect(createLoginPageUrl(requestAccessor).getLocation());
} else {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}
} catch (TenantIsPausedException e) {
handleTenantPausedException(requestAccessor, response, e);
} catch (final EngineUserNotFoundOrInactive e) {
handleUserNotFoundOrInactiveException(requestAccessor, response, e);
}
}
protected boolean isAccessingPlatformAPIWithAPISession(final HttpServletRequestAccessor requestAccessor) {
String requestPath = requestAccessor.asHttpServletRequest().getServletPath()
+ requestAccessor.asHttpServletRequest().getPathInfo();
return requestPath.matches(RestAPIAuthorizationFilter.PLATFORM_API_URI_REGEXP)
&& requestAccessor.getApiSession() != null;
}
/**
* @return true if one of the rules pass false otherwise
*/
protected boolean isAuthorized(final HttpServletRequestAccessor requestAccessor,
final HttpServletResponse response,
final FilterChain chain) throws ServletException, IOException {
for (final AuthenticationRule rule : getRules()) {
if (rule.doAuthorize(requestAccessor, response)) {
rule.proceedWithRequest(chain, requestAccessor.asHttpServletRequest(), response);
return true;
}
}
return false;
}
protected void handleUserNotFoundOrInactiveException(final HttpServletRequestAccessor requestAccessor,
final HttpServletResponse response,
final EngineUserNotFoundOrInactive e) throws ServletException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("redirection to user not found page : " + e.getMessage(), e);
}
if (redirectWhenUnauthorized && requestAccessor.asHttpServletRequest().getMethod().equals("GET")) {
redirectTo(requestAccessor, response, USER_NOT_FOUND_JSP);
} else {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
}
protected void handleTenantPausedException(final HttpServletRequestAccessor requestAccessor,
final HttpServletResponse response,
final TenantIsPausedException e) throws ServletException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("redirection to maintenance page : " + e.getMessage(), e);
}
if (redirectWhenUnauthorized && requestAccessor.asHttpServletRequest().getMethod().equals("GET")) {
redirectTo(requestAccessor, response, MAINTENANCE_JSP);
} else {
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}
}
/**
* manage redirection to maintenance page
*
* @param request
* @param response
* @param pagePath
*/
protected void redirectTo(final HttpServletRequestAccessor request, final HttpServletResponse response,
final String pagePath)
throws ServletException {
try {
response.sendRedirect(request.asHttpServletRequest().getContextPath() + pagePath);
} catch (final IOException e) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info(e.getMessage());
}
}
}
protected LinkedList<AuthenticationRule> getRules() {
return rules;
}
@Override
public void destroy() {
}
// protected for test stubbing
protected AuthenticationManager getAuthenticationManager() throws ServletException {
try {
return AuthenticationManagerFactory.getAuthenticationManager();
} catch (final AuthenticationManagerNotFoundException e) {
throw new ServletException(e);
}
}
protected RedirectUrl makeRedirectUrl(final HttpServletRequestAccessor httpRequest) {
final RedirectUrlBuilder builder = new RedirectUrlBuilder(httpRequest.getRequestedUri());
builder.appendParameters(httpRequest.getParameterMap());
return builder.build();
}
protected LoginUrl createLoginPageUrl(final HttpServletRequestAccessor requestAccessor) throws ServletException {
return new LoginUrl(getAuthenticationManager(),
makeRedirectUrl(requestAccessor).getUrl(), requestAccessor);
}
protected void cleanHttpSession(final HttpSession session) {
SessionUtil.sessionLogout(session);
}
}