Skip to content

Commit 40671a0

Browse files
authored
Merge pull request #70 from snavinch/master
+ README Changes
2 parents 1e2e57e + 4d28669 commit 40671a0

File tree

7 files changed

+863
-59
lines changed

7 files changed

+863
-59
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Additionally, you can find details and examples of how our API is structured in
4343

4444
The API Reference Guide provides examples of what information is needed for a particular request and how that information would be formatted. Using those examples, you can easily determine what methods would be necessary to include that information in a request using this SDK.
4545

46+
## MetaKey Support
47+
Meta Key is a key generated by an entity that can be used to authenticate on behalf of other entities provided that the entity which holds key is a parent entity or associated as a partner.
48+
4649

4750
### Switching between the sandbox environment and the production environment
4851
Cybersource maintains a complete sandbox environment for testing and development purposes. This sandbox environment is an exact duplicate of our production environment with the transaction authorization and settlement process simulated. By default, this SDK is configured to communicate with the sandbox environment. To switch to the production environment, set the `runEnvironment` property in the SDK Configuration. See our sample at https://github.com/CyberSource/cybersource-rest-samples-java/blob/master/src/main/java/Data/Configuration.java.

generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212

1313
package 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.*;
1916
import java.lang.reflect.Type;
2017
import java.net.HttpRetryException;
2118
import java.net.InetSocketAddress;
2219
import java.net.Proxy;
2320
import java.net.URLConnection;
2421
import java.net.URLEncoder;
25-
import java.security.GeneralSecurityException;
26-
import java.security.KeyStore;
27-
import java.security.SecureRandom;
22+
import java.security.*;
2823
import java.security.cert.Certificate;
2924
import java.security.cert.CertificateException;
3025
import java.security.cert.CertificateFactory;
@@ -45,13 +40,10 @@ import java.util.TimeZone;
4540
import java.util.concurrent.TimeUnit;
4641
import java.util.regex.Matcher;
4742
import 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.cybersource.authsdk.util.Utility;
45+
import com.google.gson.Gson;
46+
import com.google.gson.reflect.TypeToken;
5547
import org.apache.logging.log4j.Logger;
5648
import com.cybersource.authsdk.core.Authorization;
5749
import com.cybersource.authsdk.core.ConfigException;
@@ -84,6 +76,7 @@ import okhttp3.logging.HttpLoggingInterceptor;
8476
import okhttp3.logging.HttpLoggingInterceptor.Level;
8577
import okio.BufferedSink;
8678
import okio.Okio;
79+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
8780
import utilities.interceptors.RetryInterceptor;
8881
import utilities.listeners.NetworkEventListener;
8982
import utilities.telemetry.RequestTransactionMetrics;
@@ -1173,6 +1166,10 @@ public class ApiClient {
11731166
responseCode = String.valueOf(response.code());
11741167
status = response.message();
11751168
1169+
if(returnType == new TypeToken< Model.AccessTokenResponse >(){}.getType()) {
1170+
Logger logger = Log4j.getInstance(merchantConfig);
1171+
Utility.log(logger, response.peekBody(Long.MAX_VALUE).string(), org.apache.logging.log4j.Level.DEBUG);
1172+
}
11761173
T data = handleResponse(response, returnType);
11771174

11781175
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
@@ -1298,6 +1295,11 @@ public class ApiClient {
12981295
ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
12991296
callAuthenticationHeader(method, path, body, queryParams);
13001297
1298+
if(merchantConfig.getEnableClientCert())
1299+
{
1300+
addClientCertToKeyStore();
1301+
}
1302+
13011303
if (acceptHeader != null && !acceptHeader.isEmpty()) {
13021304
String defaultAcceptHeader = "," + headerParams.get("Accept");
13031305
defaultAcceptHeader = acceptHeader + defaultAcceptHeader.replace("," + acceptHeader, "");
@@ -1359,7 +1361,7 @@ public class ApiClient {
13591361

13601362
merchantConfig.setRequestHost(merchantConfig.getRequestHost().trim());
13611363

1362-
if (isMerchantDetails) {
1364+
if (isMerchantDetails && !merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.MUTUALAUTH)) {
13631365
String token = authorization.getToken(merchantConfig);
13641366
if (merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.HTTP)) {
13651367
@@ -1380,10 +1382,11 @@ public class ApiClient {
13801382
token = "Bearer " + token;
13811383
addDefaultHeader("Authorization", token);
13821384
}
1383-
1384-
// if (merchantConfig.getSolutionId() != null && !merchantConfig.getSolutionId().isEmpty()) {
1385-
// addDefaultHeader("v-c-solution-id", merchantConfig.getSolutionId());
1386-
// }
1385+
else if(merchantConfig.getAuthenticationType().equalsIgnoreCase(GlobalLabelParameters.OAUTH))
1386+
{
1387+
token = "Bearer " + token;
1388+
addDefaultHeader("Authorization", token);
1389+
}
13871390
}
13881391

13891392
if (versionInfo != null && !versionInfo.isEmpty()) {
@@ -1439,6 +1442,10 @@ public class ApiClient {
14391442
if (!HttpMethod.permitsRequestBody(method)) {
14401443
reqBody = null;
14411444
} else if ("application/x-www-form-urlencoded".equals(contentType)) {
1445+
// Convert request body to json to url encoded
1446+
Gson gson = new Gson();
1447+
String jsonString = json.serialize(body);
1448+
formParams = gson.fromJson(jsonString, HashMap.class);
14421449
reqBody = buildRequestBodyFormEncoding(formParams);
14431450
} else if ("multipart/form-data".equals(contentType)) {
14441451
reqBody = buildRequestBodyMultipart(formParams);
@@ -1550,7 +1557,7 @@ public class ApiClient {
15501557
public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) {
15511558
FormBody.Builder formBuilder = new FormBody.Builder();
15521559
for (Entry<String, Object> param : formParams.entrySet()) {
1553-
formBuilder.add(param.getKey(), parameterToString(param.getValue()));
1560+
formBuilder.addEncoded(param.getKey(), parameterToString(param.getValue()));
15541561
}
15551562
return formBuilder.build();
15561563
}
@@ -1626,6 +1633,48 @@ public class ApiClient {
16261633
}
16271634
}
16281635
1636+
/**
1637+
* Adding Client Cert (.p12) to KeyStore, Trust all site
1638+
*/
1639+
private void addClientCertToKeyStore() {
1640+
try {
1641+
1642+
// Create a trust manager that does not validate certificate chains
1643+
final TrustManager[] trustAllCerts = new TrustManager[] {
1644+
new X509TrustManager() {
1645+
@Override
1646+
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
1647+
}
1648+
1649+
@Override
1650+
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
1651+
}
1652+
1653+
@Override
1654+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
1655+
return new java.security.cert.X509Certificate[]{};
1656+
}
1657+
}
1658+
};
1659+
1660+
KeyStore merchantKeyStore = KeyStore.getInstance("PKCS12", new BouncyCastleProvider());
1661+
FileInputStream file = new FileInputStream(new File(merchantConfig.getClientCertDirectory(), merchantConfig.getClientCertFile()));
1662+
merchantKeyStore.load(file, merchantConfig.getClientCertPassword().toCharArray());
1663+
1664+
KeyManagerFactory keyManagerFactory =
1665+
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
1666+
keyManagerFactory.init(merchantKeyStore, new char[] {});
1667+
1668+
SSLContext sslContext = SSLContext.getInstance("TLS");
1669+
sslContext.init(keyManagerFactory.getKeyManagers(), trustAllCerts, new SecureRandom());
1670+
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]).build();
1671+
1672+
}
1673+
catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException | UnrecoverableKeyException ex) {
1674+
1675+
}
1676+
}
1677+
16291678
/**
16301679
* Apply SSL related settings to httpClient according to the current values
16311680
* of verifyingSsl and sslCaCert.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
<dependency>
266266
<groupId>com.cybersource</groupId>
267267
<artifactId>AuthenticationSdk</artifactId>
268-
<version>0.0.13</version>
268+
<version>0.0.14</version>
269269
</dependency>
270270
</dependencies>
271271

0 commit comments

Comments
 (0)