Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / config / PortalAuthenticationConfig.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.security.portal.config;
26
27
28 import java.util.Properties;
29
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.sparky.logging.AaiUiMsgs;
33 import org.onap.aai.sparky.security.CookieDecryptor;
34 import org.onap.aai.sparky.util.ConfigHelper;
35 import org.onap.aai.sparky.util.Encryptor;
36 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
37
38
39 /**
40  * Provides Portal authentication configuration.
41  */
42 public class PortalAuthenticationConfig {
43
44   private String username;
45   private String password;
46   private boolean isOnapEnabled;
47   private CookieDecryptor cookieDecryptor;
48   private String cookieDecryptorClassName;
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   private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
54   public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
55   private static final Logger LOG = LoggerFactory.getInstance().getLogger(PortalAuthenticationConfig.class);
56
57   private PortalAuthenticationConfig() {
58     // Prevent instantiation
59   }
60
61   private static class PortalAuthenticationConfigHelper {
62     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
63
64     private PortalAuthenticationConfigHelper() {
65       // Deliberately empty
66     }
67   }
68
69   /**
70    * Get a singleton instance of the configuration.
71    *
72    * @return
73    */
74   public static PortalAuthenticationConfig getInstance() {
75     PortalAuthenticationConfigHelper.INSTANCE.load();
76     return PortalAuthenticationConfigHelper.INSTANCE;
77   }
78
79   public String getUsername() {
80     return username;
81   }
82
83   public String getPassword() {
84     Encryptor encryptor = new Encryptor();
85     return encryptor.decryptValue(password);
86   }
87
88   public boolean getIsOnapEnabled() {
89     return isOnapEnabled;
90   }
91   public String getcookieDecryptorClassName() {
92             return cookieDecryptorClassName;
93           }
94
95   /**
96    * Reload the Portal authentication properties from the classpath.
97    */
98   public void reload() {
99     load();
100   }
101
102   /**
103    * Load the Portal authentication properties from the classpath.
104    */
105   private void load() {
106     Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
107     username = props.getProperty(PROP_USERNAME);
108     password = props.getProperty(PROP_PASSWORD);
109     isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
110     cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
111   }
112   
113   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException{
114           
115           Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
116           try {
117                 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
118         } catch (InstantiationException | IllegalAccessException e) {
119                  LOG.error(AaiUiMsgs.DECRYPTION_ERROR,"Unable to instantiate Cookie Decryptor Class");
120         }
121           return cookieDecryptor;
122   }
123   
124 }