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;
23 import java.io.IOException;
24 import java.text.MessageFormat;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import java.util.stream.Collectors;
32 import javax.servlet.http.HttpServletRequest;
34 import org.onap.aai.sparky.security.EcompSso;
35 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
36 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
37 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
38 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
39 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
40 import org.onap.portalsdk.core.onboarding.util.AuthUtil;
41 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
42 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
43 import org.onap.portalsdk.core.restful.domain.EcompRole;
44 import org.onap.portalsdk.core.restful.domain.EcompUser;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import com.fasterxml.jackson.databind.type.TypeFactory;
51 public class PortalRestAPICentralServiceImpl
52 implements IPortalRestCentralService, IPortalRestAPIService {
54 private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPICentralServiceImpl.class);
55 public static final String API_REDIRECT_VERSION = "/v4";
56 private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
57 private List<EcompUser> usersList;
58 private final ObjectMapper mapper;
62 * Initialize user list array.
64 public PortalRestAPICentralServiceImpl() {
65 usersList = new ArrayList<>();
66 mapper = new ObjectMapper();
71 public Map<String, String> getAppCredentials() throws PortalAPIException {
72 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
73 Map<String, String> appCredentialsMap = new HashMap<>();
74 String appUserName = config.getUsername();
75 String appPassword = config.getPassword();
77 appCredentialsMap.put("username", appUserName);
78 appCredentialsMap.put("password", appPassword);
79 return appCredentialsMap;
83 public void pushUser(EcompUser user) throws PortalAPIException {
84 // Do we really need to save the users? Can this method be just empty and not throw an
86 LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
87 if (usersList.size() == 0) {
90 for (EcompUser existingUser : this.usersList) {
91 if (existingUser.getLoginId().equals(user.getLoginId())) {
93 getMessage(ERROR_MESSAGE, "push", user.getLoginId()) + ", user is already stored";
95 throw new PortalAPIException(message);
104 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
105 // Do we really need to save the users? Can this method be just empty and not throw an
107 LOG.debug("Edit user [loginId:" + loginId + "]");
108 boolean isRemoved = false;
109 if (usersList.size() == 0) {
112 for (EcompUser existingUser : this.usersList) {
113 if (existingUser.getLoginId().equals(loginId)) {
114 isRemoved = usersList.remove(existingUser);
125 public String getUserId(HttpServletRequest request) throws PortalAPIException {
126 return EcompSso.validateEcompSso(request);
129 private String getMessage(String message, Object... args) {
130 MessageFormat formatter = new MessageFormat("");
131 formatter.applyPattern(message);
132 return formatter.format(args);
135 public List<EcompUser> getUsersList() {
140 public void setUsersList(List<EcompUser> usersList) {
141 this.usersList = usersList;
146 public EcompUser getUser(String loginId) throws PortalAPIException {
147 EcompUser user = new EcompUser();
148 String responseString = null;
150 responseString = RestWebServiceClient.getInstance().getPortalContent(
151 API_REDIRECT_VERSION + "/user/" + loginId, null, null, null,
152 getCredentials().get("username"), getCredentials().get("password"), true);
153 LOG.debug("responseString is: " + responseString);
154 user = mapper.readValue(responseString, EcompUser.class);
156 } catch (IOException e) {
157 String response = "PortalRestAPICentralServiceImpl.getUser failed";
158 LOG.error(response, e);
159 throw new PortalAPIException(response, e);
166 public List<EcompUser> getUsers() throws PortalAPIException {
167 List<EcompUser> usersList = new ArrayList<>();
168 String responseString = null;
171 RestWebServiceClient.getInstance().getPortalContent(API_REDIRECT_VERSION + "/users", null,
172 null, null, getCredentials().get("username"), getCredentials().get("password"), true);
173 LOG.debug("responseString is: " + responseString);
174 usersList = mapper.readValue(responseString,
175 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
177 } catch (IOException e) {
178 String response = "PortalRestAPICentralServiceImpl.getUsers failed";
179 LOG.error(response, e);
180 throw new PortalAPIException(response, e);
187 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
188 List<EcompRole> rolesList = new ArrayList<>();
189 String responseString = null;
191 responseString = RestWebServiceClient.getInstance().getPortalContent(
192 API_REDIRECT_VERSION + "/roles", requestedLoginId, null, null,
193 getCredentials().get("username"), getCredentials().get("password"), true);
194 LOG.debug("responseString is: " + responseString);
195 rolesList = mapper.readValue(responseString,
196 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
198 } catch (IOException e) {
199 String response = "PortalRestAPICentralServiceImpl.getRoles failed";
200 LOG.error(response, e);
201 throw new PortalAPIException(response, e);
208 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
209 throw new PortalAPIException("Please use Portal for Role Management");
214 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
215 List<EcompRole> userRoles = new ArrayList<>();
216 EcompUser user = new EcompUser();
217 String responseString = null;
219 responseString = RestWebServiceClient.getInstance().getPortalContent(
220 API_REDIRECT_VERSION + "/user/" + loginId, null, null, null,
221 getCredentials().get("username"), getCredentials().get("password"), true);
222 LOG.debug("responseString is: " + responseString);
223 user = mapper.readValue(responseString, EcompUser.class);
224 Set roles = user.getRoles();
225 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
227 } catch (IOException e) {
228 String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
229 LOG.error(response, e);
230 throw new PortalAPIException(response, e);
237 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
238 LOG.debug("Authentication request");
239 String nameSpace = PortalApiProperties.getProperty(PortalApiConstants.AUTH_NAMESPACE);
240 boolean accessAllowed = false;
242 accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace);
243 } catch (Exception e) {
244 String response = "PortalRestAPICentralServiceImpl.isAppAuthenticated failed";
245 LOG.error(response, e);
247 return accessAllowed;
252 public Map<String, String> getCredentials() throws PortalAPIException {
253 PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
254 Map<String, String> credentialsMap = new HashMap<>();
255 String appUserName = config.getUsername();
256 String appPassword = config.getPassword();
258 credentialsMap.put("username", appUserName);
259 credentialsMap.put("password", appPassword);
260 return credentialsMap;