EPUserUtils class fix
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / authentication / OpenIdConnectLoginStrategy.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 java.util.HashSet;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.mitre.openid.connect.model.UserInfo;
46 import org.onap.portalapp.command.EPLoginBean;
47 import org.onap.portalapp.portal.domain.EPUser;
48 import org.onap.portalapp.portal.utils.EPSystemProperties;
49 import org.onap.portalapp.util.EPUserUtils;
50 import org.onap.portalapp.util.SessionCookieUtil;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
53 import org.onap.portalsdk.core.util.SystemProperties;
54 import org.springframework.util.StringUtils;
55 import org.springframework.web.servlet.ModelAndView;
56
57 public class OpenIdConnectLoginStrategy extends org.onap.portalsdk.core.auth.LoginStrategy implements org.onap.portalapp.authentication.LoginStrategy {
58         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OpenIdConnectLoginStrategy.class);
59         private static final String GLOBAL_LOCATION_KEY = "Location";
60
61         public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
62                 
63                 logger.info("Attempting Login");                                                                                                
64                 
65                 //check both authentication cookie and authentication header
66                 UserInfo  userInfo = (UserInfo) request.getAttribute("userInfo");
67                                 
68                 if (userInfo != null && !StringUtils.isEmpty(userInfo.getPreferredUsername())) {                                                                                                
69                         //package the userid in the login form for processing
70                         EPLoginBean commandBean = new EPLoginBean();
71                         commandBean.setOrgUserId(userInfo.getPreferredUsername());
72
73                         EPUser user = new EPUser();
74                                 
75                         user.setOrgUserId(userInfo.getPreferredUsername());
76                         user.setEmail(userInfo.getEmail());
77                         user.setFirstName(userInfo.getName());
78                         user.setLastName(userInfo.getFamilyName());
79                         
80                         //store the currently logged in user's information in the session
81                         EPUserUtils.setUserSession(request, user,  new HashSet(), new HashSet(),null);
82
83                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
84                         SessionCookieUtil.preSetUp(request, response);  
85                         return true;
86                 } else {
87                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
88                         try {
89                                 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
90                                 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
91                                 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
92                                 
93                                 if (authentication == null || "".equals(authentication) || "OIDC".equals(authentication.trim())) {
94                                     response.sendRedirect("oid-login");
95                                 } else {
96                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
97                                         response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
98                                 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl);
99                             }
100                         } catch(Exception e) {
101                                 logger.error(EELFLoggerDelegate.errorLogger, "login failed", e);
102                         }
103                 }
104                 return false;
105         }
106
107         @Override
108         public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws PortalAPIException {
109                 String message = "Method not implmented; Cannot be called";
110                 logger.error(EELFLoggerDelegate.errorLogger, message);
111                 throw new PortalAPIException(message);
112         }
113
114         @Override
115         public String getUserId(HttpServletRequest request) throws PortalAPIException {
116                 String message = "Method not implmented; Cannot be called";
117                 logger.error(EELFLoggerDelegate.errorLogger, message);
118                 throw new PortalAPIException(message);
119         }
120 }