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.authentication;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
25 import org.openecomp.portalapp.command.EPLoginBean;
26 import org.openecomp.portalapp.portal.service.EPLoginService;
27 import org.openecomp.portalapp.portal.service.EPRoleService;
28 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
29 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
30 import org.openecomp.portalapp.util.EPUserUtils;
31 import org.openecomp.portalapp.util.SessionCookieUtil;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.menu.MenuProperties;
34 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
35 import org.openecomp.portalsdk.core.onboarding.util.*;
36 import org.openecomp.portalsdk.core.util.SystemProperties;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.util.StringUtils;
39 import org.springframework.web.servlet.ModelAndView;
41 public class SimpleLoginStrategy extends org.openecomp.portalsdk.core.auth.LoginStrategy implements LoginStrategy{
44 private EPLoginService loginService;
46 private EPRoleService roleService;
48 private static final String GLOBAL_LOCATION_KEY = "Location";
50 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SimpleLoginStrategy.class);
52 public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
53 logger.info("Attempting 'Simple' Login");
55 //check both authentication cookie and authentication header
56 String orgUserId = SessionCookieUtil.getUserIdFromCookie(request, response);
58 if (!StringUtils.isEmpty(orgUserId)) {
59 // package the userid in the login form for processing
60 EPLoginBean commandBean = new EPLoginBean();
61 commandBean.setOrgUserId(orgUserId);
62 commandBean = loginService.findUser(commandBean, (String)request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), null);
64 // in case authentication has passed but user is not in the ECOMP data base, return a Guest User to the home page.
65 if (commandBean.getUser() == null) {
68 // store the currently logged in user's information in the session
69 EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), "", roleService.getRoleFunctions());
70 logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
73 logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
74 SessionCookieUtil.preSetUp(request, response);
77 // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
79 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
80 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
81 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
82 if (authentication == null || authentication.equals("") || authentication.trim().equals("BOTH")) {
84 logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
85 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
86 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl); //returnUrl + "/index.htm");
88 logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
89 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
90 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl); //returnUrl + "/index.htm");
92 } catch(Exception e) {
93 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in preHandle() while redirecting, Details: " + EcompPortalUtils.getStackTrace(e));
102 public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
103 String message = "Method not implmented; Cannot be called";
104 logger.error(EELFLoggerDelegate.errorLogger, message);
105 throw new Exception(message);
109 public String getUserId(HttpServletRequest request) throws PortalAPIException {
110 String message = "Method not implmented; Cannot be called";
111 logger.error(EELFLoggerDelegate.errorLogger, message);
112 throw new PortalAPIException(message);