[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / 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.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;
40
41 public class SimpleLoginStrategy extends org.openecomp.portalsdk.core.auth.LoginStrategy implements LoginStrategy{
42         
43         @Autowired
44         private EPLoginService loginService;
45         @Autowired
46         private EPRoleService roleService;
47
48         private static final String GLOBAL_LOCATION_KEY = "Location";
49         
50         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SimpleLoginStrategy.class);
51         
52         public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
53                 logger.info("Attempting 'Simple' Login");                                                                                               
54                 
55                 //check both authentication cookie and authentication header
56                 String  orgUserId = SessionCookieUtil.getUserIdFromCookie(request, response);
57                 
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);
63
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) {
66                         }
67                         else {
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.");
71                         }
72                         
73                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
74                         SessionCookieUtil.preSetUp(request, response);
75                         return true;
76                 } else {
77                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
78                         try {
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")) {
83                                 
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");
87                         }else {
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");
91                             }
92                         } catch(Exception e) {
93                                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in preHandle() while redirecting, Details: " + EcompPortalUtils.getStackTrace(e));
94                         }
95                 }
96
97                 return false;
98
99         }
100
101         @Override
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);
106         }
107
108         @Override
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);
113         }
114 }