Skip to content

Commit c7024b7

Browse files
authored
Merge pull request #113 from CyberSource/release-march25
Release march25
2 parents 4f7fe0e + 1c5885c commit c7024b7

File tree

252 files changed

+122058
-116224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

252 files changed

+122058
-116224
lines changed

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/AuthenticationSdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>0.0.1.16</Version>
6+
<Version>0.0.1.17</Version>
77
<Authors>CyberSource</Authors>
88
<Product>Authentication_SDK</Product>
99
<Description />

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/authentication/jwt/JwtToken.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public JwtToken(MerchantConfig merchantConfig)
2121

2222
KeyAlias = merchantConfig.KeyAlias;
2323
KeyPass = merchantConfig.KeyPass;
24-
Certificate = Cache.FetchCachedCertificate(P12FilePath, KeyPass);
24+
X509Certificate2Collection certs = Cache.FetchCachedCertificate(P12FilePath, KeyPass);
25+
Certificate = Cache.GetCertBasedOnKeyAllias(certs, merchantConfig.KeyAlias);
2526
}
2627

2728
public string BearerToken { get; set; }

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/core/Authorize.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public HttpToken GetSignature()
6363
_logger.Debug($"Date: {signatureObj.GmtDateTime}");
6464
_logger.Debug($"Host: {signatureObj.HostName}");
6565

66-
//if (_merchantConfig.IsPostRequest || _merchantConfig.IsPutRequest || _merchantConfig.IsPatchRequest)
67-
//{
68-
// _logger.Debug($"digest: {signatureObj.Digest}");
69-
//}
66+
if (_merchantConfig.IsPostRequest || _merchantConfig.IsPutRequest || _merchantConfig.IsPatchRequest)
67+
{
68+
logUtility.LogDebugMessage( _logger, $"digest: {signatureObj.Digest}");
69+
}
7070

71-
//logUtility.LogDebugMessage( _logger, $"Signature : {signatureObj.SignatureParam}");
71+
logUtility.LogDebugMessage( _logger, $"Signature : {signatureObj.SignatureParam}");
7272

