Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Proxy Causes failures if other proxy is not used #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions CyberSource/Client/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ public abstract class BaseClient
private const string PROXY_URL = "proxyURL";
private const string PROXY_USER = "proxyUser";
private const string PROXY_PASSWORD = "proxyPassword";
private const string BASIC_AUTH = "Basic";
protected const string BASIC_AUTH = "Basic";

protected static Uri ProxyUri
{
get
{
var proxyURL = AppSettings.GetSetting(null, PROXY_URL);
return !string.IsNullOrEmpty(proxyURL) ? new Uri(proxyURL) : null;
}
}

protected static string ProxyUser
{
get { return AppSettings.GetSetting(null, PROXY_USER); }
}

protected static string ProxyPassword
{
get { return AppSettings.GetSetting(null, PROXY_PASSWORD); }
}

public const string CYBERSOURCE_PUBLIC_KEY = "CyberSource_SJC_US";
public const string X509_CLAIMTYPE = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/x500distinguishedname";
Expand All @@ -53,25 +72,18 @@ private static void SetupProxy()
{
// if a proxyURL is specified in the configuration file,
// create a WebProxy object for later use.
string proxyURL = AppSettings.GetSetting(null, PROXY_URL);
if (proxyURL != null)
if (ProxyUri != null)
{
mProxy = new WebProxy(proxyURL);
mProxy = new WebProxy(ProxyUri);

// if a proxyUser is specified in the configuration file,
// set up the proxy object's Credentials.
string proxyUser
= AppSettings.GetSetting(null, PROXY_USER);
if (proxyUser != null)
if (ProxyUser != null)
{
string proxyPassword
= AppSettings.GetSetting(null, PROXY_PASSWORD);

NetworkCredential credential
= new NetworkCredential(proxyUser, proxyPassword);
NetworkCredential credential = new NetworkCredential(ProxyUser, ProxyPassword);

CredentialCache cache = new CredentialCache();
cache.Add(new Uri(proxyURL), BASIC_AUTH, credential);
cache.Add(ProxyUri, BASIC_AUTH, credential);
mProxy.Credentials = cache;
}
}
Expand Down Expand Up @@ -336,8 +348,7 @@ protected static CustomBinding getWCFCustomBinding(Configuration config)

//Use custom encoder to strip unsigned timestamp in response
CustomTextMessageBindingElement textBindingElement = new CustomTextMessageBindingElement();



//Setup https transport
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
httpsTransport.RequireClientCertificate = true;
Expand All @@ -348,8 +359,12 @@ protected static CustomBinding getWCFCustomBinding(Configuration config)
//Setup Proxy if needed
if (mProxy != null)
{
WebRequest.DefaultWebProxy = mProxy;
httpsTransport.UseDefaultWebProxy = true;
httpsTransport.ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous;
if (ProxyUser != null)
{
httpsTransport.ProxyAuthenticationScheme = AuthenticationSchemes.Basic;
}
httpsTransport.ProxyAddress = mProxy.Address;
}


Expand Down
169 changes: 89 additions & 80 deletions CyberSource/Client/SoapClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using CyberSource.Base;
using CyberSource.Clients.SoapServiceReference;
using CyberSource.Clients.SoapServiceReference;
using System.Xml;


Expand Down Expand Up @@ -58,9 +60,9 @@ public static ReplyMessage RunTransaction(
DetermineEffectiveMerchantID(ref config, requestMessage);
SetVersionInformation(requestMessage);
logger = PrepareLog(config);
SetConnectionLimit(config);
SetConnectionLimit(config);


CustomBinding currentBinding = getWCFCustomBinding(config);


Expand All @@ -69,9 +71,16 @@ public static ReplyMessage RunTransaction(
EndpointAddress endpointAddress = new EndpointAddress( new Uri(config.EffectiveServerURL), EndpointIdentity.CreateDnsIdentity(config.EffectivePassword), headers );

//Get instance of service
using( proc = new TransactionProcessorClient(currentBinding, endpointAddress)){

//Set protection level to sign & encrypt only
using( proc = new TransactionProcessorClient(currentBinding, endpointAddress)) {

// Set proxy Basic auth credentials
if (ProxyUser != null)
{
proc.ClientCredentials.UserName.Password = ProxyPassword;
proc.ClientCredentials.UserName.UserName = ProxyUser;
}

//Set protection level to sign & encrypt only
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;

// set the timeout
Expand All @@ -82,52 +91,52 @@ public static ReplyMessage RunTransaction(
string keyFilePath = Path.Combine(config.KeysDirectory,config.EffectiveKeyFilename);
proc.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(keyFilePath,config.EffectivePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

proc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
// Changes for SHA2 certificates support
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(keyFilePath, config.EffectivePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
foreach (X509Certificate2 cert1 in collection)
{
if (cert1.Subject.Contains(config.MerchantID))
{
proc.ClientCredentials.ClientCertificate.Certificate = cert1;
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert1;
break;
}
}
if (config.UseSignedAndEncrypted)
{
foreach (X509Certificate2 cert2 in collection)
{
//Console.WriteLine(cert1.Subject);
if (cert2.Subject.Contains(CYBERSOURCE_PUBLIC_KEY))
{
//Set protection level to sign & encrypt only
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert2;
break;
}
}
proc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

// Changes for SHA2 certificates support
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(keyFilePath, config.EffectivePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

foreach (X509Certificate2 cert1 in collection)
{
if (cert1.Subject.Contains(config.MerchantID))
{
proc.ClientCredentials.ClientCertificate.Certificate = cert1;
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert1;
break;
}
}

if (config.UseSignedAndEncrypted)
{
foreach (X509Certificate2 cert2 in collection)
{
//Console.WriteLine(cert1.Subject);
if (cert2.Subject.Contains(CYBERSOURCE_PUBLIC_KEY))
{
//Set protection level to sign & encrypt only
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert2;
break;
}
}
}

// send request now
// Changes for NGT-3035
XmlNode req = SerializeObjectToXmlNode(requestMessage);
if (logger != null)
{
logger.LogRequest(req, config.Demo);
}
ReplyMessage reply = proc.runTransaction(requestMessage);
XmlNode rep = SerializeObjectToXmlNode(reply);
if (logger != null)
{
logger.LogReply(rep, config.Demo);
}
// send request now
// Changes for NGT-3035
XmlNode req = SerializeObjectToXmlNode(requestMessage);
if (logger != null)
{
logger.LogRequest(req, config.Demo);
}

ReplyMessage reply = proc.runTransaction(requestMessage);
XmlNode rep = SerializeObjectToXmlNode(reply);
if (logger != null)
{
logger.LogReply(rep, config.Demo);
}

return (reply);
}
}
Expand All @@ -150,35 +159,35 @@ public static ReplyMessage RunTransaction(
proc.Close();
}
}
}
// Changes for NGT-3035
private static XmlNode SerializeObjectToXmlNode(Object obj)
{
if (obj == null)
throw new ArgumentNullException("Argument cannot be null");
XmlNode resultNode = null;
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add("", "");
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
xmlSerializer.Serialize(memoryStream, obj, ns);
}
catch (InvalidOperationException)
{
return null;
}
memoryStream.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(memoryStream);
resultNode = doc.DocumentElement;
}
return resultNode;
}

// Changes for NGT-3035
private static XmlNode SerializeObjectToXmlNode(Object obj)
{
if (obj == null)
throw new ArgumentNullException("Argument cannot be null");

XmlNode resultNode = null;
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add("", "");
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
xmlSerializer.Serialize(memoryStream, obj, ns);
}
catch (InvalidOperationException)
{
return null;
}
memoryStream.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(memoryStream);
resultNode = doc.DocumentElement;
}

return resultNode;
}


Expand Down