fix the cookie decryption logic
[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 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.security.portal.config;
24
25
26 import java.util.Properties;
27
28 import org.onap.aai.sparky.security.CookieDecryptor;
29 import org.onap.aai.sparky.util.ConfigHelper;
30 import org.onap.aai.sparky.util.Encryptor;
31 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
32
33
34 /**
35  * Provides Portal authentication configuration.
36  */
37 public class PortalAuthenticationConfig {
38
39   private String username;
40   private String password;
41   private boolean isOnapEnabled;
42   private CookieDecryptor cookieDecryptor;
43   private String cookieDecryptorClassName;
44
45   public static final String PROP_USERNAME = "username";
46   public static final String PROP_PASSWORD = "password"; // NOSONAR
47   public static final String PROP_IS_ONAP_ENABLED = "onap_enabled"; // NOSONAR
48   private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
49   public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
50
51   private PortalAuthenticationConfig() {
52     // Prevent instantiation
53   }
54
55   private static class PortalAuthenticationConfigHelper {
56     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
57
58     private PortalAuthenticationConfigHelper() {
59       // Deliberately empty
60     }
61   }
62
63   /**
64    * Get a singleton instance of the configuration.
65    *
66    * @return
67    */
68   public static PortalAuthenticationConfig getInstance() {
69     PortalAuthenticationConfigHelper.INSTANCE.load();
70     return PortalAuthenticationConfigHelper.INSTANCE;
71   }
72
73   public String getUsername() {
74     return username;
75   }
76
77   public String getPassword() {
78     Encryptor encryptor = new Encryptor();
79     return encryptor.decryptValue(password);
80   }
81   
82   public boolean getIsOnapEnabled() {
83     return isOnapEnabled;
84   }
85   public String getcookieDecryptorClassName() {
86             return cookieDecryptorClassName;
87           }
88
89   /**
90    * Reload the Portal authentication properties from the classpath.
91    */
92   public void reload() {
93     load();
94   }
95
96   /**
97    * Load the Portal authentication properties from the classpath.
98    */
99   private void load() {
100     Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
101     username = props.getProperty(PROP_USERNAME);
102     password = props.getProperty(PROP_PASSWORD);
103     isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
104     cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
105   }
106   
107   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException{
108           
109           Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
110           try {
111                 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
112         } catch (InstantiationException | IllegalAccessException e) {
113                 e.printStackTrace();
114         }
115           return cookieDecryptor;
116   }
117   
118 }