7373
return signatureObj;
7474
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/core/MerchantConfig.cs

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace AuthenticationSdk.core
1818
*============================================================================================*/
1919
public class MerchantConfig
2020
{
21-
public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictionary = null)
21+
public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictionary = null, Dictionary<string, bool> mapToControlMLEonAPI = null)
2222
{
2323
var _propertiesSetUsing = string.Empty;
2424

@@ -37,7 +37,7 @@ public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictiona
3737
{
3838
_propertiesSetUsing = "Dictionary Object";
3939

40-
SetValuesUsingDictObj(merchantConfigDictionary);
40+
SetValuesUsingDictObj(merchantConfigDictionary, mapToControlMLEonAPI);
4141
}
4242
else
4343
{
@@ -48,7 +48,7 @@ public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictiona
4848
{
4949
_propertiesSetUsing = "App.Config File";
5050

51-
SetValuesFromAppConfig(merchantConfigSection);
51+
SetValuesFromAppConfig(merchantConfigSection, mapToControlMLEonAPI);
5252
}
5353
else
5454
{
@@ -64,6 +64,8 @@ public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictiona
6464

6565
// Validations
6666
ValidateProperties();
67+
//validate MLE configs
68+
ValidateMLEProperties();
6769
}
6870

6971
#region Class Properties
@@ -162,6 +164,12 @@ public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictiona
162164

163165
public string PemFileDirectory { get; set; }
164166

167+
public bool UseMLEGlobally { get; set; }
168+
169+
public Dictionary<string, bool> MapToControlMLEonAPI { get; set; }
170+
171+
public string MleKeyAlias { get; set; }
172+
165173
#endregion
166174

167175
public void LogMerchantConfigurationProperties()
@@ -195,7 +203,7 @@ public void LogMerchantConfigurationProperties()
195203
Logger.Debug($"Merchant Configuration :\n{merchCfgLogString}");
196204
}
197205

198-
private void SetValuesFromAppConfig(NameValueCollection merchantConfigSection)
206+
private void SetValuesFromAppConfig(NameValueCollection merchantConfigSection, Dictionary<string, bool> mapToControlMLEonAPI)
199207
{
200208
MerchantId = merchantConfigSection["merchantID"];
201209
PortfolioId = merchantConfigSection["portfolioID"];
@@ -223,9 +231,30 @@ private void SetValuesFromAppConfig(NameValueCollection merchantConfigSection)
223231
ProxyUsername = merchantConfigSection["proxyUsername"];
224232
ProxyPassword = merchantConfigSection["proxyPassword"];
225233
PemFileDirectory = merchantConfigSection["pemFileDirectory"];
234+
235+
if (merchantConfigSection["useMLEGlobally"] != null && "true".Equals(merchantConfigSection["useMLEGlobally"], StringComparison.OrdinalIgnoreCase))
236+
{
237+
UseMLEGlobally = bool.Parse(merchantConfigSection["useMLEGlobally"]);
238+
}
239+
else
240+
{
241+
UseMLEGlobally = false;
242+
}
243+
244+
MapToControlMLEonAPI = mapToControlMLEonAPI;
245+
246+
if (merchantConfigSection["mleKeyAlias"] != null)
247+
{
248+
MleKeyAlias = merchantConfigSection["mleKeyAlias"]?.Trim();
249+
}
250+
251+
if (string.IsNullOrWhiteSpace(MleKeyAlias?.Trim()))
252+
{
253+
MleKeyAlias = Constants.DefaultMleAliasForCert;
254+
}
226255
}
227256

228-
private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantConfigDictionary)
257+
private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantConfigDictionary, Dictionary<string, bool> mapToControlMLEonAPI)
229258
{
230259
var key = string.Empty;
231260

@@ -434,6 +463,31 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
434463
{
435464
PemFileDirectory = merchantConfigDictionary["pemFileDirectory"];
436465
}
466+
467+
if (merchantConfigDictionary.ContainsKey("useMLEGlobally") && "true".Equals(merchantConfigDictionary["useMLEGlobally"], StringComparison.OrdinalIgnoreCase))
468+
{
469+
UseMLEGlobally = bool.Parse(merchantConfigDictionary["useMLEGlobally"]);
470+
}
471+
else
472+
{
473+
UseMLEGlobally = false;
474+
}
475+
476+
if (mapToControlMLEonAPI != null)
477+
{
478+
MapToControlMLEonAPI = mapToControlMLEonAPI;
479+
}
480+
481+
if (merchantConfigDictionary.ContainsKey("mleKeyAlias"))
482+
{
483+
MleKeyAlias = merchantConfigDictionary["mleKeyAlias"]?.Trim();
484+
}
485+
486+
//if MleKeyAlias is null or empty or contains only whitespace then set default value
487+
if (string.IsNullOrWhiteSpace(MleKeyAlias?.Trim()))
488+
{
489+
MleKeyAlias = Constants.DefaultMleAliasForCert;
490+
}
437491
}
438492
}
439493
catch (KeyNotFoundException err)
@@ -547,5 +601,29 @@ private void ValidateProperties()
547601
P12Keyfilepath = $"{KeyDirectory}{pathDirectorySeparator}{KeyfileName}.p12";
548602
}
549603
}
604+
605+
private void ValidateMLEProperties()
606+
{
607+
bool mleConfigured = UseMLEGlobally;
608+
609+
if (MapToControlMLEonAPI != null && MapToControlMLEonAPI.Count > 0)
610+
{
611+
foreach (bool value in MapToControlMLEonAPI.Values)
612+
{
613+
if (value)
614+
{
615+
mleConfigured = true;
616+
break;
617+
}
618+
}
619+
}
620+
621+
//if MLE=true then check for auth Type
622+
if (mleConfigured && !Enumerations.AuthenticationType.JWT.ToString().Equals(AuthenticationType, StringComparison.OrdinalIgnoreCase))
623+
{
624+
Logger.Error("MLE is only supported in JWT auth type");
625+
throw new Exception("MLE is only supported in JWT auth type");
626+
}
627+
}
550628
}
551629
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/Cache.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public static class Cache
2424

2525
private static readonly string regexForFileNameFromDirectory = "(^([a-z]|[A-Z]):(?=\\\\(?![\\0-\\37<>:\"/\\\\|?*])|\\/(?![\\0-\\37<>:\"/\\\\|?*])|$)|^\\\\(?=[\\\\\\/][^\\0-\\37<>:\"/\\\\|?*]+)|^(?=(\\\\|\\/)$)|^\\.(?=(\\\\|\\/)$)|^\\.\\.(?=(\\\\|\\/)$)|^(?=(\\\\|\\/)[^\\0-\\37<>:\"/\\\\|?*]+)|^\\.(?=(\\\\|\\/)[^\\0-\\37<>:\"/\\\\|?*]+)|^\\.\\.(?=(\\\\|\\/)[^\\0-\\37<>:\"/\\\\|?*]+))((\\\\|\\/)([^\\0-\\37<>:\"/\\\\|?*]+|(\\\\|\\/)$))*()$";
2626

