3bfc7eaeabb62fb2d15285cb2c14075dd6fff391
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPIServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.security.portal;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.LinkedHashSet;
29 import java.util.List;
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.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;
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    * @see
68    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
69    * restful.domain.EcompUser)
70    */
71   @Override
72   public void pushUser(EcompUser user) throws PortalAPIException {
73     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
74
75     if (userManager.getUser(user.getLoginId()).isPresent()) {
76       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
77           + ", user is already stored";
78       LOG.error(message);
79       throw new PortalAPIException(message);
80     }
81
82     try {
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);
88     }
89   }
90
91   /*
92    * (non-Javadoc)
93    *
94    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
95    * com.att.fusion.core.restful.domain.EcompUser)
96    */
97   @Override
98   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
99     LOG.debug("Edit user [loginId:" + loginId + "]");
100
101     userManager.getUser(loginId).orElseThrow(() -> {
102       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
103       LOG.error(message);
104       return new PortalAPIException(message);
105     });
106
107     try {
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);
113     }
114   }
115
116   /*
117    * (non-Javadoc)
118    *
119    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
120    */
121   @Override
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";
126       LOG.error(message);
127       return new PortalAPIException(message);
128     });
129   }
130
131   /*
132    * (non-Javadoc)
133    *
134    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
135    */
136   @Override
137   public List<EcompUser> getUsers() throws PortalAPIException {
138     LOG.debug("Get users");
139     return userManager.getUsers();
140   }
141
142   @Override
143   public String getUserId(HttpServletRequest request) throws PortalAPIException {
144     return EcompSso.validateEcompSso(request);
145   }
146
147   /////////////////////////////////////////////////////////////////////////////
148   // Role interface
149   /////////////////////////////////////////////////////////////////////////////
150
151   public List<EcompRole> getAvailableRoles() throws PortalAPIException {
152     LOG.debug("Get available roles");
153     return UserManager.getRoles();
154   }
155
156   /*
157    * (non-Javadoc)
158    *
159    * @see
160    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
161    */
162   @Override
163   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
164     LOG.debug("Get user roles");
165     return userManager.getUserRoles(loginId);
166   }
167
168   /*
169    * (non-Javadoc)
170    *
171    * @see
172    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
173    * java.util.List)
174    */
175   @Override
176   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
177     LOG.debug("Push user role [loginId:" + loginId + "]");
178     try {
179       EcompUser user = getUser(loginId);
180       if (roles != null) {
181         user.setRoles(new LinkedHashSet<EcompRole>(roles));
182       } else {
183         user.setRoles(new LinkedHashSet<EcompRole>());
184       }
185       editUser(loginId, user);
186     } catch (PortalAPIException e) {
187       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
188       LOG.error(message);
189       throw new PortalAPIException(message, e);
190     }
191   }
192
193   /////////////////////////////////////////////////////////////////////////////
194   // Security interface
195   /////////////////////////////////////////////////////////////////////////////
196
197   /*
198    * (non-Javadoc)
199    *
200    * @see
201    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
202    * http.HttpServletRequest)
203    */
204   @Override
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());
212   }
213
214   private String getMessage(String message, Object... args) {
215     MessageFormat formatter = new MessageFormat("");
216     formatter.applyPattern(message);
217     return formatter.format(args);
218   }
219
220   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
221     LOG.debug("Get available roles");
222     return UserManager.getRoles();
223   }
224
225 }