2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sparky.security.filter;
25 import java.io.IOException;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.Cookie;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpSession;
37 import javax.ws.rs.core.HttpHeaders;
39 import org.openecomp.cl.api.Logger;
40 import org.openecomp.cl.eelf.LoggerFactory;
41 import org.openecomp.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
42 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
43 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
44 import org.openecomp.portalsdk.core.onboarding.util.SSOUtil;
45 import org.openecomp.sparky.logging.AaiUiMsgs;
46 import org.openecomp.sparky.security.EcompSso;
47 import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig;
50 * This filter checks every request for proper ECOMP Portal single sign on initialization. The
51 * possible paths and actions:
53 * <LI>User starts at an app page via a bookmark. No ECOMP portal cookie is set. Redirect there to
54 * get one; then continue as below.
55 * <LI>User starts at ECOMP Portal and goes to app. Alternately, the user's session times out and
56 * the user hits refresh. The ECOMP Portal cookie is set, but there is no valid session. Create one
58 * <LI>User has valid ECOMP Portal cookie and session. Reset the max idle in that session.
63 * <LI>Portal Session should be up prior to App Session</LI>
64 * <LI>If App Session Expires or if EPService cookie is unavailable, we need to redirect to Portal.
65 * <LI>Method {@link #initiateSessionMgtHandler(HttpServletRequest)} should be called for Session
66 * management when the initial session is created
67 * <LI>While redirecting, the cookie "redirectUrl" should also be set so that Portal knows where to
68 * forward the request to once the Portal Session is created and EPService cookie is set.
69 * <LI>Method {@link #resetSessionMaxIdleTimeOut(HttpServletRequest)} should be called for every
70 * request to reset the MaxInactiveInterval to the right value.
73 * This filter incorporates most features of the SDK application's SessionTimeoutInterceptor and
74 * SingleSignOnController classes
76 public class LoginFilter implements Filter {
78 private static final Logger LOG = LoggerFactory.getInstance().getLogger(LoginFilter.class);
81 public void init(FilterConfig filterConfig) throws ServletException {
82 // Validate that app has provided useful portal properties
83 if (PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL) == null) {
84 throw new ServletException("Failed to find URL in portal.properties");
87 PortalAuthenticationConfig appProperties;
89 appProperties = PortalAuthenticationConfig.getInstance();
90 } catch (Exception ex) {
91 throw new ServletException("Failed to get properties", ex);
94 String restUser = appProperties.getUsername();
95 String restPassword = appProperties.getPassword();
96 if (restUser == null || restPassword == null) {
97 throw new ServletException("Failed to find user and/or password from properties");
102 public void destroy() {
103 // No resources to release
109 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
110 * javax.servlet.FilterChain)
113 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
114 throws ServletException, IOException {
115 HttpServletRequest request = (HttpServletRequest) req;
116 HttpServletResponse response = (HttpServletResponse) res;
118 // Choose authentication appropriate for the request.
119 final String restApiURI = request.getContextPath() + PortalApiConstants.API_PREFIX;
120 if (request.getRequestURI().startsWith(restApiURI)) {
121 // REST servlet checks credentials
122 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "doFilter: delegating auth to REST servlet for request " + request.getRequestURI());
123 chain.doFilter(request, response);
125 // All other requests require ECOMP Portal authentication
126 if (EcompSso.validateEcompSso(request) == null) {
127 String redirectURL, logMessage;
129 // Redirect to Portal UI
130 redirectURL = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL);
131 logMessage = "Unauthorized login attempt.";
133 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
135 " | Remote IP: " + request.getRemoteAddr() +
136 " | User agent: " + request.getHeader(HttpHeaders.USER_AGENT) +
137 " | Request URL: " + request.getRequestURL() +
138 " | Redirecting to: " + redirectURL);
140 response.sendRedirect(redirectURL);
142 HttpSession session = request.getSession(false);
143 if (session == null) {
145 session = request.getSession(true);
146 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "doFilter: created new session " + session.getId());
147 initiateSessionMgtHandler(request);
150 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "doFilter: resetting idle in existing session " + session.getId());
151 resetSessionMaxIdleTimeOut(request);
153 // Pass request back down the filter chain
154 chain.doFilter(request, response);
160 * Publishes information about the session.
164 private void initiateSessionMgtHandler(HttpServletRequest request) {
165 String portalJSessionId = getPortalJSessionId(request);
166 String jSessionId = getJessionId(request);
167 storeMaxInactiveTime(request);
168 PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, request.getSession(false));
172 * Gets the ECOMP Portal service cookie value.
175 * @return Cookie value, or null if not found.
177 private String getPortalJSessionId(HttpServletRequest request) {
178 Cookie ep = EcompSso.getCookie(request, EcompSso.EP_SERVICE);
179 return ep == null ? null : ep.getValue();
183 * Gets the container session ID.
186 * @return Session ID, or null if no session.
188 private String getJessionId(HttpServletRequest request) {
189 HttpSession session = request.getSession();
190 return session == null ? null : session.getId();
194 * Sets the global session's max idle time to the session's max inactive interval.
198 private void storeMaxInactiveTime(HttpServletRequest request) {
199 HttpSession session = request.getSession(false);
201 && session.getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME) == null) {
202 session.setAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME,
203 session.getMaxInactiveInterval());
208 * Sets the session's max inactive interval.
212 private void resetSessionMaxIdleTimeOut(HttpServletRequest request) {
214 HttpSession session = request.getSession(false);
215 if (session != null) {
216 final Object maxIdleAttribute = session
217 .getAttribute(PortalApiConstants.GLOBAL_SESSION_MAX_IDLE_TIME);
218 if (maxIdleAttribute != null) {
219 session.setMaxInactiveInterval(Integer.parseInt(maxIdleAttribute.toString()));
222 } catch (Exception e) {
223 LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO, "resetSessionMaxIdleTimeOut: failed to set session max inactive interval - " + e.getLocalizedMessage());