27-
public static X509Certificate2 FetchCachedCertificate(string p12FilePath, string keyPassword)
27+
private class CertInfo
28+
{
29+
public X509Certificate2Collection Certificates { get; set; }
30+
public DateTime Timestamp { get; set; }
31+
}
32+
33+
public static X509Certificate2Collection FetchCachedCertificate(string p12FilePath, string keyPassword)
2834
{
2935
try
3036
{
@@ -34,30 +40,27 @@ public static X509Certificate2 FetchCachedCertificate(string p12FilePath, string
3440

3541
var matches = Regex.Match(p12FilePath, regexForFileNameFromDirectory);
3642
var certFile = matches.Groups[11].ToString();
37-
38-
if (!cache.Contains(certFile))
43+
if (!cache.Contains(certFile) || ((CertInfo)cache[certFile]).Timestamp != File.GetLastWriteTime(p12FilePath))
3944
{
4045
var policy = new CacheItemPolicy();
4146
var filePaths = new List<string>();
4247
var cachedFilePath = Path.GetFullPath(p12FilePath);
4348
filePaths.Add(cachedFilePath);
4449
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
50+
var certificates = new X509Certificate2Collection();
51+
certificates.Import(p12FilePath, keyPassword, X509KeyStorageFlags.PersistKeySet);
4552

46-
var certificate = new X509Certificate2(p12FilePath, keyPassword);
47-
cache.Set(certFile, certificate, policy);
48-
return certificate;
49-
}
50-
else if (cache[certFile] is X509Certificate2 cachedCertificateFromP12File)
51-
{
52-
return cachedCertificateFromP12File;
53-
}
54-
else
55-
{
56-
return null;
53+
CertInfo certInfo = new CertInfo();
54+
certInfo.Certificates = certificates;
55+
certInfo.Timestamp = File.GetLastWriteTime(p12FilePath);
56+
57+
cache.Set(certFile, certInfo, policy);
5758
}
59+
//return all certs in p12
60+
return ((CertInfo)cache[certFile]).Certificates;
5861
}
5962
}
60-
catch (CryptographicException e)
63+
catch (Exception e)
6164
{
6265
if (e.Message.Equals("The specified network password is not correct.\r\n"))
6366
{
@@ -97,5 +100,18 @@ public static RSAParameters FetchCachedRSAParameters(MerchantConfig merchantConf
97100
return (RSAParameters)cache[certFile];
98101
}
99102
}
103+
104+
public static X509Certificate2 GetCertBasedOnKeyAllias(X509Certificate2Collection certs, String keyAlias)
105+
{
106+
foreach (var cert in certs)
107+
{
108+
if (cert.GetNameInfo(X509NameType.SimpleName, false).Equals(keyAlias, StringComparison.OrdinalIgnoreCase))
109+
{
110+
return cert;
111+
}
112+
}
113+
throw new Exception($"{Constants.ErrorPrefix} Certificate with alias {keyAlias} not found.");
114+
115+
}
100116
}
101117
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/Constants.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ public static class Constants
3333
public static readonly string DeprecationPrefix = "Deprecated: ";
3434

3535
public static readonly string P12FileDirectory = "..\\..\\Resource";
36+
37+
public static readonly string DefaultMleAliasForCert = "CyberSource_SJC_US";
38+
39+
public static readonly int CertificateExpiryDateWarningDays = 90;
40+
41+
public static readonly string LOG_REQUEST_BEFORE_MLE = "LOG_REQUEST_BEFORE_MLE: ";
42+
43+
public static readonly string LOG_REQUEST_AFTER_MLE = "LOG_REQUEST_AFTER_MLE: ";
3644
}
3745
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/LogUtility.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ private void LoadSensitiveDataConfiguration()
4949

5050
public string MaskSensitiveData(string str)
5151
{
52+
if (str.StartsWith(Constants.LOG_REQUEST_BEFORE_MLE))
53+
{
54+
return Constants.LOG_REQUEST_BEFORE_MLE + MaskSensitiveData(str.Substring(Constants.LOG_REQUEST_BEFORE_MLE.Length));
55+
}
56+
if (str.StartsWith(Constants.LOG_REQUEST_AFTER_MLE))
57+
{
58+
return Constants.LOG_REQUEST_AFTER_MLE + MaskSensitiveData(str.Substring(Constants.LOG_REQUEST_AFTER_MLE.Length));
59+
}
60+
5261
bool isJsonString;
5362
try
5463
{
@@ -112,14 +121,7 @@ public void MaskSensitiveData(JObject jsonMsg)
112121

113122
public void LogDebugMessage(Logger logger, String debugMessage)
114123
{
115-
if (IsMaskingEnabled(logger))
116-
{
117-
logger.Debug(MaskSensitiveData(debugMessage));
118-
}
119-
else
120-
{
121-
logger.Debug(debugMessage);
122-
}
124+
logger.Debug(MaskSensitiveData(debugMessage));
123125
}
124126

125127
public bool IsMaskingEnabled(Logger logger)

0 commit comments

Comments
 (0)