fix backdoor issue when using portal
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPICentralServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sparky.security.portal;
22
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;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31
32 import javax.servlet.http.HttpServletRequest;
33
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;
47
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import com.fasterxml.jackson.databind.type.TypeFactory;
50
51 public class PortalRestAPICentralServiceImpl
52     implements IPortalRestCentralService, IPortalRestAPIService {
53
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;
59
60
61   /**
62    * Initialize user list array.
63    */
64   public PortalRestAPICentralServiceImpl() {
65     usersList = new ArrayList<>();
66     mapper = new ObjectMapper();
67   }
68
69
70   @Override
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();
76
77     appCredentialsMap.put("username", appUserName);
78     appCredentialsMap.put("password", appPassword);
79     return appCredentialsMap;
80   }
81
82   @Override
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
85     // exception?
86     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
87     if (usersList.size() == 0) {
88       usersList.add(user);
89     } else {
90       for (EcompUser existingUser : this.usersList) {
91         if (existingUser.getLoginId().equals(user.getLoginId())) {
92           String message =
93               getMessage(ERROR_MESSAGE, "push", user.getLoginId()) + ", user is already stored";
94           LOG.error(message);
95           throw new PortalAPIException(message);
96         }
97         usersList.add(user);
98
99       }
100     }
101   }
102
103   @Override
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
106     // exception?
107     LOG.debug("Edit user [loginId:" + loginId + "]");
108     boolean isRemoved = false;
109     if (usersList.size() == 0) {
110       usersList.add(user);
111     } else {
112       for (EcompUser existingUser : this.usersList) {
113         if (existingUser.getLoginId().equals(loginId)) {
114           isRemoved = usersList.remove(existingUser);
115         }
116         if (isRemoved) {
117           usersList.add(user);
118         }
119
120       }
121     }
122   }
123
124   @Override
125   public String getUserId(HttpServletRequest request) throws PortalAPIException {
126     return EcompSso.validateEcompSso(request);
127   }
128
129   private String getMessage(String message, Object... args) {
130     MessageFormat formatter = new MessageFormat("");
131     formatter.applyPattern(message);
132     return formatter.format(args);
133   }
134
135   public List<EcompUser> getUsersList() {
136     return usersList;
137   }
138
139
140   public void setUsersList(List<EcompUser> usersList) {
141     this.usersList = usersList;
142   }
143
144
145   @Override
146   public EcompUser getUser(String loginId) throws PortalAPIException {
147     EcompUser user = new EcompUser();
148     String responseString = null;
149     try {
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);
155
156     } catch (IOException e) {
157       String response = "PortalRestAPICentralServiceImpl.getUser failed";
158       LOG.error(response, e);
159       throw new PortalAPIException(response, e);
160     }
161     return user;
162   }
163
164
165   @Override
166   public List<EcompUser> getUsers() throws PortalAPIException {
167     List<EcompUser> usersList = new ArrayList<>();
168     String responseString = null;
169     try {
170       responseString =
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));
176
177     } catch (IOException e) {
178       String response = "PortalRestAPICentralServiceImpl.getUsers failed";
179       LOG.error(response, e);
180       throw new PortalAPIException(response, e);
181     }
182     return usersList;
183   }
184
185
186   @Override
187   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
188     List<EcompRole> rolesList = new ArrayList<>();
189     String responseString = null;
190     try {
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));
197
198     } catch (IOException e) {
199       String response = "PortalRestAPICentralServiceImpl.getRoles failed";
200       LOG.error(response, e);
201       throw new PortalAPIException(response, e);
202     }
203     return rolesList;
204   }
205
206
207   @Override
208   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
209     throw new PortalAPIException("Please use Portal for Role Management");
210   }
211
212
213   @Override
214   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
215     List<EcompRole> userRoles = new ArrayList<>();
216     EcompUser user = new EcompUser();
217     String responseString = null;
218     try {
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());
226
227     } catch (IOException e) {
228       String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
229       LOG.error(response, e);
230       throw new PortalAPIException(response, e);
231     }
232     return userRoles;
233   }
234
235
236   @Override
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;
241     try {
242       accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace);
243     } catch (Exception e) {
244       String response = "PortalRestAPICentralServiceImpl.isAppAuthenticated failed";
245       LOG.error(response, e);
246     }
247     return accessAllowed;
248   }
249
250
251   @Override
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();
257
258     credentialsMap.put("username", appUserName);
259     credentialsMap.put("password", appPassword);
260     return credentialsMap;
261   }
262
263 }