Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/FluffySpoon.AspNet.EncryptWeMust.Azure/FluffySpoon.AspNet.EncryptWeMust.Azure.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust.EntityFramework.Sample/FluffySpoon.AspNet.EncryptWeMust.EntityFramework.Sample.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust.EntityFramework/FluffySpoon.AspNet.EncryptWeMust.EntityFramework.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust.Redis/FluffySpoon.AspNet.EncryptWeMust.DistributedCache.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust.Sample/FluffySpoon.AspNet.EncryptWeMust.Sample.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust.Tests/FluffySpoon.AspNet.EncryptWeMust.Tests.csproj
#	src/FluffySpoon.AspNet.EncryptWeMust/FluffySpoon.AspNet.EncryptWeMust.csproj
  • Loading branch information
ffMathy committed May 2, 2022
2 parents 89cd298 + 19a1deb commit cf9b452
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# What is this?
The simplest LetsEncrypt setup for ASP .NET Core. Almost no server configuration needed.

`Install-Package FluffySpoon.AspNet.EncryptWeMust`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Certes;
using Certes.Acme;
using Certes.Acme.Resource;
using FluentAssertions;
using FluentAssertions.Extensions;
using FluffySpoon.AspNet.EncryptWeMust.Certes;
Expand Down Expand Up @@ -149,6 +151,58 @@ public async Task OnNoValidCertificateAvailable_ShouldRenewCertificate()
await LetsEncryptClientFactory.Received(1).GetClient();
}

[TestMethod]
public async Task CheckAllChallengesValidated()
{
// arrange

var PemCert = CertToPem(((LetsEncryptX509Certificate)ValidCert).GetCertificate());
var certChain = new CertificateChain(PemCert);
var readyOrder = new Order {
Status = OrderStatus.Ready,
Identifiers = new[] { new Identifier { Value = "example.com" } }
};
var validOrder = new Order { Status = OrderStatus.Valid };
var orderContext = Substitute.For<IOrderContext>();
orderContext.Resource().Returns(readyOrder);
orderContext.Finalize(default).ReturnsForAnyArgs(validOrder);
orderContext.Download().Returns(certChain);

var validChallenge = new Challenge { Status = ChallengeStatus.Valid };
var pendingChallenge = new Challenge { Status = ChallengeStatus.Pending };
var challenge1 = Substitute.For<IChallengeContext>();
challenge1.Validate().Returns(validChallenge);
challenge1.Resource().Returns(validChallenge);
var challenge2 = Substitute.For<IChallengeContext>();
challenge2.Validate().Returns(pendingChallenge);
challenge2.Resource().Returns(validChallenge);

var placedOrder = new PlacedOrder(null, orderContext, new[] { challenge1, challenge2 });

var options = new LetsEncryptOptions { CertificateSigningRequest = new CsrInfo() };

var client = new LetsEncryptClient(null, options, NullLogger.Instance);

// act

var result = await client.FinalizeOrder(placedOrder);

// assert

var cert = new LetsEncryptX509Certificate(result.Bytes);
PemCert.Should().Be(CertToPem(cert.GetCertificate()));
await challenge1.Received().Validate();
await challenge2.Received().Validate();
await challenge2.Received().Resource();
}

private static string CertToPem(X509Certificate2 cert)
{
return string.Concat("-----BEGIN CERTIFICATE-----\n",
Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks),
"\n-----END CERTIFICATE-----");
}

private static T[] SeqEq<T>(T[] xs) => Arg.Is<T[]>(ys => xs.SequenceEqual(ys));
private static T RefEq<T>(T it) => Arg.Is<T>(x => ReferenceEquals(x, it));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ private static async Task<Challenge[]> ValidateChallengesAsync(IChallengeContext

while (true)
{
var anyValid = challenges.Any(x => x.Status == ChallengeStatus.Valid);
var allInvalid = challenges.All(x => x.Status == ChallengeStatus.Invalid);
var allValid = challenges.All(x => x.Status == ChallengeStatus.Valid);
var anyInvalid = challenges.Any(x => x.Status == ChallengeStatus.Invalid);

if (anyValid || allInvalid)
if (allValid || anyInvalid)
break;

await Task.Delay(1000);
Expand Down

0 comments on commit cf9b452

Please sign in to comment.