1
1
using CyberSource . Base ;
2
2
using System ;
3
+ using System . Collections ;
3
4
using System . Net ;
4
5
using System . ServiceModel ;
5
6
using System . Xml . Serialization ;
6
7
using System . ServiceModel . Channels ;
7
8
using System . ServiceModel . Security . Tokens ;
8
-
9
+ using System . Security . Cryptography . X509Certificates ;
10
+ using System . Collections . Concurrent ;
11
+
9
12
namespace CyberSource . Clients
10
13
{
11
14
/// <summary>
@@ -16,7 +19,8 @@ public abstract class BaseClient
16
19
/// <summary>
17
20
/// Version of this client.
18
21
/// </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" ;
20
24
21
25
/// <summary>
22
26
/// Proxy object that is initialized during start-up, if needed.
@@ -43,12 +47,13 @@ public abstract class BaseClient
43
47
44
48
public const string CYBERSOURCE_PUBLIC_KEY = "CyberSource_SJC_US" ;
45
49
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 > ( ) ;
46
51
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
+ }
52
57
53
58
private static void SetupProxy ( )
54
59
{
@@ -126,14 +131,14 @@ protected static Configuration BuildConfigurationForRequest(
126
131
/// </param>
127
132
/// <returns>the built Configuration object</returns>
128
133
private static Configuration InternalBuildConfiguration (
129
- string merchantID , bool failIfNoMerchantID )
134
+ string merchantID , bool failIfNoMerchantID )
130
135
{
131
- Configuration config = new Configuration ( ) ;
132
-
136
+ Configuration config = new Configuration ( ) ;
137
+
133
138
if ( merchantID == null )
134
139
{
135
140
merchantID
136
- = AppSettings . GetSetting ( null , MERCHANT_ID ) ;
141
+ = AppSettings . GetSetting ( null , MERCHANT_ID ) ;
137
142
}
138
143
if ( merchantID != null || failIfNoMerchantID )
139
144
{
@@ -168,7 +173,7 @@ int boolVal
168
173
config . setLogProperties (
169
174
boolVal == 1 ,
170
175
AppSettings . GetSetting (
171
- merchantID , Configuration . LOG_DIRECTORY ) ) ;
176
+ merchantID , Configuration . LOG_DIRECTORY ) ) ;
172
177
173
178
config . ServerURL
174
179
= AppSettings . GetSetting (
@@ -222,6 +227,12 @@ int boolVal
222
227
merchantID , Configuration . USE_SIGNED_AND_ENCRYPTED ) ;
223
228
if ( boolVal != - 1 ) config . UseSignedAndEncrypted = ( boolVal == 1 ) ;
224
229
230
+ // certificate cache flag
231
+ boolVal
232
+ = AppSettings . GetBoolSetting (
233
+ merchantID , Configuration . CERTIFICATE_CACHE_ENABLED ) ;
234
+ if ( boolVal != - 1 ) config . CertificateCacheEnabled = ( boolVal == 1 ) ;
235
+
225
236
return ( config ) ;
226
237
}
227
238
@@ -285,7 +296,7 @@ protected static string GetXmlElementAttributeNamespace(Type type)
285
296
{
286
297
if ( logger != null )
287
298
{
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 ) ;
289
300
}
290
301
return "" ;
291
302
}
@@ -359,6 +370,54 @@ protected static CustomBinding getWCFCustomBinding(Configuration config)
359
370
currentBinding . Elements . Add ( textBindingElement ) ;
360
371
currentBinding . Elements . Add ( httpsTransport ) ;
361
372
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 ;
362
421
}
363
422
}
364
423
}
0 commit comments