2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import java.util.ArrayList;
41 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
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.GetMapping;
63 import org.springframework.web.bind.annotation.PostMapping;
64 import org.springframework.web.bind.annotation.PutMapping;
65 import org.springframework.web.bind.annotation.DeleteMapping;
66 import org.springframework.web.bind.annotation.RestController;
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
72 public class BasicAuthAccountController extends EPRestrictedBaseController {
74 private static final String FAILURE = "FAILURE";
75 private static final String SUCCESS = "SUCCESS";
76 private static final String AUTHORIZATION_REQUIRED = "Authorization Required";
77 private static final String ADMIN_ONLY_OPERATIONS = "Admin Only Operation! ";
79 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthAccountController.class);
80 private final DataValidator dataValidator = new DataValidator();
83 private BasicAuthAccountService basicAuthAccountService;
86 private AdminRolesService adminRolesService;
89 * Saves Basic Authentication account for external systems
95 * @param newBasicAuthAccount
96 * BasicAuthCredentials
97 * @return Id of the newly created account
101 @PostMapping(value = { "/portalApi/basicAuthAccount" })
102 public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
103 @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
107 EPUser user = EPUserUtils.getUserSession(request);
108 if (!adminRolesService.isSuperAdmin(user)) {
109 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
110 ADMIN_ONLY_OPERATIONS);
113 if (newBasicAuthAccount == null) {
114 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
115 "newBasicAuthAccount cannot be null or empty");
118 if(!dataValidator.isValid(newBasicAuthAccount)){
119 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "createBasicAuthAccount() failed, new credential are not safe",
125 accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
126 } catch (Exception e){
127 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
130 List<Long> endpointIdList = new ArrayList<>();
132 for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
133 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
135 for (Long endpointId : endpointIdList) {
136 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
138 } catch (Exception e) {
139 logger.error(EELFLoggerDelegate.errorLogger, "createBasicAuthAccount failed", e);
140 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
143 return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
147 * Returns list of all BasicAuthCredentials in the system
152 * HttpServletResponse
153 * @return List<BasicAuthCredentials>
158 @GetMapping(value = { "/portalApi/basicAuthAccount" })
159 public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
160 HttpServletResponse response) throws Exception {
162 EPUser user = EPUserUtils.getUserSession(request);
163 if (!adminRolesService.isSuperAdmin(user)) {
164 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
165 "UnAuthorized! Admin Only Operation", new ArrayList<>());
168 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "Success",
169 basicAuthAccountService.getAccountData());
173 * Updates an existing BasicAuthCredentials account
178 * HttpServletResponse
181 * @param newBasicAuthAccount
182 * BasicAuthCredentials
183 * @return PortalRestResponse<String>
187 @PutMapping(value = { "/portalApi/basicAuthAccount/{accountId}" })
188 public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
189 @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
192 EPUser user = EPUserUtils.getUserSession(request);
193 if (!adminRolesService.isSuperAdmin(user)) {
194 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
195 ADMIN_ONLY_OPERATIONS);
198 if (newBasicAuthAccount == null) {
199 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
200 "BasicAuthCredentials cannot be null or empty");
203 basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
204 } catch (Exception e) {
205 logger.error(EELFLoggerDelegate.errorLogger, "updateAccount failed", e);
206 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
208 return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
212 * deletes an existing BasicAuthCredentials account
217 * HttpServletResponse
220 * @return PortalRestResponse<String>
224 @DeleteMapping(value = { "/portalApi/basicAuthAccount/{accountId}" })
225 public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
226 @PathVariable("accountId") long accountId) throws Exception {
228 EPUser user = EPUserUtils.getUserSession(request);
229 if (!adminRolesService.isSuperAdmin(user)) {
230 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
231 ADMIN_ONLY_OPERATIONS);
235 basicAuthAccountService.deleteEndpointAccout(accountId);
236 } catch (Exception e) {
237 logger.error(EELFLoggerDelegate.errorLogger, "deleteAccount failed", e);
238 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
240 return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");