Skip to content

Commit 132a6b7

Browse files
committed
Used 16 char length nonces (32-char OAuth nonces are rejected by some services)
1 parent f99ad63 commit 132a6b7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Mastercard.Developer.OAuth1Signer.Core/OAuth.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Security.Cryptography;
45
using System.Text;
56
using Mastercard.Developer.OAuth1Signer.Core.Utils;
@@ -13,6 +14,7 @@ namespace Mastercard.Developer.OAuth1Signer.Core
1314
public static class OAuth
1415
{
1516
public const string AuthorizationHeaderName = "Authorization";
17+
private static readonly Random Random = new Random();
1618

1719
/// <summary>
1820
/// Creates a Mastercard API compliant OAuth Authorization header.
@@ -210,9 +212,9 @@ private static byte[] Sha256Digest(string input, Encoding encoding)
210212
}
211213

212214
/// <summary>
213-
/// Generates a random string for replay protection as per https://tools.ietf.org/html/rfc5849#section-3.3.
215+
/// Generates a 16 char random string for replay protection as per https://tools.ietf.org/html/rfc5849#section-3.3.
214216
/// </summary>
215-
internal static string GetNonce() => Guid.NewGuid().ToString().Replace("-", string.Empty);
217+
internal static string GetNonce() => string.Concat(Enumerable.Range(0, 16).Select(_ => Random.Next(16).ToString("x")));
216218

217219
/// <summary>
218220
/// Returns UNIX Timestamp as required per https://tools.ietf.org/html/rfc5849#section-3.3.

Mastercard.Developer.OAuth1Signer.Tests/OAuthTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ public void TestToUriRfc3986()
173173
}
174174

175175
[TestMethod]
176-
public void TestGetNonce_ShouldHaveLengthOf32()
176+
public void TestGetNonce_ShouldHaveLengthOf16()
177177
{
178-
var nonce = OAuth.GetNonce();
179-
Assert.AreEqual(32, nonce.Length);
178+
Enumerable.Range(0, 100000).ToList().ForEach(_ =>
179+
{
180+
var nonce = OAuth.GetNonce();
181+
Assert.AreEqual(16, nonce.Length);
182+
});
180183
}
181184
}
182185
}

0 commit comments

Comments
 (0)