Security/ Package Name changes
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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         
59         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OpenIdConnectLoginStrategy.class);
60         
61         private static final String GLOBAL_LOCATION_KEY = "Location";
62
63         @SuppressWarnings("rawtypes")
64         public boolean login(HttpServletRequest request, HttpServletResponse response) throws Exception{
65                 
66                 logger.info("Attempting Login");                                                                                                
67                 
68                 //check both authentication cookie and authentication header
69                 UserInfo  userInfo = (UserInfo) request.getAttribute("userInfo");
70                                 
71                 if (userInfo != null && !StringUtils.isEmpty(userInfo.getPreferredUsername())) {                                                                                                
72                         //package the userid in the login form for processing
73                         EPLoginBean commandBean = new EPLoginBean();
74                         commandBean.setOrgUserId(userInfo.getPreferredUsername());
75
76                         EPUser user = new EPUser();
77                                 
78                         user.setOrgUserId(userInfo.getPreferredUsername());
79                         user.setEmail(userInfo.getEmail());
80                         user.setFirstName(userInfo.getName());
81                         user.setLastName(userInfo.getFamilyName());
82                         
83                         //store the currently logged in user's information in the session
84                         EPUserUtils.setUserSession(request, user,  new HashSet(), new HashSet(), SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM),null);
85
86                         logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
87                         SessionCookieUtil.preSetUp(request, response);  
88                         return true;
89                 } else {
90                         // in case authentication cookie is missing, send 401 UNAUTHORIZED to client and it will redirect to Logon
91                         try {
92                                 String authentication = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM);
93                                 String loginUrl = SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL);
94                                 logger.info(EELFLoggerDelegate.errorLogger, "Authentication Mechanism: '" + authentication + "'.");
95                                 
96                                 if (authentication == null || authentication.equals("") || authentication.trim().equals("OIDC")) {                              
97                                     response.sendRedirect("oid-login");
98                                 } else {
99                                         logger.info(EELFLoggerDelegate.errorLogger, "No cookies are found, redirecting the request to '" + loginUrl + "'.");
100                                         response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
101                                 response.setHeader(GLOBAL_LOCATION_KEY, loginUrl);
102                             }
103                         } catch(Exception e) {
104                                 logger.error(EELFLoggerDelegate.errorLogger, "login failed", e);
105                         }
106                 }
107                 return false;
108         }
109
110         @Override
111         public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
112                 String message = "Method not implmented; Cannot be called";
113                 logger.error(EELFLoggerDelegate.errorLogger, message);
114                 throw new Exception(message);
115         }
116
117         @Override
118         public String getUserId(HttpServletRequest request) throws PortalAPIException {
119                 String message = "Method not implmented; Cannot be called";
120                 logger.error(EELFLoggerDelegate.errorLogger, message);
121                 throw new PortalAPIException(message);
122         }
123 }