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.
26 package org.openecomp.sparky.security.portal;
29 import java.io.IOException;
30 import java.text.MessageFormat;
31 import java.util.LinkedHashSet;
32 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
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.openecomp.sparky.security.EcompSso;
41 import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig;
42 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Responds to ECOMP Portal's REST queries for user and role information and management.
49 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
51 private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
52 private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
54 private UserManager userManager;
57 * Initialise user manager.
59 public PortalRestAPIServiceImpl() {
60 userManager = new UserManager(new File(TierSupportUiConstants.USERS_FILE_LOCATION));
63 /////////////////////////////////////////////////////////////////////////////
65 /////////////////////////////////////////////////////////////////////////////
71 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
72 * restful.domain.EcompUser)
75 public void pushUser(EcompUser user) throws PortalAPIException {
76 LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
78 if (userManager.getUser(user.getLoginId()).isPresent()) {
79 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
80 + ", user is already stored";
82 throw new PortalAPIException(message);
86 userManager.pushUser(user);
87 } catch (IOException e) {
88 String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
89 LOG.error(message, e);
90 throw new PortalAPIException(message, e);
97 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
98 * com.att.fusion.core.restful.domain.EcompUser)
101 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
102 LOG.debug("Edit user [loginId:" + loginId + "]");
104 userManager.getUser(loginId).orElseThrow(() -> {
105 String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
107 return new PortalAPIException(message);
111 userManager.editUser(loginId, user);
112 } catch (IOException e) {
113 String message = getMessage(ERROR_MESSAGE, "edit", loginId);
114 LOG.error(message, e);
115 throw new PortalAPIException(message, e);
122 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
125 public EcompUser getUser(String loginId) throws PortalAPIException {
126 LOG.debug("Get user [loginId:" + loginId + "]");
127 return userManager.getUser(loginId).orElseThrow(() -> {
128 String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
130 return new PortalAPIException(message);
137 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
140 public List<EcompUser> getUsers() throws PortalAPIException {
141 LOG.debug("Get users");
142 return userManager.getUsers();
146 public String getUserId(HttpServletRequest request) throws PortalAPIException {
147 return EcompSso.validateEcompSso(request);
150 /////////////////////////////////////////////////////////////////////////////
152 /////////////////////////////////////////////////////////////////////////////
157 * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getAvailableRoles()
160 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
161 LOG.debug("Get available roles");
162 return UserManager.getRoles();
169 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
172 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
173 LOG.debug("Get user roles");
174 return userManager.getUserRoles(loginId);
181 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
185 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
186 LOG.debug("Push user role [loginId:" + loginId + "]");
188 EcompUser user = getUser(loginId);
190 user.setRoles(new LinkedHashSet<EcompRole>(roles));
192 user.setRoles(new LinkedHashSet<EcompRole>());
194 editUser(loginId, user);
195 } catch (PortalAPIException e) {
196 String message = getMessage(ERROR_MESSAGE, "push role", loginId);
198 throw new PortalAPIException(message, e);
202 /////////////////////////////////////////////////////////////////////////////
203 // Security interface
204 /////////////////////////////////////////////////////////////////////////////
210 * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
211 * http.HttpServletRequest)
214 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
215 LOG.debug("Authentication request");
216 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
217 String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
218 String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
219 return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
220 && restPassword.equals(config.getPassword());
223 private String getMessage(String message, Object... args) {
224 MessageFormat formatter = new MessageFormat("");
225 formatter.applyPattern(message);
226 return formatter.format(args);