EPUserUtils class fix
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / authentication / SimpleLoginStrategy.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.authentication;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import lombok.NoArgsConstructor;
44 import org.onap.portalapp.command.EPLoginBean;
45 import org.onap.portalapp.portal.service.EPLoginService;
46 import org.onap.portalapp.portal.service.EPRoleFunctionService;
47 import org.onap.portalapp.portal.utils.EPSystemProperties;
48 import org.onap.portalapp.util.EPUserUtils;
49 import org.onap.portalapp.util.SessionCookieUtil;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.onap.portalsdk.core.menu.MenuProperties;
52 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
53 import org.onap.portalsdk.core.util.SystemProperties;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.util.StringUtils;
56 import org.springframework.web.servlet.ModelAndView;
57
58 @NoArgsConstructor
59 public class SimpleLoginStrategy extends org.onap.portalsdk.core.auth.LoginStrategy implements LoginStrategy{
60         private static final String GLOBAL_LOCATION_KEY = "Location";
61         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SimpleLoginStrategy.class);
62
63         private EPLoginService loginService;
64         private EPRoleFunctionService ePRoleFunctionService;
65
66         @Autowired
67         public SimpleLoginStrategy(EPLoginService loginService,
68                 EPRoleFunctionService ePRoleFunctionService) {
69                 this.loginService = loginService;
70                 this.ePRoleFunctionService = ePRoleFunctionService;
71         }
72
73         public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
74                 logger.info("Attempting 'Simple' Login");                                                                                               
75                 
76                 //check both authentication cookie and authentication header
77                 String  orgUserId = SessionCookieUtil.getUserIdFromCookie(request, response);
78                 
79                 if (!StringUtils.isEmpty(orgUserId)) {                                                                                                  
80                         // package the userid in the login form for processing
81                         EPLoginBean commandBean = new EPLoginBean();
82                         commandBean.setOrgUserId(orgUserId);
83                         commandBean = loginService.findUser(commandBean, (String)request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), null);
84
85                          // in case authentication has passed but user is not in the ONAP data base, return a Guest User to the home page.
86                         if (commandBean.getUser() != null) {
87                                 // store the currently logged in user's information in the session
88                                 EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), ePRoleFunctionService);
89                                 logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
90                         }
91                         
92                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
93                         SessionCookieUtil.preSetUp(request, response);
94                         return true;
95                 } else {
96                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
97                         try {
98                                 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
99                                 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
100                                 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
101                                 if (authentication == null || authentication.isEmpty() || "BOTH".equals(authentication.trim())) {
102                                 
103                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
104                                     response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
105                             response.setHeader(GLOBAL_LOCATION_KEY, loginUrl);
106                         }else {
107                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
108                                         response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
109                                 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl);
110                             }
111                         } catch(Exception e) {
112                                 logger.error(EELFLoggerDelegate.errorLogger, "login failed", e);
113                         }
114                 }
115
116                 return false;
117
118         }
119
120         @Override
121         public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws PortalAPIException {
122                 String message = "Method not implmented; Cannot be called";
123                 logger.error(EELFLoggerDelegate.errorLogger, message);
124                 throw new PortalAPIException(message);
125         }
126
127         @Override
128         public String getUserId(HttpServletRequest request) throws PortalAPIException {
129                 String message = "Method not implmented; Cannot be called";
130                 logger.error(EELFLoggerDelegate.errorLogger, message);
131                 throw new PortalAPIException(message);
132         }
133 }