fix backdoor issue when using portal
[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 java.util.ArrayList;
24 import java.util.List;
25
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.security.portal.PortalRestAPICentralServiceImpl;
33 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
34 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
35 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
36 import org.onap.portalsdk.core.restful.domain.EcompRole;
37
38 /**
39  * Provides authentication services for onboarded ECOMP applications.
40  */
41 public class EcompSso {
42
43   public static final String EP_SERVICE = "EPService";
44   public static final String CSP_COOKIE_NAME = "csp_cookie_name";
45   public static final String CSP_GATE_KEEPER_PROD_KEY = "csp_gate_keeper_prod_key";
46   public static final String ONAP_ENABLED = "ONAP_ENABLED";
47   private static  EcompSso eCompSso = new EcompSso();
48   private PortalRestAPICentralServiceImpl portalRestCentralImpl = new PortalRestAPICentralServiceImpl();
49   private static final Logger LOG = LoggerFactory.getInstance().getLogger(EcompSso.class);
50
51   /**
52    * Searches the request for a cookie with the specified name.
53    *
54    * @param request
55    * @param cookieName
56    * @return Cookie, or null if not found.
57    */
58   public static Cookie getCookie(HttpServletRequest request, String cookieName) {
59     Cookie[] cookies = request.getCookies();
60     if (cookies != null)
61       for (Cookie cookie : cookies) {
62         if (cookie.getName().equals(cookieName)) {
63           return cookie;
64         }
65       }
66
67     return null;
68   }
69
70   /**
71    * Validates whether the ECOMP Portal sign-on process has completed, which relies the AT&T Global
72    * Log On single-sign on process. Checks for the ECOMP cookie (see {@link #EP_SERVICE}). If found,
73    * then searches for a CSP cookie; if not found, for a WebJunction header.
74    *
75    * @param request
76    * @return ATT UID if the ECOMP cookie is present and the sign-on process established an ATT UID;
77    *         else null.
78    */
79   public static String validateEcompSso(HttpServletRequest request) {
80     String uid = null;
81     boolean isOnapEnabled = PortalAuthenticationConfig.getInstance().getIsOnapEnabled();
82     if (isOnapEnabled) {
83       final String cookieName = PortalAuthenticationConfig.getInstance().getUserIdCookieName();
84
85       if (cookieName == null) {
86         LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to load cookie");
87         return null;
88       }
89       Cookie csp = getCookie(request, cookieName);
90       if (csp == null) {
91         LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to get cookie " + cookieName);
92         return null;
93       }
94       final String cspCookieEncrypted = csp.getValue();
95
96       try {
97         uid = PortalAuthenticationConfig.getInstance().getCookieDecryptor()
98             .decryptCookie(cspCookieEncrypted);
99       } catch (ClassNotFoundException e) {
100         LOG.error(AaiUiMsgs.DECRYPTION_ERROR, "Unable to find the Cookie Decryptor Class");
101       }
102
103     } else {
104       try {
105         String[] cspFields = getCspData(request);
106         if (cspFields != null && cspFields.length > 5)
107           uid = cspFields[5];
108       } catch (Exception t) {
109         LOG.info(AaiUiMsgs.LOGIN_FILTER_INFO,
110             "getLoginIdFromCookie failed " + t.getLocalizedMessage());
111       }
112     }
113     boolean validated = eCompSso.validateUserAccess(uid);
114     if (!validated) {
115       LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "Unable to grant user access to application");
116       return null;
117     }
118     return uid;
119   }
120
121   /**
122    * Searches the specified request for the CSP cookie, decodes it and parses it to a String array.
123    *
124    * @param request
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.
127    */
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);
133       return null;
134     }
135     Cookie csp = getCookie(request, cookieName);
136     if (csp == null) {
137       LOG.debug(AaiUiMsgs.LOGIN_FILTER_DEBUG, "getCspData failed to get cookie " + cookieName);
138       return null;
139     }
140     final String cspCookieEncrypted = csp.getValue();
141
142     String cspCookieDecrypted = null;
143     try {
144       cspCookieDecrypted = PortalAuthenticationConfig.getInstance().getCookieDecryptor()
145           .decryptCookie(cspCookieEncrypted);
146       return cspCookieDecrypted.split("\\|");
147
148     } catch (ClassNotFoundException e) {
149       LOG.error(AaiUiMsgs.DECRYPTION_ERROR, "Unable to find the Cookie Decryptor Class");
150     }
151
152     return null;
153   }
154
155   public boolean validateUserAccess(String uid) {
156     boolean hasAccess = false;
157     ArrayList<String> appRoles = PortalAuthenticationConfig.getInstance().getAppRoles();
158     if (uid != null) {
159       List<EcompRole> userRoles = null;
160       try {
161         userRoles = portalRestCentralImpl.getUserRoles(uid);
162       } catch (PortalAPIException e) {
163         LOG.error(AaiUiMsgs.ERROR_GENERIC, "Unable to get user roles from Portal");
164       }
165       if (userRoles == null || appRoles.isEmpty()) {
166         LOG.debug(AaiUiMsgs.DEBUG_GENERIC, " Role list is either null or empty");
167         return hasAccess;
168       } else {
169         for (EcompRole userRole : userRoles) {
170           if (appRoles.contains(userRole.getName())) {
171             hasAccess = true;
172           }
173         }
174       }
175     }
176     return hasAccess;
177   }
178 }