11using CyberSource . Base ;
22using System ;
3+ using System . Collections ;
34using System . Net ;
45using System . ServiceModel ;
56using System . Xml . Serialization ;
67using System . ServiceModel . Channels ;
78using System . ServiceModel . Security . Tokens ;
8-
9+ using System . Security . Cryptography . X509Certificates ;
10+ using System . Collections . Concurrent ;
11+
912namespace CyberSource . Clients
1013{
1114 /// <summary>
@@ -16,7 +19,8 @@ public abstract class BaseClient
1619 /// <summary>
1720 /// Version of this client.
1821 /// </summary>
19- public const string CLIENT_LIBRARY_VERSION = "1.4.2" ;
22+ public const string CLIENT_LIBRARY_VERSION = "1.4.3" ;
23+ public const string CYBS_SUBJECT_NAME = "CyberSource_SJC_US" ;
2024
2125 /// <summary>
2226 /// Proxy object that is initialized during start-up, if needed.
@@ -43,12 +47,13 @@ public abstract class BaseClient
4347
4448 public const string CYBERSOURCE_PUBLIC_KEY = "CyberSource_SJC_US" ;
4549 public const string X509_CLAIMTYPE = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/x500distinguishedname" ;
50+ protected static ConcurrentDictionary < string , CertificateEntry > merchantIdentities = new ConcurrentDictionary < string , CertificateEntry > ( ) ;
4651
47- static BaseClient ( )
48- {
49- ServicePointManager . SecurityProtocol = ( SecurityProtocolType ) 3072 | ( SecurityProtocolType ) 768 ;
50- SetupProxy ( ) ;
51- }
52+ static BaseClient ( )
53+ {
54+ ServicePointManager . SecurityProtocol = ( SecurityProtocolType ) 3072 | ( SecurityProtocolType ) 768 ;
55+ SetupProxy ( ) ;
56+ }
5257
5358 private static void SetupProxy ( )
5459 {
@@ -126,14 +131,14 @@ protected static Configuration BuildConfigurationForRequest(
126131 /// </param>
127132 /// <returns>the built Configuration object</returns>
128133 private static Configuration InternalBuildConfiguration (
129- string merchantID , bool failIfNoMerchantID )
134+ string merchantID , bool failIfNoMerchantID )
130135 {
131- Configuration config = new Configuration ( ) ;
132-
136+ Configuration config = new Configuration ( ) ;
137+
133138 if ( merchantID == null )
134139 {
135140 merchantID
136- = AppSettings . GetSetting ( null , MERCHANT_ID ) ;
141+ = AppSettings . GetSetting ( null , MERCHANT_ID ) ;
137142 }
138143 if ( merchantID != null || failIfNoMerchantID )
139144 {
@@ -168,7 +173,7 @@ int boolVal
168173 config . setLogProperties (
169174 boolVal == 1 ,
170175 AppSettings . GetSetting (
171- merchantID , Configuration . LOG_DIRECTORY ) ) ;
176+ merchantID , Configuration . LOG_DIRECTORY ) ) ;
172177
173178 config . ServerURL
174179 = AppSettings . GetSetting (
@@ -222,6 +227,12 @@ int boolVal
222227 merchantID , Configuration . USE_SIGNED_AND_ENCRYPTED ) ;
223228 if ( boolVal != - 1 ) config . UseSignedAndEncrypted = ( boolVal == 1 ) ;
224229
230+ // certificate cache flag
231+ boolVal
232+ = AppSettings . GetBoolSetting (
233+ merchantID , Configuration . CERTIFICATE_CACHE_ENABLED ) ;
234+ if ( boolVal != - 1 ) config . CertificateCacheEnabled = ( boolVal == 1 ) ;
235+
225236 return ( config ) ;
226237 }
227238
@@ -285,7 +296,7 @@ protected static string GetXmlElementAttributeNamespace(Type type)
285296 {
286297 if ( logger != null )
287298 {
288- logger . Log ( Logger . LogType . CONFIG , "Failed to get Namespace from Service Reference. This should not prevent the client from working: Type=" + type . FullName ) ;
299+ logger . Log ( Logger . LogType . CONFIG , "Failed to get Namespace from Service Reference. This should not prevent the client from working: Type=" + type . FullName ) ;
289300 }
290301 return "" ;
291302 }
@@ -359,6 +370,54 @@ protected static CustomBinding getWCFCustomBinding(Configuration config)
359370 currentBinding . Elements . Add ( textBindingElement ) ;
360371 currentBinding . Elements . Add ( httpsTransport ) ;
361372 return currentBinding ;
373+ }
374+
375+
376+ /// <summary>
377+ ///
378+ /// </summary>
379+ /// <param name="merchantId"></param>
380+ /// <param name="merchantIdentities"></param>
381+ /// <returns></returns>
382+ protected static X509Certificate2 GetOrFindValidMerchantCertFromStore ( string merchantId , ConcurrentDictionary < string , CertificateEntry > merchantIdentities )
383+ {
384+ return merchantIdentities [ merchantId ] != null ? merchantIdentities [ merchantId ] . MerchantCert : null ;
385+ }
386+
387+ /// <summary>
388+ ///
389+ /// </summary>
390+ /// <param name="merchantId"></param>
391+ /// <param name="merchantIdentities"></param>
392+ /// <returns></returns>
393+ protected static X509Certificate2 GetOrFindValidCybsCertFromStore ( string merchantId , ConcurrentDictionary < string , CertificateEntry > merchantIdentities )
394+ {
395+ return merchantIdentities [ merchantId ] != null ? merchantIdentities [ merchantId ] . CybsCert : null ;
396+ }
397+
398+ /// <summary>
399+ ///
400+ /// </summary>
401+ /// <param name="merchantIdentities"></param>
402+ /// <param name="logger"></param>
403+ /// <param name="merchantId"></param>
404+ /// <param name="creationTime"></param>
405+ /// <returns></returns>
406+ public static bool IsMerchantCertExpired ( Logger logger , string merchantId , DateTime modifiedTime , ConcurrentDictionary < string , CertificateEntry > merchantIdentities )
407+ {
408+ if ( merchantIdentities [ merchantId ] != null )
409+ {
410+ if ( merchantIdentities [ merchantId ] . ModifiedTime != modifiedTime )
411+ {
412+ if ( logger != null )
413+ {
414+ logger . LogInfo ( "certificate is expired, will be loaded again in memory for merchantID: " + merchantId ) ;
415+ }
416+ return true ;
417+ }
418+
419+ }
420+ return false ;
362421 }
363422 }
364423}
0 commit comments