Persistent XSS vulnerability in basicAuthAccount form fix
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / BasicAuthAccountController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.onap.portalapp.controller.EPRestrictedBaseController;
47 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
48 import org.onap.portalapp.portal.domain.EPEndpoint;
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
51 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
52 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
53 import org.onap.portalapp.portal.service.AdminRolesService;
54 import org.onap.portalapp.portal.service.BasicAuthAccountService;
55 import org.onap.portalapp.util.EPUserUtils;
56 import org.onap.portalapp.validation.DataValidator;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.context.annotation.EnableAspectJAutoProxy;
60 import org.springframework.web.bind.annotation.PathVariable;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.bind.annotation.RestController;
65
66 @RestController
67 @org.springframework.context.annotation.Configuration
68 @EnableAspectJAutoProxy
69 @EPAuditLog
70 public class BasicAuthAccountController extends EPRestrictedBaseController {
71
72     private static final String FAILURE = "FAILURE";
73     private static final String SUCCESS = "SUCCESS";
74     private static final String AUTHORIZATION_REQUIRED = "Authorization Required";
75     private static final String ADMIN_ONLY_OPERATIONS = "Admin Only Operation! ";
76
77     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthAccountController.class);
78     private final DataValidator dataValidator = new DataValidator();
79
80         @Autowired
81         private BasicAuthAccountService basicAuthAccountService;
82
83         @Autowired
84         private AdminRolesService adminRolesService;
85
86         /**
87          * Saves Basic Authentication account for external systems
88          *
89          * @param request
90          *            HttpServletRequest
91          * @param response
92          *            HttpServletResponse
93          * @param newBasicAuthAccount
94          *            BasicAuthCredentials
95          * @return Id of the newly created account
96          * @throws Exception
97          *             on failure
98          */
99         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
100         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
101                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
102
103
104
105                 EPUser user = EPUserUtils.getUserSession(request);
106                 if (!adminRolesService.isSuperAdmin(user)) {
107             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
108                     ADMIN_ONLY_OPERATIONS);
109                 }
110
111                 if (newBasicAuthAccount == null) {
112             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
113                                         "newBasicAuthAccount cannot be null or empty");
114                 }
115
116                 if(!dataValidator.isValid(newBasicAuthAccount)){
117                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "createBasicAuthAccount() failed, new credential are not safe",
118                                 "");
119                 }
120
121                 long accountId;
122                 try {
123                         accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
124                 } catch (Exception e){
125                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
126                 }
127
128                 List<Long> endpointIdList = new ArrayList<>();
129                 try {
130                         for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
131                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
132                         }
133                         for (Long endpointId : endpointIdList) {
134                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
135                         }
136                 } catch (Exception e) {
137             logger.error(EELFLoggerDelegate.errorLogger, "createBasicAuthAccount failed", e);
138             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
139                 }
140
141         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
142         }
143
144         /**
145          * Returns list of all BasicAuthCredentials in the system
146          *
147          * @param request
148          *            HttpServletRequest
149          * @param response
150          *            HttpServletResponse
151          * @return List<BasicAuthCredentials>
152          * @throws Exception
153          *             on failure
154          */
155
156         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
157         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
158                         HttpServletResponse response) throws Exception {
159
160                 EPUser user = EPUserUtils.getUserSession(request);
161                 if (!adminRolesService.isSuperAdmin(user)) {
162             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
163                                         "UnAuthorized! Admin Only Operation", new ArrayList<>());
164                 }
165
166         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "Success",
167                                 basicAuthAccountService.getAccountData());
168         }
169
170         /**
171          * Updates an existing BasicAuthCredentials account
172          *
173          * @param request
174          *            HttpServletRequest
175          * @param response
176          *            HttpServletResponse
177          * @param accountId
178          *            account ID
179          * @param newBasicAuthAccount
180          *            BasicAuthCredentials
181          * @return PortalRestResponse<String>
182          * @throws Exception
183          *             on failure
184          */
185         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
186         public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
187                         @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
188                         throws Exception {
189
190                 EPUser user = EPUserUtils.getUserSession(request);
191                 if (!adminRolesService.isSuperAdmin(user)) {
192             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
193                     ADMIN_ONLY_OPERATIONS);
194                 }
195
196                 if (newBasicAuthAccount == null) {
197             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
198                                         "BasicAuthCredentials cannot be null or empty");
199                 }
200                 try {
201                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
202                 } catch (Exception e) {
203             logger.error(EELFLoggerDelegate.errorLogger, "updateAccount failed", e);
204             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
205                 }
206         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
207         }
208
209         /**
210          * deletes an existing BasicAuthCredentials account
211          *
212          * @param request
213          *            HttpServletRequest
214          * @param response
215          *            HttpServletResponse
216          * @param accountId
217          *            account ID
218          * @return PortalRestResponse<String>
219          * @throws Exception
220          *             on failure
221          */
222         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
223         public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
224                         @PathVariable("accountId") long accountId) throws Exception {
225
226                 EPUser user = EPUserUtils.getUserSession(request);
227                 if (!adminRolesService.isSuperAdmin(user)) {
228             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
229                     ADMIN_ONLY_OPERATIONS);
230                 }
231
232                 try {
233                         basicAuthAccountService.deleteEndpointAccout(accountId);
234                 } catch (Exception e) {
235             logger.error(EELFLoggerDelegate.errorLogger, "deleteAccount failed", e);
236             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
237                 }
238         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
239         }
240
241 }