1212
1313package Invokers;
1414
15- import java.io.File;
16- import java.io.IOException;
17- import java.io.InputStream;
18- import java.io.UnsupportedEncodingException;
15+ import java.io.*;
1916import java.lang.reflect.Type;
2017import java.net.HttpRetryException;
2118import java.net.InetSocketAddress;
2219import java.net.Proxy;
2320import java.net.URLConnection;
2421import java.net.URLEncoder;
25- import java.security.GeneralSecurityException;
26- import java.security.KeyStore;
27- import java.security.SecureRandom;
22+ import java.security.*;
2823import java.security.cert.Certificate;
2924import java.security.cert.CertificateException;
3025import java.security.cert.CertificateFactory;
@@ -45,13 +40,8 @@ import java.util.TimeZone;
4540import java.util.concurrent.TimeUnit;
4641import java.util.regex.Matcher;
4742import java.util.regex.Pattern;
48- import javax.net.ssl.HostnameVerifier;
49- import javax.net.ssl.KeyManager;
50- import javax.net.ssl.SSLContext;
51- import javax.net.ssl.SSLSession;
52- import javax.net.ssl.TrustManager;
53- import javax.net.ssl.TrustManagerFactory;
54- import javax.net.ssl.X509TrustManager;
43+ import javax.net.ssl.*;
44+ import com.google.gson.Gson;
5545import org.apache.logging.log4j.Logger;
5646import com.cybersource.authsdk.core.Authorization;
5747import com.cybersource.authsdk.core.ConfigException;
@@ -84,6 +74,7 @@ import okhttp3.logging.HttpLoggingInterceptor;
8474import okhttp3.logging.HttpLoggingInterceptor.Level;
8575import okio.BufferedSink;
8676import okio.Okio;
77+ import org.bouncycastle.jce.provider.BouncyCastleProvider;
8778import utilities.interceptors.RetryInterceptor;
8879import utilities.listeners.NetworkEventListener;
8980import utilities.telemetry.RequestTransactionMetrics;
@@ -1298,6 +1289,11 @@ public class ApiClient {
12981289 ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
12991290 callAuthenticationHeader(method, path, body, queryParams);
13001291
1292+ if (merchantConfig.getEnableClientCert())
1293+ {
1294+ addClientCertToKeyStore();
1295+ }
1296+
13011297 if (acceptHeader != null && !acceptHeader.isEmpty()) {
13021298 String defaultAcceptHeader = " ," + headerParams.get(" Accept" );
13031299 defaultAcceptHeader = acceptHeader + defaultAcceptHeader.replace(" ," + acceptHeader, " " );
@@ -1359,7 +1355,7 @@ public class ApiClient {
13591355
13601356 merchantConfig.setRequestHost(merchantConfig.getRequestHost().trim());
13611357
1362- if (isMerchantDetails) {
1358+ if (isMerchantDetails && !merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.NOAUTH) ) {
13631359 String token = authorization.getToken(merchantConfig);
13641360 if (merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.HTTP)) {
13651361
@@ -1380,10 +1376,11 @@ public class ApiClient {
13801376 token = " Bearer " + token;
13811377 addDefaultHeader(" Authorization" , token);
13821378 }
1383-
1384- // if (merchantConfig.getSolutionId() != null && !merchantConfig.getSolutionId().isEmpty()) {
1385- // addDefaultHeader(" v-c-solution-id" , merchantConfig.getSolutionId());
1386- // }
1379+ else if(merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.OAUTH))
1380+ {
1381+ token = " Bearer " + token;
1382+ addDefaultHeader(" Authorization" , token);
1383+ }
13871384 }
13881385
13891386 if (versionInfo != null && !versionInfo.isEmpty()) {
@@ -1439,6 +1436,11 @@ public class ApiClient {
14391436 if (!HttpMethod.permitsRequestBody(method)) {
14401437 reqBody = null;
14411438 } else if ("application/x-www-form-urlencoded".equals(contentType)) {
1439+ // Convert request body to json to url encoded
1440+ Gson gson = new Gson();
1441+ String jsonString = json.serialize(body);
1442+ formParams = gson.fromJson(jsonString, HashMap.class);
1443+ while (formParams.values().remove(null));
14421444 reqBody = buildRequestBodyFormEncoding(formParams);
14431445 } else if ("multipart/form-data".equals(contentType)) {
14441446 reqBody = buildRequestBodyMultipart(formParams);
@@ -1550,7 +1552,7 @@ public class ApiClient {
15501552 public RequestBody buildRequestBodyFormEncoding(Map<String , Object > formParams) {
15511553 FormBody.Builder formBuilder = new FormBody.Builder();
15521554 for (Entry< String, Object> param : formParams.entrySet()) {
1553- formBuilder.add (param.getKey(), parameterToString(param.getValue()));
1555+ formBuilder.addEncoded (param.getKey(), parameterToString(param.getValue()));
15541556 }
15551557 return formBuilder.build();
15561558 }
@@ -1626,6 +1628,48 @@ public class ApiClient {
16261628 }
16271629 }
16281630
1631+ /**
1632+ * Adding Client Cert (.p12) to KeyStore, Trust all site
1633+ */
1634+ private void addClientCertToKeyStore() {
1635+ try {
1636+
1637+ // Create a trust manager that does not validate certificate chains
1638+ final TrustManager[] trustAllCerts = new TrustManager[] {
1639+ new X509TrustManager() {
1640+ @Override
1641+ public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
1642+ }
1643+
1644+ @Override
1645+ public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
1646+ }
1647+
1648+ @Override
1649+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
1650+ return new java.security.cert.X509Certificate[]{};
1651+ }
1652+ }
1653+ };
1654+
1655+ KeyStore merchantKeyStore = KeyStore.getInstance("PKCS12", new BouncyCastleProvider());
1656+ FileInputStream file = new FileInputStream(new File(merchantConfig.getClientCertDirectory(), merchantConfig.getClientCertFile()));
1657+ merchantKeyStore.load(file, merchantConfig.getClientCertPassword().toCharArray());
1658+
1659+ KeyManagerFactory keyManagerFactory =
1660+ KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
1661+ keyManagerFactory.init(merchantKeyStore, new char[] {});
1662+
1663+ SSLContext sslContext = SSLContext.getInstance("TLS");
1664+ sslContext.init(keyManagerFactory.getKeyManagers(), trustAllCerts, new SecureRandom());
1665+ httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]).build();
1666+
1667+ }
1668+ catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException | UnrecoverableKeyException ex) {
1669+
1670+ }
1671+ }
1672+
16291673 /**
16301674 * Apply SSL related settings to httpClient according to the current values
16311675 * of verifyingSsl and sslCaCert.
0 commit comments