nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / authentication / OpenIdConnectLoginStrategy.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 java.util.HashSet;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
37
38 public class OpenIdConnectLoginStrategy implements LoginStrategy {
39         
40         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OpenIdConnectLoginStrategy.class);
41         
42         private static final String GLOBAL_LOCATION_KEY = "Location";
43
44         @SuppressWarnings("rawtypes")
45         public boolean login(HttpServletRequest request, HttpServletResponse response){
46                 
47                 logger.info("Attempting Login");                                                                                                
48                 
49                 //check both authentication cookie and authentication header
50                 UserInfo  userInfo = (UserInfo) request.getAttribute("userInfo");
51                                 
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());
56
57                         EPUser user = new EPUser();
58                                 
59                         user.setOrgUserId(userInfo.getPreferredUsername());
60                         user.setEmail(userInfo.getEmail());
61                         user.setFirstName(userInfo.getName());
62                         user.setLastName(userInfo.getFamilyName());
63                         
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));
66
67                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
68                         SessionCookieUtil.preSetUp(request, response);  
69                         return true;
70                 } else {
71                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
72                         
73                         try {
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 + "'.");
77                                 
78                                 if (authentication == null || authentication.equals("") || authentication.trim().equals("OIDC")) {                              
79                                     response.sendRedirect("oid-login");
80                                 } else {
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);
84                             }
85                         } catch(Exception e) {
86                                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in preHandle() while redirecting, Details: " + EcompPortalUtils.getStackTrace(e));
87                         }
88                 }
89                 return false;
90         }
91 }