Resolved the License issue in Policy
[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.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
31 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
32 import org.openecomp.portalsdk.core.util.SystemProperties;
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 userIdCookie = getCookie(request, USER_ID); 
63                 if (userIdCookie != null) { 
64                         final String cookieValue = userIdCookie.getValue(); 
65                         if (!SystemProperties.containsProperty(SystemProperties.Decryption_Key)) 
66                                 throw new IllegalStateException("Failed to find property " + SystemProperties.Decryption_Key); 
67                         final String decryptionKey = SystemProperties.getProperty(SystemProperties.Decryption_Key); 
68                         try {
69                                 userId = CipherUtil.decrypt(cookieValue, decryptionKey);
70                                 LOGGER.debug("getUserIdFromCookie: decrypted as {}" +userId); 
71                         } catch (Exception e) {
72                                 LOGGER.error("Exception Occured in getUserIdFromCookie" + e);
73                         } 
74                 } 
75                 return userId;
76     }
77
78     private static boolean isLoginCookieExist(HttpServletRequest request) {
79         Cookie ep = getCookie(request, EP_SERVICE);
80         return (ep != null);
81     }
82
83     private static Cookie getCookie(HttpServletRequest request, String cookieName) {
84         Cookie[] cookies = request.getCookies();
85         if (cookies != null)
86             for (Cookie cookie : cookies)
87                 if (cookie.getName().equals(cookieName))
88                     return cookie;
89
90         return null;
91     }
92
93 }