2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import java.net.URLDecoder;
41 import java.net.URLEncoder;
42 import java.util.HashMap;
43 import java.util.List;
46 import javax.servlet.http.Cookie;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpSession;
50 import org.onap.portalsdk.core.auth.LoginStrategy;
51 import org.onap.portalsdk.core.command.LoginBean;
52 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
53 import org.onap.portalsdk.core.domain.RoleFunction;
54 import org.onap.portalsdk.core.domain.User;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.menu.MenuProperties;
57 import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
58 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
59 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
60 import org.onap.portalsdk.core.service.LoginService;
61 import org.onap.portalsdk.core.service.RoleService;
62 import org.onap.portalsdk.core.util.SystemProperties;
63 import org.onap.portalsdk.core.web.support.AppUtils;
64 import org.onap.portalsdk.core.web.support.UserUtils;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.stereotype.Controller;
67 import org.springframework.web.bind.annotation.RequestMapping;
68 import org.springframework.web.bind.annotation.RequestMethod;
69 import org.springframework.web.servlet.ModelAndView;
70 import org.springframework.web.util.WebUtils;
74 public class SingleSignOnController extends UnRestrictedBaseController {
76 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SingleSignOnController.class);
79 private LoginService loginService;
82 private LoginStrategy loginStrategy;
85 private RoleService roleService;
87 private String viewName;
88 private String welcomeView;
90 public String getWelcomeView() {
94 public void setWelcomeView(String welcomeView) {
95 this.welcomeView = welcomeView;
99 * Handles requests directed to the single sign-on page by the session timeout
103 * @return Redirect to an appropriate address
106 @RequestMapping(value = { "/single_signon.htm" }, method = RequestMethod.GET)
107 public ModelAndView singleSignOnLogin(HttpServletRequest request) throws Exception {
109 Map<String, String> model = new HashMap<>();
110 HashMap<String, String> additionalParamsMap = new HashMap<>();
111 LoginBean commandBean = new LoginBean();
113 // SessionTimeoutInterceptor sets these parameters
114 String forwardURL = URLDecoder.decode(request.getParameter("forwardURL"), "UTF-8");
115 String redirectToPortal = request.getParameter("redirectToPortal");
117 if (isLoginCookieExist(request) && redirectToPortal == null) {
118 HttpSession session = AppUtils.getSession(request);
119 User user = UserUtils.getUserSession(request);
120 if (session == null || user == null) {
122 final String authMech = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
123 String userId = loginStrategy.getUserId(request);
124 commandBean.setUserid(userId);
125 commandBean = getLoginService().findUser(commandBean,
126 (String) request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY),
127 additionalParamsMap);
128 List<RoleFunction> roleFunctionList = roleService.getRoleFunctions(userId);
129 if (commandBean.getUser() == null) {
130 String loginErrorMessage = (commandBean.getLoginErrorMessage() != null)
131 ? commandBean.getLoginErrorMessage()
132 : SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_NOT_FOUND;
133 model.put(LoginStrategy.ERROR_MESSAGE_KEY, SystemProperties.getProperty(loginErrorMessage));
134 final String redirectUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL)
135 + "?noUserError=Yes";
136 logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: user is null, redirect URL is {}",
138 return new ModelAndView("redirect:" + redirectUrl);
140 // store the user's information in the session
142 if (null == authMech || "".equals(authMech) || "BOTH".equals(authMech)) {
143 loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP);
144 } else if ("CSP".equals(authMech)) {
145 loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP);
147 loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_WEB_JUNCTION);
149 UserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(),
150 commandBean.getBusinessDirectMenu(), loginMethod, roleFunctionList);
151 initateSessionMgtHandler(request);
152 logger.debug(EELFLoggerDelegate.debugLogger,
153 "singleSignOnLogin: create new user session for expired user {}; user {} exists in the system",
154 userId, commandBean.getUser().getOrgUserId());
155 return new ModelAndView("redirect:" + forwardURL);
157 } // user is null or session is null
159 // both user and session are non-null.
160 logger.info(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: redirecting to the forwardURL {}",
162 return new ModelAndView("redirect:" + forwardURL);
167 * Login cookie not found, or redirect-to-portal parameter was found.
169 * Redirect the user to the portal with a suitable return URL. The forwardURL
170 * parameter that arrives as a parameter is a partial (not absolute) request
171 * path for a page in the application. The challenge here is to compute the
172 * correct absolute path for the original request so the portal can redirect the
173 * user back to the right place. If the application sits behind WebJunction, or
174 * if separate FE-BE hosts are used, then the URL yielded by the request has a
175 * host name that is not reachable by the user.
177 String returnToAppUrl;
178 if (SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)) {
179 // New feature as of 1610, release 3.3.3:
180 // application can publish a base URL in system.properties
181 String appUrl = SystemProperties.getProperty(SystemProperties.APP_BASE_URL);
182 returnToAppUrl = appUrl + (appUrl.endsWith("/") ? "" : "/") + forwardURL;
183 logger.debug(EELFLoggerDelegate.debugLogger,
184 "singleSignOnLogin: using app base URL {} and redirectURL {}", appUrl, returnToAppUrl);
186 // Be backward compatible with applications that don't need this
188 // This is the controller for the single_signon.htm page, so the
190 // should always find the specified token.
191 returnToAppUrl = request.getRequestURL().toString().replace("single_signon.htm",
193 logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: computed redirectURL {}",
196 final String encodedReturnToAppUrl = URLEncoder.encode(returnToAppUrl, "UTF-8");
197 // Also send the application's UEB key so Portal can block URL
198 // reflection attacks.
199 final String uebAppKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY);
200 final String url = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL);
201 final String portalUrl = url.substring(0, url.lastIndexOf('/')) + "/process_csp";
202 final String redirectUrl = portalUrl + "?uebAppKey=" + uebAppKey + "&redirectUrl=" + encodedReturnToAppUrl;
203 logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: portal-bound redirect URL is {}",
206 // this line may not be necessary but jsessionid coockie is not getting created in all cases;
207 // so force the cookie creation
208 request.getSession(true);
210 return new ModelAndView("redirect:" + redirectUrl);
214 protected void initateSessionMgtHandler(HttpServletRequest request) {
215 String portalJSessionId = getPortalJSessionId(request);
216 String jSessionId = getJessionId(request);
217 PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, AppUtils.getSession(request));
220 public boolean isLoginCookieExist(HttpServletRequest request) {
221 Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE);
225 public String getPortalJSessionId(HttpServletRequest request) {
226 Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE);
227 return ep.getValue();
230 public String getJessionId(HttpServletRequest request) {
231 return request.getSession().getId();
235 public String getViewName() {
240 public void setViewName(String viewName) {
241 this.viewName = viewName;
244 public LoginService getLoginService() {
248 public void setLoginService(LoginService loginService) {
249 this.loginService = loginService;