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.config;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Properties;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.security.CookieDecryptor;
32 import org.onap.aai.sparky.util.ConfigHelper;
33 import org.onap.aai.sparky.util.Encryptor;
34 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
38 * Provides Portal authentication configuration.
40 public class PortalAuthenticationConfig {
42 private String username;
43 private String password;
44 private boolean isOnapEnabled;
45 private String userIdCookieName;
46 private CookieDecryptor cookieDecryptor;
47 private String cookieDecryptorClassName;
48 private String delimitedAppRoles;
50 public static final String PROP_USERNAME = "username";
51 public static final String PROP_PASSWORD = "password"; // NOSONAR
52 public static final String PROP_IS_ONAP_ENABLED = "onap_enabled"; // NOSONAR
53 public static final String PROP_USERID_COOKIE_NAME = "onap.user_id_cookie_name"; // NOSONAR
54 private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
55 public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
56 public static final String PROP_APP_ROLES = "app_roles";
57 private static final Logger LOG = LoggerFactory.getInstance().getLogger(PortalAuthenticationConfig.class);
59 private PortalAuthenticationConfig() {
60 // Prevent instantiation
63 private static class PortalAuthenticationConfigHelper {
64 private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
66 private PortalAuthenticationConfigHelper() {
72 * Get a singleton instance of the configuration.
76 public static PortalAuthenticationConfig getInstance() {
77 PortalAuthenticationConfigHelper.INSTANCE.load();
78 return PortalAuthenticationConfigHelper.INSTANCE;
81 public String getUsername() {
85 public String getPassword() {
86 Encryptor encryptor = new Encryptor();
87 return encryptor.decryptValue(password);
90 public boolean getIsOnapEnabled() {
93 public String getcookieDecryptorClassName() {
94 return cookieDecryptorClassName;
96 public String getUserIdCookieName() {
97 return userIdCookieName;
100 public void setUserIdCookieName(String userIdCookieName) {
101 this.userIdCookieName = userIdCookieName;
104 * Reload the Portal authentication properties from the classpath.
106 public void reload() {
111 * Load the Portal authentication properties from the classpath.
113 private void load() {
114 Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
115 username = props.getProperty(PROP_USERNAME);
116 password = props.getProperty(PROP_PASSWORD);
117 isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
118 userIdCookieName = props.getProperty(PROP_USERID_COOKIE_NAME);
119 cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
120 delimitedAppRoles = props.getProperty(PROP_APP_ROLES);
123 public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException {
125 Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
127 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
128 } catch (InstantiationException | IllegalAccessException e) {
129 LOG.error(AaiUiMsgs.DECRYPTION_ERROR, "Unable to instantiate Cookie Decryptor Class");
131 return cookieDecryptor;
134 public ArrayList<String> getAppRoles() {
136 ArrayList<String> appRoles = null;
137 if (delimitedAppRoles == null) {
138 return new ArrayList<>();
142 appRoles = new ArrayList<String>(Arrays.asList(delimitedAppRoles.split(",")));
143 } catch (Exception exc) {
144 appRoles = new ArrayList<>();