2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.controller;
22 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
24 import java.net.URLDecoder;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.servlet.http.Cookie;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.json.JSONObject;
34 import org.openecomp.portalapp.command.EPLoginBean;
35 import org.openecomp.portalapp.portal.domain.SharedContext;
36 import org.openecomp.portalapp.portal.service.EPLoginService;
37 import org.openecomp.portalapp.portal.service.SharedContextService;
38 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
39 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
40 import org.openecomp.portalapp.service.EPProfileService;
41 import org.openecomp.portalapp.util.EPUserUtils;
42 import org.openecomp.portalapp.util.SessionCookieUtil;
43 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
44 import org.openecomp.portalsdk.core.menu.MenuProperties;
45 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalTimeoutHandler;
46 import org.openecomp.portalsdk.core.util.CipherUtil;
47 import org.openecomp.portalsdk.core.util.SystemProperties;
48 import org.openecomp.portalsdk.core.web.support.AppUtils;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.util.StopWatch;
53 import org.springframework.web.bind.annotation.RequestMapping;
54 import org.springframework.web.bind.annotation.RequestMethod;
55 import org.springframework.web.bind.annotation.ResponseBody;
56 import org.springframework.web.servlet.ModelAndView;
57 import org.springframework.web.util.WebUtils;
59 import com.fasterxml.jackson.databind.DeserializationFeature;
60 import com.fasterxml.jackson.databind.JsonNode;
61 import com.fasterxml.jackson.databind.ObjectMapper;
65 public class LoginController extends EPUnRestrictedBaseController implements LoginService{
67 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoginController.class);
69 public static final String DEFAULT_SUCCESS_VIEW = "applicationsHome";
70 public static final String DEFAULT_FAILURE_VIEW = "login";
71 public static final String ERROR_MESSAGE_KEY = "error";
72 public static final String REDIRECT_URL = "redirectUrl";
75 EPProfileService service;
77 private EPLoginService loginService;
79 private SharedContextService sharedContextService;
81 String viewName = "login";
82 private String welcomeView;
84 public String getWelcomeView() {
88 public void setWelcomeView(String welcomeView) {
89 this.welcomeView = welcomeView;
92 @RequestMapping(value = {"/login.htm" }, method = RequestMethod.GET)
93 public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
94 Map<String, Object> model = new HashMap<String, Object>();
96 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
100 if (authentication == null || authentication.equals("") || authentication.trim().equals("OIDC"))
101 loginPage = "openIdLogin";
103 loginPage = getViewName();
105 return new ModelAndView(loginPage,"model", model);
108 @SuppressWarnings("rawtypes")
109 @RequestMapping(value = {"/open_source/login" }, method = RequestMethod.POST)
110 public @ResponseBody String loginValidate(HttpServletRequest request, HttpServletResponse response) throws Exception{
112 ObjectMapper mapper = new ObjectMapper();
113 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
114 JsonNode root = mapper.readTree(request.getReader());
116 EPLoginBean commandBean = new EPLoginBean();
117 String loginId = root.get("loginId").textValue();
118 String password = root.get("password").textValue();
119 commandBean.setLoginId(loginId);
120 commandBean.setLoginPwd(CipherUtil.encrypt(password));
121 HashMap additionalParamsMap = new HashMap();
122 StringBuilder sbAdditionalInfo = new StringBuilder();
124 commandBean = getLoginService().findUser(commandBean, (String)request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY),
125 additionalParamsMap);
126 String fullURL = EPUserUtils.getFullURL(request);
127 if (commandBean.getUser() == null) {
128 String loginErrorMessage = (commandBean.getLoginErrorMessage() != null) ? commandBean.getLoginErrorMessage()
129 : "login.error.external.invalid";
131 logger.info(EELFLoggerDelegate.debugLogger, "loginId = " + loginId + " does not exist in the the DB.");
132 logger.info(EELFLoggerDelegate.errorLogger, "loginId = " + loginId + " does not exist in the the DB.");
133 sbAdditionalInfo.append(String.format("But the Login-Id: %s doesn't exist in the Database. Request-URL: %s",
135 return loginErrorMessage;
138 // store the currently logged in user's information in the session
139 EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(),
143 logger.info(EELFLoggerDelegate.debugLogger, "******************* store user info into share context begins");
144 String sessionId = request.getSession().getId();
145 List<SharedContext> existingSC = getSharedContextService().getSharedContexts(sessionId);
146 if(existingSC==null || existingSC.size()==0){
147 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_FIRST_NAME, commandBean.getUser().getFirstName());
148 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_LAST_NAME, commandBean.getUser().getLastName());
149 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_EMAIL, commandBean.getUser().getEmail());
150 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_ORG_USERID, commandBean.getLoginId());
154 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
155 logger.info(EELFLoggerDelegate.errorLogger, "failed the shared context adding process " + e.getMessage());
156 logger.info(EELFLoggerDelegate.debugLogger, "********************** failed the shared context adding process " + e.getMessage());
158 logger.info(EELFLoggerDelegate.debugLogger, "********************* PresetUp the EP service cookie and intial sessionManagement");
160 SessionCookieUtil.preSetUp(request, response);
161 SessionCookieUtil.setUpUserIdCookie(request, response, loginId);
163 JSONObject j = new JSONObject("{success: success}");
171 @RequestMapping(value = {"/processSingleSignOn" }, method = RequestMethod.GET)
172 public ModelAndView processSingelSignOn(HttpServletRequest request, HttpServletResponse response) throws Exception{
174 Map<Object, Object> model = new HashMap<Object, Object>();
175 HashMap<Object, Object> additionalParamsMap = new HashMap<Object, Object>();
176 EPLoginBean commandBean = new EPLoginBean();
177 MDC.put(MDC_KEY_REQUEST_ID, EPUserUtils.getRequestId(request));
178 String orgUserId = "";
179 //get userId from cookie
180 orgUserId = SessionCookieUtil.getUserIdFromCookie(request, response);
181 logger.info(EELFLoggerDelegate.debugLogger, "******************** process_singelSignOn process begins");
182 logger.info(EELFLoggerDelegate.debugLogger, "******************* We get the orgUserId " + orgUserId);
184 StringBuilder sbAdditionalInfo = new StringBuilder();
185 if ((orgUserId == null || orgUserId.length() == 0)) {
186 model.put(ERROR_MESSAGE_KEY, SystemProperties.MESSAGE_KEY_LOGIN_ERROR_COOKIE_EMPTY);
187 if(request.getParameter("redirectUrl")!=null && request.getParameter("redirectUrl").length()!=0){
188 return new ModelAndView("redirect:" + DEFAULT_FAILURE_VIEW + ".htm" + "?redirectUrl=" + request.getParameter("redirectUrl"));
190 return new ModelAndView("redirect:" + DEFAULT_FAILURE_VIEW + ".htm");
195 StopWatch stopWatch = new StopWatch("LoginController.Login");
199 logger.info(EELFLoggerDelegate.metricsLogger, "Operation findUser is started to locate " + orgUserId + " in the database.");
200 logger.info(EELFLoggerDelegate.debugLogger, "Operation findUser is started to locate " + orgUserId + " in the database.");
201 commandBean.setLoginId(orgUserId);
202 commandBean.setOrgUserId(orgUserId);
203 commandBean = getLoginService().findUserWithoutPassword(commandBean, (String)request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), additionalParamsMap);
206 MDC.put(EPSystemProperties.MDC_TIMER, stopWatch.getTotalTimeMillis() + "ms");
207 logger.info(EELFLoggerDelegate.metricsLogger, "Operation findUser is completed.");
208 logger.info(EELFLoggerDelegate.debugLogger, "Operation findUser is completed.");
209 } catch(Exception e) {
211 MDC.put(EPSystemProperties.MDC_TIMER, stopWatch.getTotalTimeMillis() + "ms");
212 logger.info(EELFLoggerDelegate.errorLogger, "Exception occurred while performing findUser " + orgUserId + ". Details: " + EcompPortalUtils.getStackTrace(e));
213 logger.info(EELFLoggerDelegate.debugLogger, "Exception occurred while performing findUser " + orgUserId + ". Details: " + EcompPortalUtils.getStackTrace(e));
214 logger.info(EELFLoggerDelegate.metricsLogger, "Operation findUser is failed.");
216 MDC.remove(EPSystemProperties.MDC_TIMER);
219 sbAdditionalInfo.append("Login attempt is succeeded. ");
220 String fullURL = EPUserUtils.getFullURL(request);
221 if (commandBean.getUser() == null) {
222 logger.info(EELFLoggerDelegate.debugLogger, "loginId = " + orgUserId + " does not exist in the the DB.");
223 logger.info(EELFLoggerDelegate.errorLogger, "loginId = " + orgUserId + " does not exist in the the DB.");
224 logger.info(EELFLoggerDelegate.debugLogger, "loginId = " + orgUserId + " does not exist in the the DB.");
226 sbAdditionalInfo.append(String.format("But the Login-Id: %s doesn't exist in the Database. Created a Guest Session. Request-URL: %s",
227 orgUserId, fullURL));
228 if(request.getParameter("redirectUrl")!=null && request.getParameter("redirectUrl").length()!=0){
229 return new ModelAndView("redirect:" + DEFAULT_FAILURE_VIEW + ".htm" + "?redirectUrl=" + request.getParameter("redirectUrl"));
231 return new ModelAndView("redirect:" + DEFAULT_FAILURE_VIEW + ".htm");
236 sbAdditionalInfo.append(String.format("Login-Id: %s, Login-Method: %s, Request-URL: %s", orgUserId, "", fullURL));
237 logger.info(EELFLoggerDelegate.debugLogger, "*********************** now set up user session for " + orgUserId);
239 EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), "");
240 logger.info(EELFLoggerDelegate.debugLogger, "*********************** now set up user session for " + orgUserId + " finished");
242 //Store user's information into share context
244 logger.info(EELFLoggerDelegate.debugLogger, "******************* store user info into share context begins");
246 String sessionId = request.getSession().getId();
247 List<SharedContext> existingSC = getSharedContextService().getSharedContexts(sessionId);
248 if(existingSC==null || existingSC.size()==0){
249 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_FIRST_NAME, commandBean.getUser().getFirstName());
250 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_LAST_NAME, commandBean.getUser().getLastName());
251 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_EMAIL, commandBean.getUser().getEmail());
252 getSharedContextService().addSharedContext(sessionId, EPSystemProperties.USER_ORG_USERID, commandBean.getLoginId());
256 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
257 logger.info(EELFLoggerDelegate.errorLogger, "failed the shared context adding process " + e.getMessage());
258 logger.info(EELFLoggerDelegate.debugLogger, "********************** failed the shared context adding process " + e.getMessage());
262 logger.info(EELFLoggerDelegate.debugLogger, "********************* PresetUp the EP service cookie and intial sessionManagement");
264 SessionCookieUtil.preSetUp(request, response);
265 SessionCookieUtil.setUpUserIdCookie(request, response, orgUserId);
266 logger.info(EELFLoggerDelegate.debugLogger, "********************* PresetUp the EP service cookie and intial sessionManagement completed");
267 logger.info(EELFLoggerDelegate.errorLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
268 logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
270 String redirect = "redirectUrl";
272 //get redirectUrl from URL parameter
273 if(request.getParameter(redirect)!=null && request.getParameter(redirect).length()!=0){
274 String forwardUrl = URLDecoder.decode(request.getParameter(redirect),"UTF-8");
276 Cookie cookie2 = new Cookie(redirect, "");
277 cookie2.setMaxAge(0);
278 cookie2.setDomain(EPSystemProperties.getProperty(EPSystemProperties.COOKIE_DOMAIN));
279 cookie2.setPath("/");
280 response.addCookie(cookie2);
281 return new ModelAndView("redirect:" + forwardUrl);
284 //first check if redirectUrl exists or not
285 if(WebUtils.getCookie(request, redirect)!=null){
286 String forwardUrl = WebUtils.getCookie(request, redirect).getValue();
288 Cookie cookie2 = new Cookie(redirect, "");
289 cookie2.setMaxAge(0);
290 cookie2.setDomain(EPSystemProperties.getProperty(EPSystemProperties.COOKIE_DOMAIN));
291 cookie2.setPath("/");
292 response.addCookie(cookie2);
294 return new ModelAndView("redirect:" + forwardUrl);
299 // if user has been authenticated, now take them to the welcome page.
300 //return new ModelAndView("redirect:" + DEFAULT_SUCCESS_VIEW + ".htm");
301 logger.info(EELFLoggerDelegate.debugLogger, "********************** Now return to application home page");
303 return new ModelAndView("redirect:" + SystemProperties.getProperty(EPSystemProperties.FE_URL));
306 // Re-enable for BE/FE separation. For 1607, at last minute we decided to go out
307 // without BE/FE separation.
309 //return new ModelAndView("redirect:" + SystemProperties.getProperty(EPSystemProperties.FE_URL));
313 public String getJessionId(HttpServletRequest request){
315 return request.getSession().getId();
317 Cookie ep = WebUtils.getCookie(request, JSESSIONID);
319 return request.getSession().getId();
321 return ep.getValue();
326 protected void initateSessionMgtHandler(HttpServletRequest request) {
327 String jSessionId = getJessionId(request);
328 PortalTimeoutHandler.sessionCreated(jSessionId, jSessionId, AppUtils.getSession(request));
332 public String getViewName() {
335 public void setViewName(String viewName) {
336 this.viewName = viewName;
338 public EPLoginService getLoginService() {
342 public void setLoginService(EPLoginService loginService) {
343 this.loginService = loginService;
346 public SharedContextService getSharedContextService() {
347 return sharedContextService;
350 public void setSharedContextService(SharedContextService sharedContextService) {
351 this.sharedContextService = sharedContextService;