/** * ============LICENSE_START=================================================== * SPARKY (AAI UI service) * ============================================================================ * Copyright © 2017 AT&T Intellectual Property. * Copyright © 2017 Amdocs * All rights reserved. * ============================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END===================================================== * * ECOMP and OpenECOMP are trademarks * and service marks of AT&T Intellectual Property. */ package org.openecomp.sparky.security.portal; import java.io.File; import java.io.IOException; import java.text.MessageFormat; import java.util.LinkedHashSet; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService; import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException; import org.openecomp.portalsdk.core.restful.domain.EcompRole; import org.openecomp.portalsdk.core.restful.domain.EcompUser; import org.openecomp.sparky.security.EcompSso; import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig; import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Responds to ECOMP Portal's REST queries for user and role information and management. */ public class PortalRestAPIServiceImpl implements IPortalRestAPIService { private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class); private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]"; private UserManager userManager; /** * Initialise user manager. */ public PortalRestAPIServiceImpl() { userManager = new UserManager(new File(TierSupportUiConstants.USERS_FILE_LOCATION)); } ///////////////////////////////////////////////////////////////////////////// // User interface ///////////////////////////////////////////////////////////////////////////// /* * (non-Javadoc) * * @see * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core. * restful.domain.EcompUser) */ @Override public void pushUser(EcompUser user) throws PortalAPIException { LOG.debug("Push user [loginId:" + user.getLoginId() + "]"); if (userManager.getUser(user.getLoginId()).isPresent()) { String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId()) + ", user is already stored"; LOG.error(message); throw new PortalAPIException(message); } try { userManager.pushUser(user); } catch (IOException e) { String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId()); LOG.error(message, e); throw new PortalAPIException(message, e); } } /* * (non-Javadoc) * * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String, * com.att.fusion.core.restful.domain.EcompUser) */ @Override public void editUser(String loginId, EcompUser user) throws PortalAPIException { LOG.debug("Edit user [loginId:" + loginId + "]"); userManager.getUser(loginId).orElseThrow(() -> { String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user"; LOG.error(message); return new PortalAPIException(message); }); try { userManager.editUser(loginId, user); } catch (IOException e) { String message = getMessage(ERROR_MESSAGE, "edit", loginId); LOG.error(message, e); throw new PortalAPIException(message, e); } } /* * (non-Javadoc) * * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String) */ @Override public EcompUser getUser(String loginId) throws PortalAPIException { LOG.debug("Get user [loginId:" + loginId + "]"); return userManager.getUser(loginId).orElseThrow(() -> { String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user"; LOG.error(message); return new PortalAPIException(message); }); } /* * (non-Javadoc) * * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers() */ @Override public List getUsers() throws PortalAPIException { LOG.debug("Get users"); return userManager.getUsers(); } @Override public String getUserId(HttpServletRequest request) throws PortalAPIException { return EcompSso.validateEcompSso(request); } ///////////////////////////////////////////////////////////////////////////// // Role interface ///////////////////////////////////////////////////////////////////////////// /* * (non-Javadoc) * * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getAvailableRoles() */ @Override public List getAvailableRoles() throws PortalAPIException { LOG.debug("Get available roles"); return UserManager.getRoles(); } /* * (non-Javadoc) * * @see * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String) */ @Override public List getUserRoles(String loginId) throws PortalAPIException { LOG.debug("Get user roles"); return userManager.getUserRoles(loginId); } /* * (non-Javadoc) * * @see * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String, * java.util.List) */ @Override public void pushUserRole(String loginId, List roles) throws PortalAPIException { LOG.debug("Push user role [loginId:" + loginId + "]"); try { EcompUser user = getUser(loginId); if (roles != null) { user.setRoles(new LinkedHashSet(roles)); } else { user.setRoles(new LinkedHashSet()); } editUser(loginId, user); } catch (PortalAPIException e) { String message = getMessage(ERROR_MESSAGE, "push role", loginId); LOG.error(message); throw new PortalAPIException(message, e); } } ///////////////////////////////////////////////////////////////////////////// // Security interface ///////////////////////////////////////////////////////////////////////////// /* * (non-Javadoc) * * @see * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet. * http.HttpServletRequest) */ @Override public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException { LOG.debug("Authentication request"); PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance(); String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME); String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD); return restUsername != null && restPassword != null && restUsername.equals(config.getUsername()) && restPassword.equals(config.getPassword()); } private String getMessage(String message, Object... args) { MessageFormat formatter = new MessageFormat(""); formatter.applyPattern(message); return formatter.format(args); } }