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