2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.login;
22 import javax.servlet.http.Cookie;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.portalsdk.core.auth.LoginStrategy;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
31 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
32 import org.openecomp.portalsdk.core.util.SystemProperties;
33 import org.springframework.web.servlet.ModelAndView;
35 public class LoginStrategyImpl extends LoginStrategy {
37 private static final Logger LOGGER = FlexLogger.getLogger(LoginStrategyImpl.class);
40 public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
41 // 'login' for opensource is same as 'external' login.
42 return doExternalLogin(request, response);
46 public String getUserId(HttpServletRequest request) throws PortalAPIException {
47 // Check ONAP Portal cookie
48 if (!isLoginCookieExist(request))
53 userid = getUserIdFromCookie(request);
54 } catch (Exception e) {
55 LOGGER.error("Exception Occured" + e);
60 private static String getUserIdFromCookie(HttpServletRequest request) throws PortalAPIException {
62 Cookie userIdCookie = getCookie(request, USER_ID);
63 if (userIdCookie != null) {
64 final String cookieValue = userIdCookie.getValue();
65 if (!SystemProperties.containsProperty(SystemProperties.Decryption_Key))
66 throw new IllegalStateException("Failed to find property " + SystemProperties.Decryption_Key);
67 final String decryptionKey = SystemProperties.getProperty(SystemProperties.Decryption_Key);
69 userId = CipherUtil.decrypt(cookieValue, decryptionKey);
70 LOGGER.debug("getUserIdFromCookie: decrypted as {}" +userId);
71 } catch (Exception e) {
72 LOGGER.error("Exception Occured in getUserIdFromCookie" + e);
78 private static boolean isLoginCookieExist(HttpServletRequest request) {
79 Cookie ep = getCookie(request, EP_SERVICE);
83 private static Cookie getCookie(HttpServletRequest request, String cookieName) {
84 Cookie[] cookies = request.getCookies();
86 for (Cookie cookie : cookies)
87 if (cookie.getName().equals(cookieName))