Added missing license headers
[policy/engine.git] / ONAP-SDK-APP / src / main / java / org / openecomp / portalapp / login / LoginStrategyImpl.java
1 /*-
2  * ================================================================================
3  * ONAP Portal SDK
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.login;
21
22 import javax.servlet.http.Cookie;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.portalsdk.core.auth.LoginStrategy;
29 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
30 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
31 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
32 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
33 import org.springframework.web.servlet.ModelAndView;
34
35 public class LoginStrategyImpl extends LoginStrategy {
36
37     private static final Logger LOGGER = FlexLogger.getLogger(LoginStrategyImpl.class);
38
39     @Override
40     public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
41         // 'login' for opensource is same as 'external' login.
42         return doExternalLogin(request, response);
43     }
44
45     @Override
46     public String getUserId(HttpServletRequest request) throws PortalAPIException {
47         // Check ONAP Portal cookie
48         if (!isLoginCookieExist(request))
49             return null;
50
51         String userid = null;
52         try {
53             userid = getUserIdFromCookie(request);
54         } catch (Exception e) {
55             LOGGER.error("Exception Occured" + e);
56         }
57         return userid;
58     }
59
60     private static String getUserIdFromCookie(HttpServletRequest request) throws PortalAPIException {
61         String userId = "";
62         Cookie[] cookies = request.getCookies();
63         Cookie userIdcookie = null;
64         if (cookies != null)
65             for (Cookie cookie : cookies)
66                 if (cookie.getName().equals(USER_ID))
67                     userIdcookie = cookie;
68         if (userIdcookie != null) {
69             try {
70                 userId = CipherUtil.decrypt(userIdcookie.getValue(),
71                         PortalApiProperties.getProperty(PortalApiConstants.Decryption_Key));
72             } catch (Exception e) {
73                 throw new PortalAPIException(e);
74             }
75         }
76         return userId;
77
78     }
79
80     private static boolean isLoginCookieExist(HttpServletRequest request) {
81         Cookie ep = getCookie(request, EP_SERVICE);
82         return (ep != null);
83     }
84
85     private static Cookie getCookie(HttpServletRequest request, String cookieName) {
86         Cookie[] cookies = request.getCookies();
87         if (cookies != null)
88             for (Cookie cookie : cookies)
89                 if (cookie.getName().equals(cookieName))
90                     return cookie;
91
92         return null;
93     }
94
95 }