5a631198fee82f384ec697677de59a7a862f1edb
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP 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 package org.openecomp.portalsdk.core.interceptor;
21
22 import java.net.HttpURLConnection;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpSession;
28
29 import org.openecomp.portalsdk.core.controller.FusionBaseController;
30 import org.openecomp.portalsdk.core.domain.App;
31 import org.openecomp.portalsdk.core.exception.UrlAccessRestrictedException;
32 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
35 import org.openecomp.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
36 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
37 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
38 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
39 import org.openecomp.portalsdk.core.service.DataAccessService;
40 import org.openecomp.portalsdk.core.service.LoginService;
41 import org.openecomp.portalsdk.core.service.UrlAccessService;
42 import org.openecomp.portalsdk.core.service.WebServiceCallService;
43 import org.openecomp.portalsdk.core.util.SystemProperties;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.web.method.HandlerMethod;
46 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
47
48 public class ResourceInterceptor extends HandlerInterceptorAdapter {
49         public static final String APP_METADATA = "APP.METADATA";
50
51         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ResourceInterceptor.class);
52
53         @Autowired
54         private DataAccessService dataAccessService;
55         @Autowired
56         private LoginService loginService;
57         @Autowired
58         private WebServiceCallService webServiceCallService;
59
60         private AbstractCacheManager cacheManager;
61
62         @Autowired
63         UrlAccessService urlAccessService;
64         
65         @Override
66         public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
67                         throws Exception {
68                 String uri = request.getRequestURI();
69                 String url = uri.substring(uri.indexOf("/", 1) + 1);
70                 logger.info(EELFLoggerDelegate.debugLogger, "Url - " + url);
71                 logger.info(EELFLoggerDelegate.debugLogger, "lastIndexOf - " + uri.substring(uri.lastIndexOf("/") + 1));
72                 if (handler instanceof HandlerMethod) {
73                         HandlerMethod method = (HandlerMethod) handler;
74                         FusionBaseController controller = (FusionBaseController) method.getBean();
75                         if (!controller.isAccessible()) {
76                                 if (controller.isRESTfulCall()) {
77                                         // check user authentication for RESTful calls
78                                         String secretKey = null;
79                                         try {
80                                                 if (!webServiceCallService.verifyRESTCredential(secretKey, request.getHeader("username"),
81                                                                 request.getHeader("password"))) {
82                                                         logger.error(EELFLoggerDelegate.errorLogger, "Error accesing RESTful service. Un-authorized",AlarmSeverityEnum.MINOR);
83                                                         throw new UrlAccessRestrictedException();
84                                                 }
85                                         } catch (Exception e) {
86                                                 logger.error(EELFLoggerDelegate.errorLogger, "Error authenticating RESTful service :" + e,AlarmSeverityEnum.MINOR);
87                                                 //throw new UrlAccessRestrictedException();
88                                                  HttpSession httpSession = request.getSession();
89                                                     ((HttpServletResponse) response).setStatus(HttpURLConnection.HTTP_UNAUTHORIZED);
90                                                     return false;
91                                         }
92                                 }
93                                 if (!urlAccessService.isUrlAccessible(request, url)) {
94                                         logger.error(EELFLoggerDelegate.errorLogger, "Error accesing URL. Un-authorized",AlarmSeverityEnum.MINOR);
95                                         throw new UrlAccessRestrictedException();
96                                 }
97                         }
98                 }
99
100                 logger.debug("successfully authorized rest call");
101                 logger.info(EELFLoggerDelegate.debugLogger, "successfully authorized rest call");
102                 handleSessionUpdates(request);
103                 logger.debug("handled session updates for synchronization");
104                 logger.info(EELFLoggerDelegate.debugLogger, "handled session updates for synchronization");
105                 return super.preHandle(request, response, handler);
106         }
107
108         /**
109          * 
110          * @param request
111          */
112         protected void handleSessionUpdates(HttpServletRequest request) {
113
114                 App app = null;
115                 Object appObj = getCacheManager().getObject(APP_METADATA);
116                 if (appObj == null) {
117                         app = findApp();
118                         getCacheManager().putObject(APP_METADATA, app);
119
120                 } else {
121                         app = (App) appObj;
122                 }
123
124                 String ecompRestURL = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL);
125                 String decreptedPwd = "";
126                 try {
127                         decreptedPwd = CipherUtil.decrypt(app.getAppPassword(),
128                                         SystemProperties.getProperty(SystemProperties.Decryption_Key));
129                 } catch (Exception e) {
130                         logger.error(EELFLoggerDelegate.errorLogger, "Could not decrypt Password" + e.getMessage(),AlarmSeverityEnum.MINOR);
131                 }
132
133                 PortalTimeoutHandler.handleSessionUpdatesNative(request, app.getUsername(), decreptedPwd,
134                                 PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY), ecompRestURL, null);
135         }
136
137         public App findApp() {
138                 List<?> list = null;
139                 StringBuffer criteria = new StringBuffer();
140                 criteria.append(" where id = 1");
141                 list = getDataAccessService().getList(App.class, criteria.toString(), null, null);
142                 return (list == null || list.size() == 0) ? null : (App) list.get(0);
143         }
144
145         public DataAccessService getDataAccessService() {
146                 return dataAccessService;
147         }
148
149         public void setDataAccessService(DataAccessService dataAccessService) {
150                 this.dataAccessService = dataAccessService;
151         }
152
153         public LoginService getLoginService() {
154                 return loginService;
155         }
156
157         public void setLoginService(LoginService loginService) {
158                 this.loginService = loginService;
159         }
160
161         @Autowired
162         public void setCacheManager(AbstractCacheManager cacheManager) {
163                 this.cacheManager = cacheManager;
164         }
165
166         public AbstractCacheManager getCacheManager() {
167                 return cacheManager;
168         }
169
170 }