nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / authentication / SimpleLoginStrategy.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.authentication;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.openecomp.portalapp.command.EPLoginBean;
26 import org.openecomp.portalapp.portal.service.EPLoginService;
27 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
28 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
29 import org.openecomp.portalapp.util.EPUserUtils;
30 import org.openecomp.portalapp.util.SessionCookieUtil;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.portalsdk.core.menu.MenuProperties;
33 import org.openecomp.portalsdk.core.onboarding.crossapi.ECOMPSSO;
34 import org.openecomp.portalsdk.core.util.SystemProperties;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.util.StringUtils;
37
38 public class SimpleLoginStrategy implements LoginStrategy{
39         
40         @Autowired
41         private EPLoginService loginService;
42
43         private static final String GLOBAL_LOCATION_KEY = "Location";
44         
45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SimpleLoginStrategy.class);
46         
47         public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
48                 logger.info("Attempting 'Simple' Login");                                                                                               
49                 
50                 //check both authentication cookie and authentication header
51                 String  orgUserId = null;
52                 try{
53                          orgUserId = ECOMPSSO.getUserIdFromCookie(request);
54                 } catch(Exception ex){
55                         logger.error(EELFLoggerDelegate.errorLogger, "Error getting User ID: '" + ex.getLocalizedMessage() + "'.");
56                 }
57                 
58                 
59                 if (!StringUtils.isEmpty(orgUserId)) {                                                                                                  
60                         // package the userid in the login form for processing
61                         EPLoginBean commandBean = new EPLoginBean();
62                         commandBean.setOrgUserId(orgUserId);
63                         commandBean = loginService.findUser(commandBean, (String)request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), null);
64
65                          // in case authentication has passed but user is not in the ECOMP data base, return a Guest User to the home page.
66                         if (commandBean.getUser() == null) {
67                         }
68                         else {
69                                 // store the currently logged in user's information in the session
70                                 EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), "");
71                                 logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
72                         }
73                         
74                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
75                         SessionCookieUtil.preSetUp(request, response);
76                         return true;
77                 } else {
78                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
79                         try {
80                                 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
81                                 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
82                                 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
83                                 if (authentication == null || authentication.equals("") || authentication.trim().equals("BOTH")) {
84                                 
85                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
86                                     response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
87                             response.setHeader(GLOBAL_LOCATION_KEY, loginUrl); //returnUrl + "/index.htm");
88                         }else {
89                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
90                                         response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
91                                 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl); //returnUrl + "/index.htm");
92                             }
93                         } catch(Exception e) {
94                                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in preHandle() while redirecting, Details: " + EcompPortalUtils.getStackTrace(e));
95                         }
96                 }
97
98                 return false;
99
100         }
101 }