0835b7b6bafde6bf507d94cf5e3f4832f00ae6f6
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPIServiceImpl.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.File;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32
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.onap.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
37 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
38 import org.onap.portalsdk.core.restful.domain.EcompRole;
39 import org.onap.portalsdk.core.restful.domain.EcompUser;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Responds to ECOMP Portal's REST queries for user and role information and management.
45  */
46 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
47
48   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
49   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
50
51   private UserManager userManager;
52
53   /**
54    * Initialise user manager.
55    */
56   public PortalRestAPIServiceImpl() {
57     userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
58   }
59
60   /////////////////////////////////////////////////////////////////////////////
61   // User interface
62   /////////////////////////////////////////////////////////////////////////////
63
64   /*
65    * (non-Javadoc)
66    *
67    */
68   @Override
69   public void pushUser(EcompUser user) throws PortalAPIException {
70     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
71
72     if (userManager.getUser(user.getLoginId()).isPresent()) {
73       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
74           + ", user is already stored";
75       LOG.error(message);
76       throw new PortalAPIException(message);
77     }
78
79     try {
80       userManager.pushUser(user);
81     } catch (IOException e) {
82       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
83       LOG.error(message, e);
84       throw new PortalAPIException(message, e);
85     }
86   }
87
88   /*
89    * (non-Javadoc)
90    *
91    */
92   @Override
93   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
94     LOG.debug("Edit user [loginId:" + loginId + "]");
95
96     userManager.getUser(loginId).orElseThrow(() -> {
97       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
98       LOG.error(message);
99       return new PortalAPIException(message);
100     });
101
102     try {
103       userManager.editUser(loginId, user);
104     } catch (IOException e) {
105       String message = getMessage(ERROR_MESSAGE, "edit", loginId);
106       LOG.error(message, e);
107       throw new PortalAPIException(message, e);
108     }
109   }
110
111   /*
112    * (non-Javadoc)
113    *
114    */
115   @Override
116   public EcompUser getUser(String loginId) throws PortalAPIException {
117     LOG.debug("Get user [loginId:" + loginId + "]");
118     return userManager.getUser(loginId).orElseThrow(() -> {
119       String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
120       LOG.error(message);
121       return new PortalAPIException(message);
122     });
123   }
124
125   /*
126    * (non-Javadoc)
127    *
128    */
129   @Override
130   public List<EcompUser> getUsers() throws PortalAPIException {
131     LOG.debug("Get users");
132     return userManager.getUsers();
133   }
134
135   @Override
136   public String getUserId(HttpServletRequest request) throws PortalAPIException {
137     return EcompSso.validateEcompSso(request);
138   }
139
140   /////////////////////////////////////////////////////////////////////////////
141   // Role interface
142   /////////////////////////////////////////////////////////////////////////////
143
144   public List<EcompRole> getAvailableRoles() throws PortalAPIException {
145     LOG.debug("Get available roles");
146     return UserManager.getRoles();
147   }
148
149   /*
150    * (non-Javadoc)
151    *
152    * @see
153    */
154   @Override
155   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
156     LOG.debug("Get user roles");
157     return userManager.getUserRoles(loginId);
158   }
159
160   /*
161    * (non-Javadoc)
162    *
163    */
164   @Override
165   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
166     LOG.debug("Push user role [loginId:" + loginId + "]");
167     try {
168       EcompUser user = getUser(loginId);
169       if (roles != null) {
170         user.setRoles(new LinkedHashSet<EcompRole>(roles));
171       } else {
172         user.setRoles(new LinkedHashSet<EcompRole>());
173       }
174       editUser(loginId, user);
175     } catch (PortalAPIException e) {
176       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
177       LOG.error(message);
178       throw new PortalAPIException(message, e);
179     }
180   }
181
182   /////////////////////////////////////////////////////////////////////////////
183   // Security interface
184   /////////////////////////////////////////////////////////////////////////////
185
186   /*
187    * (non-Javadoc)
188    *
189    */
190   @Override
191   public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
192     LOG.debug("Authentication request");
193     PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
194     String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
195     String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
196     return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
197         && restPassword.equals(config.getPassword());
198   }
199
200   private String getMessage(String message, Object... args) {
201     MessageFormat formatter = new MessageFormat("");
202     formatter.applyPattern(message);
203     return formatter.format(args);
204   }
205
206   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
207     LOG.debug("Get available roles");
208     return UserManager.getRoles();
209   }
210   
211   public Map<String, String> getCredentials() {
212     PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
213     Map<String, String> credentialsMap = new HashMap<>();
214     String appUserName = config.getUsername();
215     String appPassword = config.getPassword();
216
217     credentialsMap.put("username", appUserName);
218     credentialsMap.put("password", appPassword);
219     return credentialsMap;
220   }
221 }