Skip to content

Commit f99ad63

Browse files
committed
Replaced 'Security.LoadPrivateKey' with 'AuthenticationUtils.LoadSigningKey'
1 parent 3577a27 commit f99ad63

File tree

10 files changed

+68
-19
lines changed

10 files changed

+68
-19
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Security.Cryptography.X509Certificates;
4+
// ReSharper disable InconsistentNaming
5+
6+
namespace Mastercard.Developer.OAuth1Signer.Core.Utils
7+
{
8+
/// <summary>
9+
/// Utility class.
10+
/// </summary>
11+
public static class AuthenticationUtils
12+
{
13+
/// <summary>
14+
/// Load a RSA key out of a PKCS#12 container.
15+
/// </summary>
16+
public static RSA LoadSigningKey(string pkcs12KeyFilePath, string signingKeyAlias, string signingKeyPassword,
17+
X509KeyStorageFlags keyStorageFlags = X509KeyStorageFlags.DefaultKeySet)
18+
{
19+
if (pkcs12KeyFilePath == null) throw new ArgumentNullException(nameof(pkcs12KeyFilePath));
20+
var signingCertificate = new X509Certificate2(pkcs12KeyFilePath, signingKeyPassword, keyStorageFlags);
21+
return signingCertificate.GetRSAPrivateKey();
22+
}
23+
}
24+
}

Mastercard.Developer.OAuth1Signer.Core/Utils/SecurityUtils.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ namespace Mastercard.Developer.OAuth1Signer.Core.Utils
88
/// <summary>
99
/// Utility class.
1010
/// </summary>
11+
[Obsolete("Use AuthenticationUtils instead.")]
1112
public static class SecurityUtils
1213
{
13-
/// <summary>
14-
/// Load a RSA key out of a PKCS#12 container.
15-
/// </summary>
1614
public static RSA LoadPrivateKey(string pkcs12KeyFilePath, string signingKeyAlias, string signingKeyPassword,
1715
X509KeyStorageFlags keyStorageFlags = X509KeyStorageFlags.DefaultKeySet)
1816
{
19-
if (pkcs12KeyFilePath == null) throw new ArgumentNullException(nameof(pkcs12KeyFilePath));
20-
var signingCertificate = new X509Certificate2(pkcs12KeyFilePath, signingKeyPassword, keyStorageFlags);
21-
return signingCertificate.GetRSAPrivateKey();
17+
return AuthenticationUtils.LoadSigningKey(pkcs12KeyFilePath, signingKeyAlias, signingKeyPassword, keyStorageFlags);
2218
}
2319
}
2420
}

