Upgraded the latest ONAP SDK
[policy/engine.git] / ONAP-SDK-APP / src / main / java / org / onap / portalapp / login / LoginStrategyImpl.java
1 /*-
2  * ================================================================================
3  * ONAP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 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  * ================================================================================
19  */
20
21 package org.onap.portalapp.login;
22
23 import javax.servlet.http.Cookie;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.onap.portalsdk.core.auth.LoginStrategy;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
30 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
31 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
32 import org.onap.portalsdk.core.util.SystemProperties;
33 import org.springframework.web.servlet.ModelAndView;
34
35 /**
36  * Implements basic single-signon login strategy for open-source applications
37  * when users start at Portal. Extracts an encrypted user ID sent by Portal.
38  */
39 public class LoginStrategyImpl extends LoginStrategy {
40
41         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoginStrategyImpl.class);
42
43         /**
44          * login for open source is same as external login in the non-open-source
45          * version.
46          */
47         @Override
48         public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
49                 return doExternalLogin(request, response);
50         }
51
52         @Override
53         public String getUserId(HttpServletRequest request) throws PortalAPIException {
54                 // Check ECOMP Portal cookie
55                 Cookie ep = getCookie(request, EP_SERVICE);
56                 if (ep == null) {
57                         logger.debug(EELFLoggerDelegate.debugLogger, "getUserId: no EP_SERVICE cookie, returning null");
58                         return null;
59                 }
60
61                 String userid = null;
62                 try {
63                         userid = getUserIdFromCookie(request);
64                 } catch (Exception e) {
65                         logger.error(EELFLoggerDelegate.errorLogger, "getUserId failed", e);
66                 }
67                 return userid;
68         }
69
70         /**
71          * Searches the request for the user-ID cookie and decrypts the value using a
72          * key configured in properties
73          * 
74          * @param request
75          *            HttpServletRequest
76          * @return User ID
77          * @throws CipherUtilException
78          *             On any failure to decrypt
79          */
80         @SuppressWarnings("deprecation")
81         private String getUserIdFromCookie(HttpServletRequest request) throws CipherUtilException {
82                 String userId = "";
83                 Cookie userIdCookie = getCookie(request, USER_ID);
84                 if (userIdCookie != null) {
85                         final String cookieValue = userIdCookie.getValue();
86                         if (!SystemProperties.containsProperty(SystemProperties.Decryption_Key))
87                                 throw new IllegalStateException("Failed to find property " + SystemProperties.Decryption_Key);
88                         final String decryptionKey = SystemProperties.getProperty(SystemProperties.Decryption_Key);
89                         userId = CipherUtil.decrypt(cookieValue, decryptionKey);
90                         logger.debug(EELFLoggerDelegate.debugLogger, "getUserIdFromCookie: decrypted as {}", userId);
91                 }
92                 return userId;
93         }
94
95         /**
96          * Searches the request for the named cookie.
97          * 
98          * @param request
99          *            HttpServletRequest
100          * @param cookieName
101          *            Name of desired cookie
102          * @return Cookie if found; otherwise null.
103          */
104         private Cookie getCookie(HttpServletRequest request, String cookieName) {
105                 Cookie[] cookies = request.getCookies();
106                 if (cookies != null)
107                         for (Cookie cookie : cookies)
108                                 if (cookie.getName().equals(cookieName))
109                                         return cookie;
110                 return null;
111         }
112
113 }