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