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