Skip to content

Commit

Permalink
Stop using a single variable for nonces
Browse files Browse the repository at this point in the history
When sending multiple requests in parallel, there is a chance they will be using the same nonce, as it's stored as a single string.

Instead, convert to using a concurrenct queue and enqueue/dequeue as requests are going out and responses coming in.
  • Loading branch information
Kencdk committed Apr 10, 2021
1 parent 91580e6 commit aa06367
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Libraries/ACMELib/ACMERestClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Kenc.ACMELib
{
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -32,7 +33,8 @@ public class ACMERestClient : IRestClient

private readonly Jws jws;
private readonly string UserAgent;
private string nonce = string.Empty;

private readonly ConcurrentQueue<string> nonces = new ConcurrentQueue<string>();

/// <summary>
/// Initializes a new instance of the <see cref="ACMERestClient"/> class.
Expand Down Expand Up @@ -92,7 +94,7 @@ public ACMERestClient(Jws jws)

if (message != null)
{
if (string.IsNullOrEmpty(nonce))
if (!nonces.TryDequeue(out var nonce))
{
throw new NoNonceException();
}
Expand Down Expand Up @@ -125,7 +127,11 @@ public ACMERestClient(Jws jws)

if (response.Headers.AllKeys.Contains("Replay-Nonce"))
{
nonce = response.Headers.GetValues("Replay-Nonce").First();
var nonce = response.Headers.GetValues("Replay-Nonce").First();
if (!string.IsNullOrWhiteSpace(nonce))
{
nonces.Enqueue(nonce);
}
}

var responseBody = string.Empty;
Expand Down

0 comments on commit aa06367

Please sign in to comment.