2  * ============LICENSE_START=======================================================
 
   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
 
  12  *       http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.aai.sparky.security.portal;
 
  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;
 
  31 import javax.servlet.http.HttpServletRequest;
 
  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;
 
  44  * Responds to ECOMP Portal's REST queries for user and role information and management.
 
  46 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
 
  48   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
 
  49   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
 
  51   private UserManager userManager;
 
  54    * Initialise user manager.
 
  56   public PortalRestAPIServiceImpl() {
 
  57     userManager = new UserManager(new File(SparkyConstants.USERS_FILE_LOCATION));
 
  60   /////////////////////////////////////////////////////////////////////////////
 
  62   /////////////////////////////////////////////////////////////////////////////
 
  69   public void pushUser(EcompUser user) throws PortalAPIException {
 
  70     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
 
  72     if (userManager.getUser(user.getLoginId()).isPresent()) {
 
  73       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
 
  74           + ", user is already stored";
 
  76       throw new PortalAPIException(message);
 
  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);
 
  93   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
 
  94     LOG.debug("Edit user [loginId:" + loginId + "]");
 
  96     userManager.getUser(loginId).orElseThrow(() -> {
 
  97       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
 
  99       return new PortalAPIException(message);
 
 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);
 
 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";
 
 121       return new PortalAPIException(message);
 
 130   public List<EcompUser> getUsers() throws PortalAPIException {
 
 131     LOG.debug("Get users");
 
 132     return userManager.getUsers();
 
 136   public String getUserId(HttpServletRequest request) throws PortalAPIException {
 
 137     return EcompSso.validateEcompSso(request);
 
 140   /////////////////////////////////////////////////////////////////////////////
 
 142   /////////////////////////////////////////////////////////////////////////////
 
 144   public List<EcompRole> getAvailableRoles() throws PortalAPIException {
 
 145     LOG.debug("Get available roles");
 
 146     return UserManager.getRoles();
 
 155   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
 
 156     LOG.debug("Get user roles");
 
 157     return userManager.getUserRoles(loginId);
 
 165   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
 
 166     LOG.debug("Push user role [loginId:" + loginId + "]");
 
 168       EcompUser user = getUser(loginId);
 
 170         user.setRoles(new LinkedHashSet<EcompRole>(roles));
 
 172         user.setRoles(new LinkedHashSet<EcompRole>());
 
 174       editUser(loginId, user);
 
 175     } catch (PortalAPIException e) {
 
 176       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
 
 178       throw new PortalAPIException(message, e);
 
 182   /////////////////////////////////////////////////////////////////////////////
 
 183   // Security interface
 
 184   /////////////////////////////////////////////////////////////////////////////
 
 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());
 
 200   private String getMessage(String message, Object... args) {
 
 201     MessageFormat formatter = new MessageFormat("");
 
 202     formatter.applyPattern(message);
 
 203     return formatter.format(args);
 
 206   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
 
 207     LOG.debug("Get available roles");
 
 208     return UserManager.getRoles();
 
 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();
 
 217     credentialsMap.put("username", appUserName);
 
 218     credentialsMap.put("password", appPassword);
 
 219     return credentialsMap;