2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.authentication;
22 import java.util.HashSet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.mitre.openid.connect.model.UserInfo;
28 import org.openecomp.portalapp.command.EPLoginBean;
29 import org.openecomp.portalapp.portal.domain.EPUser;
30 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
31 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
32 import org.openecomp.portalapp.util.EPUserUtils;
33 import org.openecomp.portalapp.util.SessionCookieUtil;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalsdk.core.util.SystemProperties;
36 import org.springframework.util.StringUtils;
38 public class OpenIdConnectLoginStrategy implements LoginStrategy {
40 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OpenIdConnectLoginStrategy.class);
42 private static final String GLOBAL_LOCATION_KEY = "Location";
44 @SuppressWarnings("rawtypes")
45 public boolean login(HttpServletRequest request, HttpServletResponse response){
47 logger.info("Attempting Login");
49 //check both authentication cookie and authentication header
50 UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
52 if (userInfo != null && !StringUtils.isEmpty(userInfo.getPreferredUsername())) {
53 //package the userid in the login form for processing
54 EPLoginBean commandBean = new EPLoginBean();
55 commandBean.setOrgUserId(userInfo.getPreferredUsername());
57 EPUser user = new EPUser();
59 user.setOrgUserId(userInfo.getPreferredUsername());
60 user.setEmail(userInfo.getEmail());
61 user.setFirstName(userInfo.getName());
62 user.setLastName(userInfo.getFamilyName());
64 //store the currently logged in user's information in the session
65 EPUserUtils.setUserSession(request, user, new HashSet(), new HashSet(), SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM));
67 logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
68 SessionCookieUtil.preSetUp(request, response);
71 // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
74 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
75 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
76 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
78 if (authentication == null || authentication.equals("") || authentication.trim().equals("OIDC")) {
79 response.sendRedirect("oid-login");
81 logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
82 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
83 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl);
85 } catch(Exception e) {
86 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in preHandle() while redirecting, Details: " + EcompPortalUtils.getStackTrace(e));