Integrate EPSDK-FW library for auth and users
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / portalapi / PortalAuthManager.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.portalapi;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
30 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Provides services to authenticate requests from/to ONAP Portal.
36  */
37 public class PortalAuthManager {
38
39         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41         final Map<String, String> credentialsMap;
42         private final IPortalSdkDecryptor portalSdkDecryptor;
43         private final String userIdCookieName;
44
45         public PortalAuthManager(final String appName, final String username, final String password,
46                         final String decryptorClassName, final String userCookie)
47                         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
48                 credentialsMap = new HashMap<>();
49                 // The map keys are hardcoded in EPSDK-FW, no constants are defined :(
50                 credentialsMap.put("appName", appName);
51                 credentialsMap.put("username", username);
52                 credentialsMap.put("password", password);
53                 this.userIdCookieName = userCookie;
54                 // Instantiate here so configuration errors are detected at app-start time
55                 logger.debug("ctor: using decryptor class {}", decryptorClassName);
56                 Class<?> decryptorClass = Class.forName(decryptorClassName);
57                 portalSdkDecryptor = (IPortalSdkDecryptor) decryptorClass.newInstance();
58         }
59
60         /**
61          * @return A map of key-value pairs with application name, user name and
62          *         password.
63          */
64         public Map<String, String> getAppCredentials() {
65                 return credentialsMap;
66         }
67
68         /**
69          * Searches the request for a cookie with the specified name.
70          *
71          * @param request
72          *                       HttpServletRequest
73          * @param cookieName
74          *                       Cookie name
75          * @return Cookie, or null if not found.
76          */
77         private Cookie getCookie(HttpServletRequest request, String cookieName) {
78                 Cookie[] cookies = request.getCookies();
79                 if (cookies != null)
80                         for (Cookie cookie : cookies)
81                                 if (cookie.getName().equals(cookieName))
82                                         return cookie;
83                 return null;
84         }
85
86         /**
87          * Validates whether the ECOMP Portal sign-on process has completed. Checks for
88          * the ECOMP cookie first, then the user cookie.
89          * 
90          * @param request
91          *                    HttpServletRequest
92          * @return User ID if the ECOMP cookie is present and the sign-on process
93          *         established a user ID; else null.
94          */
95         public String valdiateEcompSso(HttpServletRequest request) {
96                 // Check ECOMP Portal cookie
97                 Cookie ep = getCookie(request, PortalApiConstants.EP_SERVICE);
98                 if (ep == null) {
99                         logger.debug("valdiateEcompSso: cookie not found: {}", PortalApiConstants.EP_SERVICE);
100                         return null;
101                 }
102                 logger.trace("validateEcompSso: found cookie {}", PortalApiConstants.EP_SERVICE);
103                 Cookie user = getCookie(request, userIdCookieName);
104                 if (user == null) {
105                         logger.debug("valdiateEcompSso: cookie not found: {}", userIdCookieName);
106                         return null;
107                 }
108                 logger.trace("validateEcompSso: user cookie {}", userIdCookieName);
109                 String userid = null;
110                 try {
111                         userid = portalSdkDecryptor.decrypt(user.getValue());
112                 } catch (CipherUtilException e) {
113                         throw new IllegalArgumentException("valdiateEcompSso failed", e);
114                 }
115                 return userid;
116         }
117
118 }