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.onboarding.exception.PortalAPIException;
30 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
31 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
32 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
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[] cookies = request.getCookies();
63 Cookie userIdcookie = null;
65 for (Cookie cookie : cookies)
66 if (cookie.getName().equals(USER_ID))
67 userIdcookie = cookie;
68 if (userIdcookie != null) {
70 userId = CipherUtil.decrypt(userIdcookie.getValue(),
71 PortalApiProperties.getProperty(PortalApiConstants.Decryption_Key));
72 } catch (Exception e) {
73 throw new PortalAPIException(e);
80 private static boolean isLoginCookieExist(HttpServletRequest request) {
81 Cookie ep = getCookie(request, EP_SERVICE);
85 private static Cookie getCookie(HttpServletRequest request, String cookieName) {
86 Cookie[] cookies = request.getCookies();
88 for (Cookie cookie : cookies)
89 if (cookie.getName().equals(cookieName))