2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.security;
25 import javax.servlet.http.Cookie;
26 import javax.servlet.http.HttpServletRequest;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
32 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
33 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
36 * Provides authentication services for onboarded ECOMP applications.
38 public class EcompSso {
40 public static final String EP_SERVICE = "EPService";
41 public static final String CSP_COOKIE_NAME = "csp_cookie_name";
42 public static final String CSP_GATE_KEEPER_PROD_KEY = "csp_gate_keeper_prod_key";
43 public static final String ONAP_ENABLED = "ONAP_ENABLED";
44 private static final Logger LOG = LoggerFactory.getInstance().getLogger(EcompSso.class);
47 * Searches the request for a cookie with the specified name.
51 * @return Cookie, or null if not found.
53 public static Cookie getCookie(HttpServletRequest request, String cookieName) {
54 Cookie[] cookies = request.getCookies();
56 for (Cookie cookie : cookies) {
57 if (cookie.getName().equals(cookieName)) {
66 * Answers whether the ECOMP Portal service cookie is present in the specified request.
69 * @return true if the cookie is found, else false.
71 private static boolean isEPServiceCookiePresent(HttpServletRequest request) {
72 Cookie ep = getCookie(request, EP_SERVICE);
77 * Validates whether the ECOMP Portal sign-on process has completed, which relies the AT&T Global
78 * Log On single-sign on process. Checks for the ECOMP cookie (see {@link #EP_SERVICE}). If found,
79 * then searches for a CSP cookie; if not found, for a WebJunction header.
82 * @return ATT UID if the ECOMP cookie is present and the sign-on process established an ATT UID;
85 public static String validateEcompSso(HttpServletRequest request) {
86 boolean isOnapEnabled = PortalAuthenticationConfig.getInstance().getIsOnapEnabled();
88 if (isEPServiceCookiePresent(request)) {
90 * This is a "temporary" fix until proper separation between closed source and open source
97 return getLoginIdFromCookie(request);
102 * Searches the specified request for the CSP cookie, decodes it and gets the ATT UID.
105 * @return ATTUID if the cookie is present in the request and can be decoded successfully (expired
106 * cookies do not decode); else null.
108 private static String getLoginIdFromCookie(HttpServletRequest request) {
109 String attuid = null;
111 String[] cspFields = getCspData(request);
112 if (cspFields != null && cspFields.length > 5)
113 attuid = cspFields[5];
114 } catch (Throwable t) {
115 LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO,
116 "getLoginIdFromCookie failed " + t.getLocalizedMessage());
122 * Searches the specified request for the CSP cookie, decodes it and parses it to a String array.
125 * @return Array of String as parsed from the cookie; null if the cookie is not present; empty
126 * array if the cookie could not be decoded.
128 private static String[] getCspData(HttpServletRequest request) {
129 final String cookieName = PortalApiProperties.getProperty(CSP_COOKIE_NAME);
130 if (cookieName == null) {
131 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
132 "getCspData: Failed to get property " + CSP_COOKIE_NAME);
135 Cookie csp = getCookie(request, cookieName);
137 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to get cookie " + cookieName);
140 final String cspCookieEncrypted = csp.getValue();
142 String gateKeeperProdKey = PortalApiProperties.getProperty(CSP_GATE_KEEPER_PROD_KEY);
143 if (gateKeeperProdKey == null) {
144 LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG,
145 "getCspData: failed to get property " + CSP_GATE_KEEPER_PROD_KEY);
148 String cspCookieDecrypted = "";
150 cspCookieDecrypted = CipherUtil.decrypt(cspCookieEncrypted, "");
151 } catch (Exception e) {
152 LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO, "decrypting cookie failed " + e.getLocalizedMessage());
155 String[] cspData = cspCookieDecrypted.split("\\|");