Adding interfaces in documentation
[aai/sparky-be.git] / 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.Properties;
25
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.sparky.logging.AaiUiMsgs;
29 import org.onap.aai.sparky.security.CookieDecryptor;
30 import org.onap.aai.sparky.util.ConfigHelper;
31 import org.onap.aai.sparky.util.Encryptor;
32 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
33
34
35 /**
36  * Provides Portal authentication configuration.
37  */
38 public class PortalAuthenticationConfig {
39
40   private String username;
41   private String password;
42   private boolean isOnapEnabled;
43   private CookieDecryptor cookieDecryptor;
44   private String cookieDecryptorClassName;
45
46   public static final String PROP_USERNAME = "username";
47   public static final String PROP_PASSWORD = "password"; // NOSONAR
48   public static final String PROP_IS_ONAP_ENABLED = "onap_enabled"; // NOSONAR
49   private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
50   public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
51   private static final Logger LOG = LoggerFactory.getInstance().getLogger(PortalAuthenticationConfig.class);
52
53   private PortalAuthenticationConfig() {
54     // Prevent instantiation
55   }
56
57   private static class PortalAuthenticationConfigHelper {
58     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
59
60     private PortalAuthenticationConfigHelper() {
61       // Deliberately empty
62     }
63   }
64
65   /**
66    * Get a singleton instance of the configuration.
67    *
68    * @return
69    */
70   public static PortalAuthenticationConfig getInstance() {
71     PortalAuthenticationConfigHelper.INSTANCE.load();
72     return PortalAuthenticationConfigHelper.INSTANCE;
73   }
74
75   public String getUsername() {
76     return username;
77   }
78
79   public String getPassword() {
80     Encryptor encryptor = new Encryptor();
81     return encryptor.decryptValue(password);
82   }
83
84   public boolean getIsOnapEnabled() {
85     return isOnapEnabled;
86   }
87   public String getcookieDecryptorClassName() {
88             return cookieDecryptorClassName;
89           }
90
91   /**
92    * Reload the Portal authentication properties from the classpath.
93    */
94   public void reload() {
95     load();
96   }
97
98   /**
99    * Load the Portal authentication properties from the classpath.
100    */
101   private void load() {
102     Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
103     username = props.getProperty(PROP_USERNAME);
104     password = props.getProperty(PROP_PASSWORD);
105     isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
106     cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
107   }
108   
109   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException{
110           
111           Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
112           try {
113                 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
114         } catch (InstantiationException | IllegalAccessException e) {
115                  LOG.error(AaiUiMsgs.DECRYPTION_ERROR,"Unable to instantiate Cookie Decryptor Class");
116         }
117           return cookieDecryptor;
118   }
119   
120 }