Merge "Remove code smells in dashboard"
[nonrtric.git] / dashboard / 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
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.lang.reflect.InvocationTargetException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
31 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
32 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Provides services to authenticate requests from/to ONAP Portal.
38  */
39 public class PortalAuthManager {
40
41     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42
43     final Map<String, String> credentialsMap;
44     private final IPortalSdkDecryptor portalSdkDecryptor;
45     private final String userIdCookieName;
46
47     public PortalAuthManager(final String appName, final String username, final String password,
48         final String decryptorClassName, final String userCookie) throws ClassNotFoundException, InstantiationException,
49         IllegalAccessException, InvocationTargetException, NoSuchMethodException {
50         credentialsMap = new HashMap<>();
51         credentialsMap.put(IPortalRestCentralService.CREDENTIALS_APP, appName);
52         credentialsMap.put(IPortalRestCentralService.CREDENTIALS_USER, username);
53         credentialsMap.put(IPortalRestCentralService.CREDENTIALS_PASS, password);
54         this.userIdCookieName = userCookie;
55         // Instantiate here so configuration errors are detected at app-start time
56         logger.debug("ctor: using decryptor class {}", decryptorClassName);
57         Class<?> decryptorClass = Class.forName(decryptorClassName);
58         portalSdkDecryptor = (IPortalSdkDecryptor) decryptorClass.getDeclaredConstructor().newInstance();
59     }
60
61     /**
62      * @return A map of key-value pairs with application name, user name and
63      *         password.
64      */
65     public Map<String, String> getAppCredentials() {
66         return credentialsMap;
67     }
68
69     /**
70      * Searches the request for a cookie with the specified name.
71      *
72      * @param request
73      *        HttpServletRequest
74      * @param cookieName
75      *        Cookie name
76      * @return Cookie, or null if not found.
77      */
78     private Cookie getCookie(HttpServletRequest request, String cookieName) {
79         Cookie[] cookies = request.getCookies();
80         if (cookies != null)
81             for (Cookie cookie : cookies)
82                 if (cookie.getName().equals(cookieName))
83                     return cookie;
84         return null;
85     }
86
87     /**
88      * Validates whether the ECOMP Portal sign-on process has completed. Checks for
89      * the ECOMP cookie first, then the user cookie.
90      *
91      * @param request
92      *        HttpServletRequest
93      * @return User ID if the ECOMP cookie is present and the sign-on process
94      *         established a user ID; else null.
95      */
96     public String validateEcompSso(HttpServletRequest request) {
97         // Check ECOMP Portal cookie
98         Cookie ep = getCookie(request, PortalApiConstants.EP_SERVICE);
99         if (ep == null) {
100             logger.debug("validateEcompSso: cookie not found: {}", PortalApiConstants.EP_SERVICE);
101             return null;
102         }
103         logger.trace("validateEcompSso: found cookie {}", PortalApiConstants.EP_SERVICE);
104         Cookie user = getCookie(request, userIdCookieName);
105         if (user == null) {
106             logger.debug("validateEcompSso: cookie not found: {}", userIdCookieName);
107             return null;
108         }
109         logger.trace("validateEcompSso: user cookie {}", userIdCookieName);
110         String userid = null;
111         try {
112             userid = portalSdkDecryptor.decrypt(user.getValue());
113         } catch (CipherUtilException e) {
114             throw new IllegalArgumentException("validateEcompSso failed", e);
115         }
116         return userid;
117     }
118
119 }