2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.authentication;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
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;
57 public class SimpleLoginStrategy extends org.onap.portalsdk.core.auth.LoginStrategy implements LoginStrategy{
60 private EPLoginService loginService;
63 private EPRoleFunctionService ePRoleFunctionService;
65 private static final String GLOBAL_LOCATION_KEY = "Location";
67 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SimpleLoginStrategy.class);
69 public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
70 logger.info("Attempting 'Simple' Login");
72 //check both authentication cookie and authentication header
73 String orgUserId = SessionCookieUtil.getUserIdFromCookie(request, response);
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);
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) {
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.");
90 logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
91 SessionCookieUtil.preSetUp(request, response);
94 // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
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")) {
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");
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");
109 } catch(Exception e) {
110 logger.error(EELFLoggerDelegate.errorLogger, "login failed", e);
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);
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);