Skip to content

Commit 759ed12

Browse files
authored
Merge pull request #6 from CyberSource/022021
February-March 2021 Release
2 parents ed36a31 + 94e9f12 commit 759ed12

File tree

73 files changed

+1830
-437
lines changed

Some content is hidden

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

73 files changed

+1830
-437
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.1</Version>
6+
<Version>0.0.1.2</Version>
77
<Authors>CyberSource</Authors>
88
<Product>Authentication_SDK</Product>
99
<Description />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using AuthenticationSdk.core;
3+
using AuthenticationSdk.util;
4+
5+
namespace AuthenticationSdk.authentication.oauth
6+
{
7+
public class OAuthToken : Token
8+
{
9+
public OAuthToken(MerchantConfig merchantConfig)
10+
{
11+
AccessToken = merchantConfig.AccessToken;
12+
RefreshToken = merchantConfig.RefreshToken;
13+
}
14+
15+
public string AccessToken { get; set; }
16+
17+
public string RefreshToken { get; set; }
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using AuthenticationSdk.core;
5+
6+
namespace AuthenticationSdk.authentication.oauth
7+
{
8+
public class OAuthTokenGenerator : ITokenGenerator
9+
{
10+
private readonly MerchantConfig _merchantConfig;
11+
private readonly OAuthToken _oauthtoken;
12+
13+
public OAuthTokenGenerator(MerchantConfig merchantConfig)
14+
{
15+
_merchantConfig = merchantConfig;
16+
_oauthtoken = new OAuthToken(_merchantConfig);
17+
}
18+
19+
public Token GetToken()
20+
{
21+
return _oauthtoken;
22+
}
23+
}
24+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using AuthenticationSdk.authentication.http;
33
using AuthenticationSdk.authentication.jwt;
4+
using AuthenticationSdk.authentication.oauth;
45
using AuthenticationSdk.util;
56
using NLog;
67

@@ -110,6 +111,45 @@ public JwtToken GetToken()
110111
}
111112
}
112113

114+
/**
115+
* @return a OAuthToken object (OAuth Bearer Token),
116+
* based on the Merchant Configuration passed to the Constructor of Authorize Class
117+
*/
118+
public OAuthToken GetOAuthToken()
119+
{
120+
try
121+
{
122+
if (_merchantConfig != null)
123+
{
124+
LogMerchantDetails();
125+
126+
Enumerations.ValidateRequestType(_merchantConfig.RequestType);
127+
128+
var tokenObj = (OAuthToken)new OAuthTokenGenerator(_merchantConfig).GetToken();
129+
130+
if (_merchantConfig.IsGetRequest || _merchantConfig.IsDeleteRequest)
131+
{
132+
_logger.Trace("{0} {1}", "Content-Type:", "application/json");
133+
}
134+
else if (_merchantConfig.IsPostRequest || _merchantConfig.IsPutRequest || _merchantConfig.IsPatchRequest)
135+
{
136+
_logger.Trace("{0} {1}", "Content-Type:", "application/hal+json");
137+
}
138+
139+
_logger.Trace("{0} {1}", "Authorization:", tokenObj.AccessToken);
140+
141+
return tokenObj;
142+
}
143+
144+
return null;
145+
}
146+
catch (Exception e)
147+
{
148+
ExceptionUtility.Exception(e.Message, e.StackTrace);
149+
return null;
150+
}
151+
}
152+
113153
private void LogMerchantDetails()
114154
{
115155
try

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

Lines changed: 151 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ If a dictionary object has been passed use that object
7676

7777
public string UseMetaKey { get; set; }
7878

79+
public string EnableClientCert { get; set; }
80+
81+
public string ClientCertDirectory { get; set; }
82+
83+
public string ClientCertFile { get; set; }
84+
85+
public string ClientCertPassword { get; set; }
86+
87+
public string ClientId { get; set; }
88+
89+
public string AccessToken { get; set; }
90+
91+
public string RefreshToken { get; set; }
92+
93+
public string ClientSecret { get; set; }
94+
7995
public string AuthenticationType { get; set; }
8096

8197
public string KeyDirectory { get; set; }
@@ -136,6 +152,8 @@ If a dictionary object has been passed use that object
136152

137153
public bool IsJwtTokenAuthType { get; set; }
138154

155+
public bool IsOAuthTokenAuthType { get; set; }
156+
139157
public static string LogAllproperties(MerchantConfig obj)
140158
{
141159
var hiddenProperties = Constants.HideMerchantConfigProps.Split(',');
@@ -170,6 +188,12 @@ private void SetValuesFromAppConfig(NameValueCollection merchantConfigSection)
170188
KeyDirectory = merchantConfigSection["keysDirectory"];
171189
KeyfileName = merchantConfigSection["keyFilename"];
172190
RunEnvironment = merchantConfigSection["runEnvironment"];
191+
EnableClientCert = merchantConfigSection["enableClientCert"];
192+
ClientCertDirectory = merchantConfigSection["clientCertDirectory"];
193+
ClientCertFile = merchantConfigSection["clientCertFile"];
194+
ClientCertPassword = merchantConfigSection["clientCertPassword"];
195+
ClientId = merchantConfigSection["clientId"];
196+
ClientSecret = merchantConfigSection["clientSecret"];
173197
KeyAlias = merchantConfigSection["keyAlias"];
174198
KeyPass = merchantConfigSection["keyPass"];
175199
EnableLog = merchantConfigSection["enableLog"];
@@ -194,25 +218,31 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
194218
{
195219
_propertiesSetUsing = "Dictionary Object";
196220

197-
// MANDATORY KEYS
198-
key = "merchantID";
199-
MerchantId = merchantConfigDictionary[key];
221+
// MANDATORY KEYS
200222
key = "runEnvironment";
201223
RunEnvironment = merchantConfigDictionary[key];
202224
key = "authenticationType";
203225
AuthenticationType = merchantConfigDictionary[key];
226+
204227
key = "useMetaKey";
205-
UseMetaKey = merchantConfigDictionary[key];
206-
if(string.IsNullOrEmpty(UseMetaKey))
228+
UseMetaKey = "false";
229+
if (merchantConfigDictionary.ContainsKey(key))
207230
{
208-
UseMetaKey = "false";
231+
UseMetaKey = merchantConfigDictionary[key];
232+
if (string.IsNullOrEmpty(UseMetaKey))
233+
{
234+
UseMetaKey = "false";
235+
}
209236
}
237+
210238

211239
Enumerations.AuthenticationType authTypeInput;
212240
Enum.TryParse(AuthenticationType.ToUpper(), out authTypeInput);
213241

214242
if (Equals(authTypeInput, Enumerations.AuthenticationType.HTTP_SIGNATURE))
215243
{
244+
key = "merchantID";
245+
MerchantId = merchantConfigDictionary[key];
216246
key = "merchantsecretKey";
217247
MerchantSecretKey = merchantConfigDictionary[key];
218248
key = "merchantKeyId";
@@ -233,6 +263,8 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
233263
// only if the key is passed read the value, otherwise use default / null values
234264
if (Equals(authTypeInput, Enumerations.AuthenticationType.JWT))
235265
{
266+
key = "merchantID";
267+
MerchantId = merchantConfigDictionary[key];
236268
if (merchantConfigDictionary.ContainsKey("keyAlias"))
237269
{
238270
KeyAlias = merchantConfigDictionary["keyAlias"];
@@ -254,6 +286,96 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
254286
}
255287
}
256288

289+
if(Equals(authTypeInput, Enumerations.AuthenticationType.OAUTH))
290+
{
291+
IsOAuthTokenAuthType = true;
292+
key = "accessToken";
293+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
294+
{
295+
AccessToken = merchantConfigDictionary[key];
296+
}
297+
else
298+
{
299+
throw new KeyNotFoundException();
300+
}
301+
302+
key = "refreshToken";
303+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
304+
{
305+
RefreshToken = merchantConfigDictionary[key];
306+
}
307+
else
308+
{
309+
throw new KeyNotFoundException();
310+
}
311+
}
312+
313+
if (Equals(authTypeInput, Enumerations.AuthenticationType.MUTUAL_AUTH))
314+
{
315+
key = "clientId";
316+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
317+
{
318+
ClientId = merchantConfigDictionary[key];
319+
}
320+
else
321+
{
322+
throw new KeyNotFoundException();
323+
}
324+
325+
key = "clientSecret";
326+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
327+
{
328+
ClientSecret = merchantConfigDictionary[key];
329+
}
330+
else
331+
{
332+
throw new KeyNotFoundException();
333+
}
334+
}
335+
336+
key = "enableClientCert";
337+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
338+
{
339+
EnableClientCert = merchantConfigDictionary[key];
340+
}
341+
else
342+
{
343+
EnableClientCert = "false";
344+
}
345+
346+
if(Equals(bool.Parse(EnableClientCert.ToString()), true))
347+
{
348+
key = "clientCertFile";
349+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
350+
{
351+
ClientCertFile = merchantConfigDictionary[key];
352+
}
353+
else
354+
{
355+
throw new KeyNotFoundException();
356+
}
357+
358+
key = "clientCertPassword";
359+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
360+
{
361+
ClientCertPassword = merchantConfigDictionary[key];
362+
}
363+
else
364+
{
365+
throw new KeyNotFoundException();
366+
}
367+
368+
key = "clientCertDirectory";
369+
if (merchantConfigDictionary.ContainsKey(key) && !string.IsNullOrEmpty(merchantConfigDictionary[key]))
370+
{
371+
ClientCertDirectory = merchantConfigDictionary[key];
372+
}
373+
else
374+
{
375+
throw new KeyNotFoundException();
376+
}
377+
}
378+
257379
if (merchantConfigDictionary.ContainsKey("enableLog"))
258380
{
259381
EnableLog = merchantConfigDictionary["enableLog"];
@@ -314,12 +436,7 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
314436

315437
private void ValidateProperties()
316438
{
317-
// VALIDATIONS COMMON FOR BOTH AUTH MECH.
318-
if (string.IsNullOrEmpty(MerchantId))
319-
{
320-
throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
321-
}
322-
439+
323440
// Validating and setting up Authentication type
324441
Enumerations.ValidateAuthenticationType(AuthenticationType);
325442

@@ -369,6 +486,18 @@ private void ValidateProperties()
369486
{
370487
HostName = Constants.IDCProdHostName;
371488
}
489+
else if (RunEnvironmentTester.Equals(Constants.CybsMutualAuthProdRunEnv.ToUpper()))
490+
{
491+
HostName = Constants.CybsMutualAuthProdHostName;
492+
}
493+
else if (RunEnvironmentTester.Equals(Constants.CybsMutualAuthSandboxRunEnv.ToUpper()))
494+
{
495+
HostName = Constants.CybsMutualAuthSandboxHostName;
496+
}
497+
else if (RunEnvironmentTester.Equals(Constants.SitMutualAuthRunEnv.ToUpper()))
498+
{
499+
HostName = Constants.SitMutualAuthHostName;
500+
}
372501
else
373502
{
374503
HostName = RunEnvironment.ToLower();
@@ -378,6 +507,11 @@ private void ValidateProperties()
378507
// 1. FOR HTTP SIGNATURE
379508
if (IsHttpSignAuthType)
380509
{
510+
if (string.IsNullOrEmpty(MerchantId))
511+
{
512+
throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
513+
}
514+
381515
if (string.IsNullOrEmpty(MerchantKeyId))
382516
{
383517
throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantKeyId is Mandatory");
@@ -392,6 +526,11 @@ private void ValidateProperties()
392526
// 2. FOR JWT TOKEN
393527
else if (IsJwtTokenAuthType)
394528
{
529+
if (string.IsNullOrEmpty(MerchantId))
530+
{
531+
throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
532+
}
533+
395534
if (string.IsNullOrEmpty(KeyAlias))
396535
{
397536
KeyAlias = MerchantId;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static class Constants
1818

1919
public static readonly string CybsProdHostName = "api.cybersource.com";
2020

21+
public static readonly string CybsMutualAuthSandboxHostName = "api-matest.cybersource.com";
22+
23+
public static readonly string CybsMutualAuthProdHostName = "api-ma.cybersource.com";
24+
2125
public static readonly string BoASandboxHostName = "apitest.merchant-services.bankofamerica.com";
2226

2327
public static readonly string BoAProdHostName = "api.merchant-services.bankofamerica.com";
@@ -26,10 +30,16 @@ public static class Constants
2630

2731
public static readonly string IDCProdHostName = "api.in.cybersource.com";
2832

33+
public static readonly string SitMutualAuthHostName = "pnrstage.ic3.com:8451";
34+
2935
public static readonly string CybsSandboxRunEnv = "cybersource.environment.sandbox";
3036

3137
public static readonly string CybsProdRunEnv = "cybersource.environment.production";
3238

39+
public static readonly string CybsMutualAuthSandboxRunEnv = "cybersource.environment.mutualauth.sandbox";
40+
41+
public static readonly string CybsMutualAuthProdRunEnv = "cybersource.environment.mutualauth.production";
42+
3343
public static readonly string BoASandboxRunEnv = "bankofamerica.environment.sandbox";
3444

3545
public static readonly string BoAProdRunEnv = "bankofamerica.environment.production";
@@ -38,10 +48,16 @@ public static class Constants
3848

3949
public static readonly string IDCProdRunEnv = "cybesource.in.environment.production";
4050

51+
public static readonly string SitMutualAuthRunEnv = "cybesource.environment.mutualauth.sit";
52+
4153
public static readonly string AuthMechanismHttp = "http_signature";
4254

4355
public static readonly string AuthMechanismJwt = "jwt";
4456

57+
public static readonly string AuthMechanismMutualAuth = "mutual_auth";
58+
59+
public static readonly string AuthMechanismOAuth = "oauth";
60+
4561
public static readonly string ErrorPrefix = "Error: ";
4662

4763
public static readonly string WarningPrefix = "Warning: ";

0 commit comments

Comments
 (0)