2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.security.portal;
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.LinkedHashSet;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
33 import org.onap.aai.sparky.security.EcompSso;
34 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
35 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
36 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
37 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
38 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
39 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Responds to ECOMP Portal's REST queries for user and role information and management.
46 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
48 private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
49 private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
51 private UserManager userManager;
54 * Initialise user manager.
56 public PortalRestAPIServiceImpl() {
57 userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
60 /////////////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////////////
68 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
69 * restful.domain.EcompUser)
72 public void pushUser(EcompUser user) throws PortalAPIException {
73 LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
75 if (userManager.getUser(user.getLoginId()).isPresent()) {
76 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
77 + ", user is already stored";
79 throw new PortalAPIException(message);
83 userManager.pushUser(user);
84 } catch (IOException e) {
85 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
86 LOG.error(message, e);
87 throw new PortalAPIException(message, e);
94 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
95 * com.att.fusion.core.restful.domain.EcompUser)
98 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
99 LOG.debug("Edit user [loginId:" + loginId + "]");
101 userManager.getUser(loginId).orElseThrow(() -> {
102 String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
104 return new PortalAPIException(message);
108 userManager.editUser(loginId, user);
109 } catch (IOException e) {
110 String message = getMessage(ERROR_MESSAGE, "edit", loginId);
111 LOG.error(message, e);
112 throw new PortalAPIException(message, e);
119 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
122 public EcompUser getUser(String loginId) throws PortalAPIException {
123 LOG.debug("Get user [loginId:" + loginId + "]");
124 return userManager.getUser(loginId).orElseThrow(() -> {
125 String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
127 return new PortalAPIException(message);
134 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
137 public List<EcompUser> getUsers() throws PortalAPIException {
138 LOG.debug("Get users");
139 return userManager.getUsers();
143 public String getUserId(HttpServletRequest request) throws PortalAPIException {
144 return EcompSso.validateEcompSso(request);
147 /////////////////////////////////////////////////////////////////////////////
149 /////////////////////////////////////////////////////////////////////////////
151 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
152 LOG.debug("Get available roles");
153 return UserManager.getRoles();
160 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
163 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
164 LOG.debug("Get user roles");
165 return userManager.getUserRoles(loginId);
172 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
176 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
177 LOG.debug("Push user role [loginId:" + loginId + "]");
179 EcompUser user = getUser(loginId);
181 user.setRoles(new LinkedHashSet<EcompRole>(roles));
183 user.setRoles(new LinkedHashSet<EcompRole>());
185 editUser(loginId, user);
186 } catch (PortalAPIException e) {
187 String message = getMessage(ERROR_MESSAGE, "push role", loginId);
189 throw new PortalAPIException(message, e);
193 /////////////////////////////////////////////////////////////////////////////
194 // Security interface
195 /////////////////////////////////////////////////////////////////////////////
201 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
202 * http.HttpServletRequest)
205 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
206 LOG.debug("Authentication request");
207 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
208 String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
209 String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
210 return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
211 && restPassword.equals(config.getPassword());
214 private String getMessage(String message, Object... args) {
215 MessageFormat formatter = new MessageFormat("");
216 formatter.applyPattern(message);
217 return formatter.format(args);
220 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
221 LOG.debug("Get available roles");
222 return UserManager.getRoles();