2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file 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.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.security.portal;
28 import java.io.IOException;
29 import java.text.MessageFormat;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
35 import org.onap.aai.sparky.security.EcompSso;
36 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
37 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
38 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
39 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
40 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
41 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Responds to ECOMP Portal's REST queries for user and role information and management.
48 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
50 private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
51 private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
53 private UserManager userManager;
56 * Initialise user manager.
58 public PortalRestAPIServiceImpl() {
59 userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
62 /////////////////////////////////////////////////////////////////////////////
64 /////////////////////////////////////////////////////////////////////////////
71 public void pushUser(EcompUser user) throws PortalAPIException {
72 LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
74 if (userManager.getUser(user.getLoginId()).isPresent()) {
75 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
76 + ", user is already stored";
78 throw new PortalAPIException(message);
82 userManager.pushUser(user);
83 } catch (IOException e) {
84 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
85 LOG.error(message, e);
86 throw new PortalAPIException(message, e);
95 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
96 LOG.debug("Edit user [loginId:" + loginId + "]");
98 userManager.getUser(loginId).orElseThrow(() -> {
99 String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
101 return new PortalAPIException(message);
105 userManager.editUser(loginId, user);
106 } catch (IOException e) {
107 String message = getMessage(ERROR_MESSAGE, "edit", loginId);
108 LOG.error(message, e);
109 throw new PortalAPIException(message, e);
118 public EcompUser getUser(String loginId) throws PortalAPIException {
119 LOG.debug("Get user [loginId:" + loginId + "]");
120 return userManager.getUser(loginId).orElseThrow(() -> {
121 String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
123 return new PortalAPIException(message);
132 public List<EcompUser> getUsers() throws PortalAPIException {
133 LOG.debug("Get users");
134 return userManager.getUsers();
138 public String getUserId(HttpServletRequest request) throws PortalAPIException {
139 return EcompSso.validateEcompSso(request);
142 /////////////////////////////////////////////////////////////////////////////
144 /////////////////////////////////////////////////////////////////////////////
146 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
147 LOG.debug("Get available roles");
148 return UserManager.getRoles();
157 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
158 LOG.debug("Get user roles");
159 return userManager.getUserRoles(loginId);
167 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
168 LOG.debug("Push user role [loginId:" + loginId + "]");
170 EcompUser user = getUser(loginId);
172 user.setRoles(new LinkedHashSet<EcompRole>(roles));
174 user.setRoles(new LinkedHashSet<EcompRole>());
176 editUser(loginId, user);
177 } catch (PortalAPIException e) {
178 String message = getMessage(ERROR_MESSAGE, "push role", loginId);
180 throw new PortalAPIException(message, e);
184 /////////////////////////////////////////////////////////////////////////////
185 // Security interface
186 /////////////////////////////////////////////////////////////////////////////
193 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
194 LOG.debug("Authentication request");
195 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
196 String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
197 String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
198 return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
199 && restPassword.equals(config.getPassword());
202 private String getMessage(String message, Object... args) {
203 MessageFormat formatter = new MessageFormat("");
204 formatter.applyPattern(message);
205 return formatter.format(args);
208 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
209 LOG.debug("Get available roles");
210 return UserManager.getRoles();