update to use latest portal sdk
[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.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 String userIdCookieName;
44   private CookieDecryptor cookieDecryptor;
45   private String cookieDecryptorClassName;
46
47   public static final String PROP_USERNAME = "username";
48   public static final String PROP_PASSWORD = "password"; // NOSONAR
49   public static final String PROP_IS_ONAP_ENABLED = "onap_enabled"; // NOSONAR
50   public static final String PROP_USERID_COOKIE_NAME = "onap.user_id_cookie_name"; // NOSONAR
51   private static final String AUTHENTICATION_CONFIG_FILE = SparkyConstants.PORTAL_AUTHENTICATION_FILE_LOCATION;
52   public static final String PROP_COOKIEDECRYPTORCLASSNAME = "cookie_decryptor_classname";
53   private static final Logger LOG = LoggerFactory.getInstance().getLogger(PortalAuthenticationConfig.class);
54
55   private PortalAuthenticationConfig() {
56     // Prevent instantiation
57   }
58
59   private static class PortalAuthenticationConfigHelper {
60     private static final PortalAuthenticationConfig INSTANCE = new PortalAuthenticationConfig();
61
62     private PortalAuthenticationConfigHelper() {
63       // Deliberately empty
64     }
65   }
66
67   /**
68    * Get a singleton instance of the configuration.
69    *
70    * @return
71    */
72   public static PortalAuthenticationConfig getInstance() {
73     PortalAuthenticationConfigHelper.INSTANCE.load();
74     return PortalAuthenticationConfigHelper.INSTANCE;
75   }
76
77   public String getUsername() {
78     return username;
79   }
80
81   public String getPassword() {
82     Encryptor encryptor = new Encryptor();
83     return encryptor.decryptValue(password);
84   }
85
86   public boolean getIsOnapEnabled() {
87     return isOnapEnabled;
88   }
89   public String getcookieDecryptorClassName() {
90             return cookieDecryptorClassName;
91           }
92   public String getUserIdCookieName() {
93           return userIdCookieName;
94         }
95         
96   public void setUserIdCookieName(String userIdCookieName) {
97         this.userIdCookieName = userIdCookieName;
98         }
99   /**
100    * Reload the Portal authentication properties from the classpath.
101    */
102   public void reload() {
103     load();
104   }
105
106   /**
107    * Load the Portal authentication properties from the classpath.
108    */
109   private void load() {
110     Properties props = ConfigHelper.loadConfigFromExplicitPath(AUTHENTICATION_CONFIG_FILE);
111     username = props.getProperty(PROP_USERNAME);
112     password = props.getProperty(PROP_PASSWORD);
113     isOnapEnabled = Boolean.parseBoolean(props.getProperty(PROP_IS_ONAP_ENABLED, "true"));
114     userIdCookieName = props.getProperty(PROP_USERID_COOKIE_NAME);
115     cookieDecryptorClassName= props.getProperty(PROP_COOKIEDECRYPTORCLASSNAME);
116   }
117   
118   public CookieDecryptor getCookieDecryptor() throws ClassNotFoundException{
119           
120           Class cookieDecrypterClass = Class.forName(cookieDecryptorClassName);
121           try {
122                 cookieDecryptor = (CookieDecryptor) cookieDecrypterClass.newInstance();
123         } catch (InstantiationException | IllegalAccessException e) {
124                  LOG.error(AaiUiMsgs.DECRYPTION_ERROR,"Unable to instantiate Cookie Decryptor Class");
125         }
126           return cookieDecryptor;
127   }
128   
129 }