Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / EcompSso.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.security;
26
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.sparky.logging.AaiUiMsgs;
33 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
34 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
35 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
36
37 /**
38  * Provides authentication services for onboarded ECOMP applications.
39  */
40 public class EcompSso {
41
42   public static final String EP_SERVICE = "EPService";
43   public static final String CSP_COOKIE_NAME = "csp_cookie_name";
44   public static final String CSP_GATE_KEEPER_PROD_KEY = "csp_gate_keeper_prod_key";
45   public static final String ONAP_ENABLED = "ONAP_ENABLED";
46   private static final Logger LOG = LoggerFactory.getInstance().getLogger(EcompSso.class);
47
48   /**
49    * Searches the request for a cookie with the specified name.
50    *
51    * @param request
52    * @param cookieName
53    * @return Cookie, or null if not found.
54    */
55   public static Cookie getCookie(HttpServletRequest request, String cookieName) {
56     Cookie[] cookies = request.getCookies();
57     if (cookies != null)
58       for (Cookie cookie : cookies) {
59         if (cookie.getName().equals(cookieName)) {
60           return cookie;
61         }
62       }
63
64     return null;
65   }
66
67   /**
68    * Answers whether the ECOMP Portal service cookie is present in the specified request.
69    *
70    * @param request
71    * @return true if the cookie is found, else false.
72    */
73   private static boolean isEPServiceCookiePresent(HttpServletRequest request) {
74     Cookie ep = getCookie(request, EP_SERVICE);
75     return (ep != null);
76   }
77
78   /**
79    * Validates whether the ECOMP Portal sign-on process has completed, which relies the AT&T Global
80    * Log On single-sign on process. Checks for the ECOMP cookie (see {@link #EP_SERVICE}). If found,
81    * then searches for a CSP cookie; if not found, for a WebJunction header.
82    *
83    * @param request
84    * @return ATT UID if the ECOMP cookie is present and the sign-on process established an ATT UID;
85    *         else null.
86    */
87   public static String validateEcompSso(HttpServletRequest request) {
88     boolean isOnapEnabled = PortalAuthenticationConfig.getInstance().getIsOnapEnabled();
89     if (isOnapEnabled) {
90       if (isEPServiceCookiePresent(request)) {
91         /*
92          * This is a "temporary" fix until proper separation between closed source and open source
93          * code is reached
94          */
95         return ONAP_ENABLED;
96       }
97       return null;
98     } else {
99       return getLoginIdFromCookie(request);
100     }
101   }
102
103   /**
104    * Searches the specified request for the CSP cookie, decodes it and gets the ATT UID.
105    *
106    * @param request
107    * @return ATTUID if the cookie is present in the request and can be decoded successfully (expired
108    *         cookies do not decode); else null.
109    */
110   private static String getLoginIdFromCookie(HttpServletRequest request) {
111     String uid = null;
112     try {
113       String[] cspFields = getCspData(request);
114       if (cspFields != null && cspFields.length > 5)
115         uid = cspFields[5];
116     } catch (Throwable t) {
117       LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO,
118           "getLoginIdFromCookie failed " + t.getLocalizedMessage());
119     }
120     return uid;
121   }
122
123   /**
124    * Searches the specified request for the CSP cookie, decodes it and parses it to a String array.
125    *
126    * @param request
127    * @return Array of String as parsed from the cookie; null if the cookie is not present; empty
128    *         array if the cookie could not be decoded.
129    */
130   private static String[] getCspData(HttpServletRequest request) {
131     final String cookieName = PortalApiProperties.getProperty(CSP_COOKIE_NAME);
132     if (cookieName == null) {
133       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
134           "getCspData: Failed to get property " + CSP_COOKIE_NAME);
135       return null;
136     }
137     Cookie csp = getCookie(request, cookieName);
138     if (csp == null) {
139       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to get cookie " + cookieName);
140       return null;
141     }
142     final String cspCookieEncrypted = csp.getValue();
143
144     String cspCookieDecrypted = null;
145         try {
146                 cspCookieDecrypted = PortalAuthenticationConfig.getInstance().getCookieDecryptor().decryptCookie(cspCookieEncrypted);
147                 return cspCookieDecrypted.split("\\|");
148                 
149         } catch (ClassNotFoundException e) {
150                 LOG.error(AaiUiMsgs.DECRYPTION_ERROR,"Unable to find the Cookie Decryptor Class");
151         }
152         
153     return null;
154   }
155 }