Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / security / portal / PortalRestAPIServiceImpl.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.security.portal;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.text.MessageFormat;
31 import java.util.LinkedHashSet;
32 import java.util.List;
33
34 import javax.servlet.http.HttpServletRequest;
35
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;
45
46 /**
47  * Responds to ECOMP Portal's REST queries for user and role information and management.
48  */
49 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
50
51   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
52   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
53
54   private UserManager userManager;
55
56   /**
57    * Initialise user manager.
58    */
59   public PortalRestAPIServiceImpl() {
60     userManager = new UserManager(new File(TierSupportUiConstants.USERS_FILE_LOCATION));
61   }
62
63   /////////////////////////////////////////////////////////////////////////////
64   // User interface
65   /////////////////////////////////////////////////////////////////////////////
66
67   /*
68    * (non-Javadoc)
69    *
70    * @see
71    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
72    * restful.domain.EcompUser)
73    */
74   @Override
75   public void pushUser(EcompUser user) throws PortalAPIException {
76     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
77
78     if (userManager.getUser(user.getLoginId()).isPresent()) {
79       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
80           + ", user is already stored";
81       LOG.error(message);
82       throw new PortalAPIException(message);
83     }
84
85     try {
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);
91     }
92   }
93
94   /*
95    * (non-Javadoc)
96    *
97    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
98    * com.att.fusion.core.restful.domain.EcompUser)
99    */
100   @Override
101   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
102     LOG.debug("Edit user [loginId:" + loginId + "]");
103
104     userManager.getUser(loginId).orElseThrow(() -> {
105       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
106       LOG.error(message);
107       return new PortalAPIException(message);
108     });
109
110     try {
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);
116     }
117   }
118
119   /*
120    * (non-Javadoc)
121    *
122    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
123    */
124   @Override
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";
129       LOG.error(message);
130       return new PortalAPIException(message);
131     });
132   }
133
134   /*
135    * (non-Javadoc)
136    *
137    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
138    */
139   @Override
140   public List<EcompUser> getUsers() throws PortalAPIException {
141     LOG.debug("Get users");
142     return userManager.getUsers();
143   }
144
145   @Override
146   public String getUserId(HttpServletRequest request) throws PortalAPIException {
147     return EcompSso.validateEcompSso(request);
148   }
149
150   /////////////////////////////////////////////////////////////////////////////
151   // Role interface
152   /////////////////////////////////////////////////////////////////////////////
153
154   /*
155    * (non-Javadoc)
156    *
157    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getAvailableRoles()
158    */
159   @Override
160   public List<EcompRole> getAvailableRoles() throws PortalAPIException {
161     LOG.debug("Get available roles");
162     return UserManager.getRoles();
163   }
164
165   /*
166    * (non-Javadoc)
167    *
168    * @see
169    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
170    */
171   @Override
172   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
173     LOG.debug("Get user roles");
174     return userManager.getUserRoles(loginId);
175   }
176
177   /*
178    * (non-Javadoc)
179    *
180    * @see
181    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
182    * java.util.List)
183    */
184   @Override
185   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
186     LOG.debug("Push user role [loginId:" + loginId + "]");
187     try {
188       EcompUser user = getUser(loginId);
189       if (roles != null) {
190         user.setRoles(new LinkedHashSet<EcompRole>(roles));
191       } else {
192         user.setRoles(new LinkedHashSet<EcompRole>());
193       }
194       editUser(loginId, user);
195     } catch (PortalAPIException e) {
196       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
197       LOG.error(message);
198       throw new PortalAPIException(message, e);
199     }
200   }
201
202   /////////////////////////////////////////////////////////////////////////////
203   // Security interface
204   /////////////////////////////////////////////////////////////////////////////
205
206   /*
207    * (non-Javadoc)
208    *
209    * @see
210    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
211    * http.HttpServletRequest)
212    */
213   @Override
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());
221   }
222
223   private String getMessage(String message, Object... args) {
224     MessageFormat formatter = new MessageFormat("");
225     formatter.applyPattern(message);
226     return formatter.format(args);
227   }
228
229 }