2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sparky.security.portal;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.LinkedHashSet;
27 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
31 import org.onap.aai.sparky.security.EcompSso;
32 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
33 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
34 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
35 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
36 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
37 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Responds to ECOMP Portal's REST queries for user and role information and management.
44 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
46 private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
47 private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
49 private UserManager userManager;
52 * Initialise user manager.
54 public PortalRestAPIServiceImpl() {
55 userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
58 /////////////////////////////////////////////////////////////////////////////
60 /////////////////////////////////////////////////////////////////////////////
67 public void pushUser(EcompUser user) throws PortalAPIException {
68 LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
70 if (userManager.getUser(user.getLoginId()).isPresent()) {
71 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
72 + ", user is already stored";
74 throw new PortalAPIException(message);
78 userManager.pushUser(user);
79 } catch (IOException e) {
80 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
81 LOG.error(message, e);
82 throw new PortalAPIException(message, e);
91 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
92 LOG.debug("Edit user [loginId:" + loginId + "]");
94 userManager.getUser(loginId).orElseThrow(() -> {
95 String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
97 return new PortalAPIException(message);
101 userManager.editUser(loginId, user);
102 } catch (IOException e) {
103 String message = getMessage(ERROR_MESSAGE, "edit", loginId);
104 LOG.error(message, e);
105 throw new PortalAPIException(message, e);
114 public EcompUser getUser(String loginId) throws PortalAPIException {
115 LOG.debug("Get user [loginId:" + loginId + "]");
116 return userManager.getUser(loginId).orElseThrow(() -> {
117 String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
119 return new PortalAPIException(message);
128 public List<EcompUser> getUsers() throws PortalAPIException {
129 LOG.debug("Get users");
130 return userManager.getUsers();
134 public String getUserId(HttpServletRequest request) throws PortalAPIException {
135 return EcompSso.validateEcompSso(request);
138 /////////////////////////////////////////////////////////////////////////////
140 /////////////////////////////////////////////////////////////////////////////
142 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
143 LOG.debug("Get available roles");
144 return UserManager.getRoles();
153 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
154 LOG.debug("Get user roles");
155 return userManager.getUserRoles(loginId);
163 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
164 LOG.debug("Push user role [loginId:" + loginId + "]");
166 EcompUser user = getUser(loginId);
168 user.setRoles(new LinkedHashSet<EcompRole>(roles));
170 user.setRoles(new LinkedHashSet<EcompRole>());
172 editUser(loginId, user);
173 } catch (PortalAPIException e) {
174 String message = getMessage(ERROR_MESSAGE, "push role", loginId);
176 throw new PortalAPIException(message, e);
180 /////////////////////////////////////////////////////////////////////////////
181 // Security interface
182 /////////////////////////////////////////////////////////////////////////////
189 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
190 LOG.debug("Authentication request");
191 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
192 String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
193 String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
194 return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
195 && restPassword.equals(config.getPassword());
198 private String getMessage(String message, Object... args) {
199 MessageFormat formatter = new MessageFormat("");
200 formatter.applyPattern(message);
201 return formatter.format(args);
204 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
205 LOG.debug("Get available roles");
206 return UserManager.getRoles();