[AAI-244] grep scan
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / security / EcompSso.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.security;
24
25 import javax.servlet.http.Cookie;
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.openecomp.cl.api.Logger;
29 import org.openecomp.cl.eelf.LoggerFactory;
30 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
31 import org.openecomp.sparky.logging.AaiUiMsgs;
32 import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig;
33 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
34
35
36 /**
37  * Provides authentication services for onboarded ECOMP applications.
38  */
39 public class EcompSso {
40
41   public static final String EP_SERVICE = "EPService";
42   public static final String CSP_COOKIE_NAME = "csp_cookie_name";
43   public static final String CSP_GATE_KEEPER_PROD_KEY = "csp_gate_keeper_prod_key";
44   public static final String ONAP_ENABLED = "ONAP_ENABLED"; 
45   private static final Logger LOG = LoggerFactory.getInstance().getLogger(EcompSso.class);
46
47   /**
48    * Searches the request for a cookie with the specified name.
49    *
50    * @param request
51    * @param cookieName
52    * @return Cookie, or null if not found.
53    */
54   public static Cookie getCookie(HttpServletRequest request, String cookieName) {
55     Cookie[] cookies = request.getCookies();
56     if (cookies != null)
57       for (Cookie cookie : cookies) {
58         if (cookie.getName().equals(cookieName)) {
59           return cookie;
60         }
61       }
62
63     return null;
64   }
65
66   /**
67    * Answers whether the ECOMP Portal service cookie is present in the specified request.
68    *
69    * @param request
70    * @return true if the cookie is found, else false.
71    */
72   private static boolean isEPServiceCookiePresent(HttpServletRequest request) {
73     Cookie ep = getCookie(request, EP_SERVICE);
74     return (ep != null);
75   }
76
77   /**
78    * Validates whether the ECOMP Portal sign-on process has completed, which relies the AT&T Global
79    * Log On single-sign on process. Checks for the ECOMP cookie (see {@link #EP_SERVICE}). If found,
80    * then searches for a CSP cookie; if not found, for a WebJunction header.
81    *
82    * @param request
83    * @return User ID if the ECOMP cookie is present and the sign-on process established an User ID;
84    *         else null.
85    */
86   public static String validateEcompSso(HttpServletRequest request) {
87     boolean isOnapEnabled = PortalAuthenticationConfig.getInstance().getIsOnapEnabled();
88     if (isOnapEnabled) {
89       if (isEPServiceCookiePresent(request)) {
90         /* This is a "temporary" fix until proper separation
91          * between closed source and open source code is reached */
92         return ONAP_ENABLED;
93       } 
94       return null;
95     } else {
96       return getLoginIdFromCookie(request);
97     }
98   }
99
100   /**
101    * Searches the specified request for the CSP cookie, decodes it and gets the User ID.
102    *
103    * @param request
104    * @return User ID if the cookie is present in the request and can be decoded successfully (expired
105    *         cookies do not decode); else null.
106    */
107   private static String getLoginIdFromCookie(HttpServletRequest request) {
108     String userid = null;
109     try {
110       String[] cspFields = getCspData(request);
111       if (cspFields != null && cspFields.length > 5)
112         userid = cspFields[5];
113     } catch (Throwable t) {
114       LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO,
115           "getLoginIdFromCookie failed " + t.getLocalizedMessage());
116     }
117     return userid;
118   }
119
120   /**
121    * Searches the specified request for the CSP cookie, decodes it and parses it to a String array.
122    *
123    * @param request
124    * @return Array of String as parsed from the cookie; null if the cookie is not present; empty
125    *         array if the cookie could not be decoded.
126    */
127   private static String[] getCspData(HttpServletRequest request) {
128     final String cookieName = PortalApiProperties.getProperty(CSP_COOKIE_NAME);
129     if (cookieName == null) {
130       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
131           "getCspData: Failed to get property " + CSP_COOKIE_NAME);
132       return null;
133     }
134     Cookie csp = getCookie(request, cookieName);
135     if (csp == null) {
136       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to get cookie " + cookieName);
137       return null;
138     }
139     final String cspCookieEncrypted = csp.getValue();
140
141     String gateKeeperProdKey = PortalApiProperties.getProperty(CSP_GATE_KEEPER_PROD_KEY);
142     if (gateKeeperProdKey == null) {
143       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
144           "getCspData: failed to get property " + CSP_GATE_KEEPER_PROD_KEY);
145     }
146
147     String cspCookieDecrypted = "";
148     try {
149       cspCookieDecrypted = CipherUtil.decrypt(cspCookieEncrypted,"");
150     } catch (Exception e) {
151       LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO,
152           "decrypting cookie failed " + e.getLocalizedMessage());
153     }
154
155     String[] cspData = cspCookieDecrypted.split("\\|");
156     return cspData;
157   }
158 }