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