Fixes for sonar critical issues
[policy/engine.git] / ONAP-SDK-APP / src / main / java / org / openecomp / portalapp / login / LoginStrategyImpl.java
1 package org.openecomp.portalapp.login;
2
3 import javax.servlet.http.Cookie;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6
7 import org.onap.policy.common.logging.flexlogger.FlexLogger;
8 import org.onap.policy.common.logging.flexlogger.Logger;
9 import org.openecomp.portalsdk.core.auth.LoginStrategy;
10 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
11 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
12 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
13 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
14 import org.springframework.web.servlet.ModelAndView;
15
16 public class LoginStrategyImpl extends LoginStrategy {
17
18     private static final Logger LOGGER = FlexLogger.getLogger(LoginStrategyImpl.class);
19
20     @Override
21     public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
22         // 'login' for opensource is same as 'external' login.
23         return doExternalLogin(request, response);
24     }
25
26     @Override
27     public String getUserId(HttpServletRequest request) throws PortalAPIException {
28         // Check ONAP Portal cookie
29         if (!isLoginCookieExist(request))
30             return null;
31
32         String userid = null;
33         try {
34             userid = getUserIdFromCookie(request);
35         } catch (Exception e) {
36             LOGGER.error("Exception Occured" + e);
37         }
38         return userid;
39     }
40
41     private static String getUserIdFromCookie(HttpServletRequest request) throws PortalAPIException {
42         String userId = "";
43         Cookie[] cookies = request.getCookies();
44         Cookie userIdcookie = null;
45         if (cookies != null)
46             for (Cookie cookie : cookies)
47                 if (cookie.getName().equals(USER_ID))
48                     userIdcookie = cookie;
49         if (userIdcookie != null) {
50             try {
51                 userId = CipherUtil.decrypt(userIdcookie.getValue(),
52                         PortalApiProperties.getProperty(PortalApiConstants.Decryption_Key));
53             } catch (Exception e) {
54                 throw new PortalAPIException(e);
55             }
56         }
57         return userId;
58
59     }
60
61     private static boolean isLoginCookieExist(HttpServletRequest request) {
62         Cookie ep = getCookie(request, EP_SERVICE);
63         return (ep != null);
64     }
65
66     private static Cookie getCookie(HttpServletRequest request, String cookieName) {
67         Cookie[] cookies = request.getCookies();
68         if (cookies != null)
69             for (Cookie cookie : cookies)
70                 if (cookie.getName().equals(cookieName))
71                     return cookie;
72
73         return null;
74     }
75
76 }