fixes for supporting non-gui application access provisioning
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / service / sessionmgt / SessionCommunication.java
index e0396e3..441e99a 100644 (file)
@@ -41,23 +41,32 @@ import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.UUID;
 
 import javax.servlet.http.HttpServletResponse;
 
+import org.onap.portalapp.portal.domain.EPApp;
 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
+import org.onap.portalapp.portal.service.AppsCacheService;
 import org.onap.portalapp.portal.transport.OnboardingApp;
 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 import org.onap.portalapp.portal.utils.EcompPortalUtils;
 import org.onap.portalsdk.core.exception.UrlAccessRestrictedException;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
+import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
+import org.onap.portalsdk.core.onboarding.util.CipherUtil;
+import org.onap.portalsdk.core.util.SystemProperties;
 import org.slf4j.MDC;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
+import org.apache.commons.lang.StringUtils;
 
 import com.att.eelf.configuration.Configuration;
 
@@ -66,18 +75,22 @@ import com.att.eelf.configuration.Configuration;
 @EnableAspectJAutoProxy
 public class SessionCommunication {
        EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SessionCommunication.class);
+       @Autowired
+       private AppsCacheService appsCacheService;
+       
+       private static final String BASIC_AUTHENTICATION_HEADER = "Authorization";
 
        @EPAuditLog
        public String sendGet(OnboardingApp app) throws Exception {
                String appResponse = "";
                String appName = "";
                int responseCode = 0;
-               if (app != null && app.name != null && app.name != "") {
+               if (app != null && app.getAppName() != null && app.getAppName() != "") {
                        try {
-                               appName = app.name;
-                               String url = app.restUrl + "/sessionTimeOuts";
-                               String encriptedPwdDB = app.appPassword;
-                               String appUserName = app.username;
+                               appName = app.getAppName();
+                               String url = app.getRestUrl() + "/sessionTimeOuts";
+                               String encriptedPwdDB = app.getAppBasicAuthPassword();
+                               String appUserName = app.getAppBasicAuthUsername();
 
                                setLocalMDCContext(app, "/sessionTimeOuts", url);
 
@@ -90,9 +103,18 @@ public class SessionCommunication {
                                con.setConnectTimeout(3000);
                                con.setReadTimeout(8000);
                                // add request header
+                               Map<String,String> headers = getHeaders(app);
+                               appUserName =headers.get("username");
+                               encriptedPwdDB = headers.get("password");
+                               
                                con.setRequestProperty("username", appUserName);
                                con.setRequestProperty("password", encriptedPwdDB);
 
+                               
+                               String encoding = Base64.getEncoder().encodeToString((appUserName + ":" + encriptedPwdDB).getBytes());
+                               String encodingStr = "Basic " + encoding;
+                               con.setRequestProperty(BASIC_AUTHENTICATION_HEADER, encodingStr);
+
                                // con.set
                                responseCode = con.getResponseCode();
                                logger.debug(EELFLoggerDelegate.debugLogger, "Response Code : " + responseCode);
@@ -135,12 +157,12 @@ public class SessionCommunication {
                try {
                        if (app == null)
                                throw new Exception("SessionCommunication.pingSession: app is null");
-                       if (app != null && app.name != null && app.name != "") {
-                               appName = app.name;
+                       if (app != null && app.getAppName() != null && app.getAppName() != "") {
+                               appName = app.getAppName();
                        }
-                       String url = app.restUrl + "/updateSessionTimeOuts";
-                       String encriptedPwdDB = app.appPassword;
-                       String appUserName = app.username;
+                       String url = app.getRestUrl() + "/updateSessionTimeOuts";
+                       String encriptedPwdDB = app.getAppBasicAuthPassword();
+                       String appUserName = app.getAppBasicAuthUsername();
 
                        setLocalMDCContext(app, "/updateSessionTimeOuts", url);
 
@@ -153,9 +175,16 @@ public class SessionCommunication {
                        con.setConnectTimeout(3000);
                        con.setReadTimeout(15000);
 
-                       // add request header
+                       Map<String,String> headers = getHeaders(app);
+                       appUserName =headers.get("username");
+                       encriptedPwdDB = headers.get("password");
+                       
                        con.setRequestProperty("username", appUserName);
                        con.setRequestProperty("password", encriptedPwdDB);
+                       
+                       String encoding = Base64.getEncoder().encodeToString((appUserName + ":" + encriptedPwdDB).getBytes());
+                       String encodingStr = "Basic " + encoding;
+                       con.setRequestProperty(BASIC_AUTHENTICATION_HEADER, encodingStr);
 
                        con.setRequestProperty("sessionMap", sessionTimeoutMap);
                        con.setDoInput(true);
@@ -189,13 +218,13 @@ public class SessionCommunication {
        public Boolean timeoutSession(OnboardingApp app, String portalJSessionId) throws Exception {
                String appName = "Unknwon";
                int responseCode = 0;
-               if (app != null && app.name != null && app.name != "") {
+               if (app != null && app.getAppName() != null && app.getAppName() != "") {
                        try {
-                               appName = app.name;
-                               String url = app.restUrl + "/timeoutSession" + "?portalJSessionId=" + portalJSessionId;
+                               appName = app.getAppName();
+                               String url = app.getRestUrl() + "/timeoutSession" + "?portalJSessionId=" + portalJSessionId;
 
-                               String encriptedPwdDB = app.appPassword;
-                               String appUserName = app.username;
+                               String encriptedPwdDB = app.getAppBasicAuthPassword();
+                               String appUserName = app.getAppBasicAuthUsername();
                                // String decreptedPwd = CipherUtil.decrypt(encriptedPwdDB,
                                // SystemProperties.getProperty(SystemProperties.Decryption_Key));
 
@@ -209,11 +238,17 @@ public class SessionCommunication {
                                con.setConnectTimeout(3000);
                                con.setReadTimeout(15000);
 
-                               // add request header
+                               Map<String,String> headers = getHeaders(app);
+                               appUserName =headers.get("username");
+                               encriptedPwdDB = headers.get("password");
+                               
                                con.setRequestProperty("username", appUserName);
                                con.setRequestProperty("password", encriptedPwdDB);
-
-                               // con.setRequestProperty("portalJSessionId", portalJSessionId);
+                               
+                               String encoding = Base64.getEncoder().encodeToString((appUserName + ":" + encriptedPwdDB).getBytes());
+                               String encodingStr = "Basic " + encoding;
+                               con.setRequestProperty(BASIC_AUTHENTICATION_HEADER, encodingStr);
+                               
                                con.setDoInput(true);
                                con.setDoOutput(true);
                                con.getOutputStream().flush();
@@ -252,7 +287,7 @@ public class SessionCommunication {
                        MDC.put(EPCommonSystemProperties.PROTOCOL, EPCommonSystemProperties.HTTPS);
                }
                MDC.put(EPCommonSystemProperties.FULL_URL, url);
-               MDC.put(EPCommonSystemProperties.TARGET_ENTITY, app.myLoginsAppName);
+               MDC.put(EPCommonSystemProperties.TARGET_ENTITY, app.getMyLoginsAppName());
                MDC.put(EPCommonSystemProperties.TARGET_SERVICE_NAME, restPath);
        }
 
@@ -287,4 +322,43 @@ public class SessionCommunication {
                        MDC.remove(EPCommonSystemProperties.PARTNER_NAME);
                }
        }
-}
+       
+       public Map<String,String> getHeaders(OnboardingApp app)
+       {
+               String encriptedPwdDB = "";
+               String appUserName = "";
+
+               
+                Map<String,String> headersMap = new HashMap<>();
+               EPApp externalApp = null;
+
+               if(app.getAppBasicAuthPassword().isEmpty() || app.getAppBasicAuthPassword()==null){
+                       logger.debug(EELFLoggerDelegate.debugLogger, "Entering in the externalApp get app password contains null : {}");
+                       externalApp = appsCacheService.getApp(1L);
+                       logger.debug(EELFLoggerDelegate.debugLogger, "external App Information : {}",externalApp);
+
+                       String mechidUsername=externalApp.getAppBasicAuthUsername();
+                       logger.debug(EELFLoggerDelegate.debugLogger, "external App mechidUsername Information : {}",mechidUsername);
+
+                       String password=externalApp.getAppBasicAuthPassword();
+                       String decreptedexternalAppPwd = StringUtils.EMPTY;
+                       try {
+                               decreptedexternalAppPwd = CipherUtil.decryptPKC(password,
+                                               SystemProperties.getProperty(SystemProperties.Decryption_Key));
+                       } catch (CipherUtilException e) {
+                               logger.error(EELFLoggerDelegate.errorLogger, "failed to decreptedexternalAppPwd when external app pwd is null", e);
+                       }
+                       
+                       appUserName =mechidUsername;
+                       encriptedPwdDB = decreptedexternalAppPwd;
+               
+               }else{
+                       appUserName = app.getAppBasicAuthUsername();
+                       encriptedPwdDB = app.getAppBasicAuthPassword();
+               }
+               
+               headersMap.put("username", appUserName);
+               headersMap.put("password", encriptedPwdDB);
+               return headersMap;
+       }
+}
\ No newline at end of file