Mastercard.Developer.OAuth1Signer.Tests/Authenticators/RestSharpOAuth1AuthenticatorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class RestSharpOAuth1AuthenticatorTest
1313
public void TestPreAuthenticate_ShouldSignRequest()
1414
{
1515
// GIVEN
16-
var signingKey = TestUtils.GetTestPrivateKey();
16+
var signingKey = TestUtils.GetTestSigningKey();
1717
const string consumerKey = "Some key";
1818
var baseUri = new Uri("https://api.mastercard.com/");
1919
var request = new RestRequest

Mastercard.Developer.OAuth1Signer.Tests/OAuthTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void TestGetSignatureBaseString_Nominal()
137137
public void TestSignSignatureBaseString()
138138
{
139139
const string expectedSignatureString = "IJeNKYGfUhFtj5OAPRI92uwfjJJLCej3RCMLbp7R6OIYJhtwxnTkloHQ2bgV7fks4GT/A7rkqrgUGk0ewbwIC6nS3piJHyKVc7rvQXZuCQeeeQpFzLRiH3rsb+ZS+AULK+jzDje4Fb+BQR6XmxuuJmY6YrAKkj13Ln4K6bZJlSxOizbNvt+Htnx+hNd4VgaVBeJKcLhHfZbWQxK76nMnjY7nDcM/2R6LUIR2oLG1L9m55WP3bakAvmOr392ulv1+mWCwDAZZzQ4lakDD2BTu0ZaVsvBW+mcKFxYeTq7SyTQMM4lEwFPJ6RLc8jJJ+veJXHekLVzWg4qHRtzNBLz1mA==";
140-
Assert.AreEqual(expectedSignatureString, OAuth.SignSignatureBaseString("baseString", Encoding.UTF8, TestUtils.GetTestPrivateKey()));
140+
Assert.AreEqual(expectedSignatureString, OAuth.SignSignatureBaseString("baseString", Encoding.UTF8, TestUtils.GetTestSigningKey()));
141141
}
142142

143143
[TestMethod]

Mastercard.Developer.OAuth1Signer.Tests/Signers/NetHttpClientSignerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class NetHttpClientSignerTest
1313
public void TestSign_ShouldAddOAuth1HeaderToPostRequest()
1414
{
1515
// GIVEN
16-
var signingKey = TestUtils.GetTestPrivateKey();
16+
var signingKey = TestUtils.GetTestSigningKey();
1717
const string consumerKey = "Some key";
1818
var request = new HttpRequestMessage
1919
{
@@ -34,7 +34,7 @@ public void TestSign_ShouldAddOAuth1HeaderToPostRequest()
3434
public void TestSign_ShouldAddOAuth1HeaderToGetRequest()
3535
{
3636
// GIVEN
37-
var signingKey = TestUtils.GetTestPrivateKey();
37+
var signingKey = TestUtils.GetTestSigningKey();
3838
const string consumerKey = "Some key";
3939
var request = new HttpRequestMessage
4040
{

Mastercard.Developer.OAuth1Signer.Tests/Signers/RestSharpSignerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class RestSharpSignerTest
1313
public void TestSign_ShouldAddOAuth1HeaderToPostRequest()
1414
{
1515
// GIVEN
16-
var signingKey = TestUtils.GetTestPrivateKey();
16+
var signingKey = TestUtils.GetTestSigningKey();
1717
const string consumerKey = "Some key";
1818
var baseUri = new Uri("https://api.mastercard.com/");
1919
var request = new RestRequest
@@ -42,7 +42,7 @@ public void TestSign_ShouldAddOAuth1HeaderToPostRequest()
4242
public void TestSign_ShouldAddOAuth1HeaderToGetRequest()
4343
{
4444
// GIVEN
45-
var signingKey = TestUtils.GetTestPrivateKey();
45+
var signingKey = TestUtils.GetTestSigningKey();
4646
const string consumerKey = "Some key";
4747
var baseUri = new Uri("https://api.mastercard.com/");
4848
var request = new RestRequest

Mastercard.Developer.OAuth1Signer.Tests/Test/TestUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Mastercard.Developer.OAuth1Signer.Tests.Test
66
{
77
internal static class TestUtils
88
{
9-
internal static RSA GetTestPrivateKey() => SecurityUtils.LoadPrivateKey(
9+
internal static RSA GetTestSigningKey() => AuthenticationUtils.LoadSigningKey(
1010
"./_Resources/test_key_container.p12",
1111
"mykeyalias",
1212
"Password1",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Security.Cryptography.X509Certificates;
2+
using Mastercard.Developer.OAuth1Signer.Core.Utils;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Mastercard.Developer.OAuth1Signer.Tests.Utils
6+
{
7+
[TestClass]
8+
public class AuthenticationUtilsTest
9+
{
10+
[TestMethod]
11+
public void TestLoadSigningKey_ShouldReturnKey()
12+
{
13+
// GIVEN
14+
const string keyContainerPath = "./_Resources/test_key_container.p12";
15+
const string keyAlias = "mykeyalias";
16+
const string keyPassword = "Password1";
17+
18+
// WHEN
19+
const X509KeyStorageFlags flags = X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable; // https://github.com/dotnet/corefx/issues/14745
20+
var privateKey = AuthenticationUtils.LoadSigningKey(keyContainerPath, keyAlias, keyPassword, flags);
21+
22+
// THEN
23+
Assert.AreEqual(2048, privateKey.KeySize);
24+
Assert.AreEqual("RSA", privateKey.KeyExchangeAlgorithm);
25+
}
26+
}
27+
}

Mastercard.Developer.OAuth1Signer.Tests/Utils/SecurityUtilsTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
23
using Mastercard.Developer.OAuth1Signer.Core.Utils;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45

56
namespace Mastercard.Developer.OAuth1Signer.Tests.Utils
67
{
78
[TestClass]
9+
[Obsolete("Use AuthenticationUtils instead.")]
810
public class SecurityUtilsTest
911
{
1012
[TestMethod]

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ dotnet add package Mastercard.Developer.OAuth1Signer.RestSharp
5353

5454
### Loading the Signing Key <a name="loading-the-signing-key"></a>
5555

56-
A `System.Security.Cryptography.RSA` key object can be created by calling the `SecurityUtils.LoadPrivateKey` method:
56+
A `System.Security.Cryptography.RSA` key object can be created by calling the `AuthenticationUtils.LoadSigningKey` method:
5757
```cs
58-
var signingKey = SecurityUtils.LoadPrivateKey(
59-
"<insert PKCS#12 key file path>",
60-
"<insert key alias>",
61-
"<insert key password>");
58+
var signingKey = AuthenticationUtils.LoadSigningKey(
59+
"<insert PKCS#12 key file path>",
60+
"<insert key alias>",
61+
"<insert key password>");
6262
```
6363

6464
### Creating the OAuth Authorization Header <a name="creating-the-oauth-authorization-header"></a>

0 commit comments

Comments
 (0)