fix backdoor issue when using portal
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / config / PortalAuthenticationConfig.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.config;
22
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Properties;
27
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;
35
36
37 /**
38  * Provides Portal authentication configuration.
39  */
40 public class PortalAuthenticationConfig {
41
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;
49
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);
58
59   private PortalAuthenticationConfig() {
60     // Prevent instantiation
61   }
62
63   private static class PortalAuthenticationConfigHelper {
64     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
65
66     private PortalAuthenticationConfigHelper() {
67       // Deliberately empty
68     }
69   }
70
71   /**
72    * Get a singleton instance of the configuration.
73    *
74    * @return
75    */
76   public static PortalAuthenticationConfig getInstance() {
77     PortalAuthenticationConfigHelper.INSTANCE.load();
78     return PortalAuthenticationConfigHelper.INSTANCE;
79   }
80
81   public String getUsername() {
82     return username;
83   }
84
85   public String getPassword() {
86     Encryptor encryptor = new Encryptor();
87     return encryptor.decryptValue(password);
88   }
89
90   public boolean getIsOnapEnabled() {
91     return isOnapEnabled;
92   }
93   public String getcookieDecryptorClassName() {
94             return cookieDecryptorClassName;
95           }
96   public String getUserIdCookieName() {
97           return userIdCookieName;
98         }
99         
100   public void setUserIdCookieName(String userIdCookieName) {
101         this.userIdCookieName = userIdCookieName;
102         }
103   /**
104    * Reload the Portal authentication properties from the classpath.
105    */
106   public void reload() {
107     load();
108   }
109
110   /**
111    * Load the Portal authentication properties from the classpath.
112    */
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);
121   }
122
123   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException {
124
125     Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
126     try {
127       cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
128     } catch (InstantiationException | IllegalAccessException e) {
129       LOG.error(AaiUiMsgs.DECRYPTION_ERROR, "Unable to instantiate Cookie Decryptor Class");
130     }
131     return cookieDecryptor;
132   }
133
134   public ArrayList<String> getAppRoles() {
135
136     ArrayList<String> appRoles = null;
137     if (delimitedAppRoles == null) {
138       return new ArrayList<>();
139     }
140
141     try {
142       appRoles = new ArrayList<String>(Arrays.asList(delimitedAppRoles.split(",")));
143     } catch (Exception exc) {
144       appRoles = new ArrayList<>();
145     }
146     return appRoles;
147   }
148
149 }