Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed May 29, 2018
1 parent c3932e4 commit 6e5d6d9
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 51 deletions.
4 changes: 4 additions & 0 deletions LetsEncrypt.sln.licenseheader
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extensions: designer.cs generated.cs
extensions: .cs
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
9 changes: 1 addition & 8 deletions samples/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Web
{
Expand Down
7 changes: 1 addition & 6 deletions samples/Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Web
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
Expand Down
27 changes: 15 additions & 12 deletions src/McMaster.AspNetCore.LetsEncrypt/Internal/CertificateFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
Expand All @@ -18,7 +21,7 @@ internal class CertificateFactory : IDisposable
private readonly IOptions<LetsEncryptOptions> _options;
private readonly IHttpChallengeResponseStore _challengeStore;
private readonly ILogger _logger;
private readonly AcmeClient client;
private readonly AcmeClient _client;

public CertificateFactory(IOptions<LetsEncryptOptions> options,
IHttpChallengeResponseStore challengeStore,
Expand All @@ -27,7 +30,7 @@ public CertificateFactory(IOptions<LetsEncryptOptions> options,
_options = options;
_challengeStore = challengeStore;
_logger = logger;
client = new AcmeClient(_options.Value.AcmeServer);
_client = new AcmeClient(_options.Value.AcmeServer);
}

public async Task RegisterUserAsync(CancellationToken cancellationToken)
Expand All @@ -36,7 +39,7 @@ public async Task RegisterUserAsync(CancellationToken cancellationToken)
var registration = "mailto:" + options.EmailAddress;

_logger.LogInformation("Creating certificate registration for {registration}", registration);
var account = await client.NewRegistraton(registration);
var account = await _client.NewRegistraton(registration);
_logger.LogResponse("NewRegistration", account);

var tosUri = account.GetTermsOfServiceUri();
Expand All @@ -45,7 +48,7 @@ public async Task RegisterUserAsync(CancellationToken cancellationToken)

cancellationToken.ThrowIfCancellationRequested();
_logger.LogDebug("Accepting the terms of service");
account = await client.UpdateRegistration(account);
account = await _client.UpdateRegistration(account);
_logger.LogResponse("UpdateRegistration", account);
}

Expand Down Expand Up @@ -93,7 +96,7 @@ private async Task ValidateDomainOwnershipAsync(string hostName, CancellationTok
cancellationToken.ThrowIfCancellationRequested();

_logger.LogDebug("Requesting authorization to create certificates for {hostname}", hostName);
var auth = await client.NewAuthorization(new AuthorizationIdentifier
var auth = await _client.NewAuthorization(new AuthorizationIdentifier
{
Type = AuthorizationIdentifierTypes.Dns,
Value = hostName,
Expand All @@ -109,14 +112,14 @@ private async Task ValidateDomainOwnershipAsync(string hostName, CancellationTok
throw new InvalidOperationException($"Did not receive challenge information for challenge type {ChallengeTypes.Http01}");
}

var keyAuth = client.ComputeKeyAuthorization(httpChallenge);
var keyAuth = _client.ComputeKeyAuthorization(httpChallenge);
_challengeStore.AddChallengeResponse(httpChallenge.Token, keyAuth);

cancellationToken.ThrowIfCancellationRequested();

_logger.LogDebug("Requesting completion of challenge to prove ownership of {hostname}", hostName);

var challengeCompletion = await client.CompleteChallenge(httpChallenge);
var challengeCompletion = await _client.CompleteChallenge(httpChallenge);

_logger.LogResponse("CompleteChallenge", challengeCompletion);

Expand All @@ -131,7 +134,7 @@ private async Task ValidateDomainOwnershipAsync(string hostName, CancellationTok

cancellationToken.ThrowIfCancellationRequested();

authorization = await client.GetAuthorization(challengeCompletion.Location);
authorization = await _client.GetAuthorization(challengeCompletion.Location);

_logger.LogResponse("GetAuthorization", authorization);

Expand All @@ -158,7 +161,7 @@ private async Task ValidateDomainOwnershipAsync(string hostName, CancellationTok

private Exception InvalidAuthorizationError(string hostName, AcmeResult<AuthorizationEntity> authorization)
{
string reason = "unknown";
var reason = "unknown";
try
{
var errorStub = new { error = new { type = "", detail = "", status = -1 } };
Expand All @@ -185,7 +188,7 @@ private async Task<X509Certificate2> CompleteCertificateRequestAsync(string host

_logger.LogInformation("Sending certifcate request for '{dn}'", dn);

var cert = await client.NewCertificate(csr);
var cert = await _client.NewCertificate(csr);

_logger.LogResponse("NewCertificate", cert);

Expand All @@ -195,7 +198,7 @@ private async Task<X509Certificate2> CompleteCertificateRequestAsync(string host

public void Dispose()
{
client.Dispose();
_client.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Connections;
Expand All @@ -12,11 +15,11 @@ internal class CertificateSelector
{
private ConcurrentDictionary<string, X509Certificate2> _certs = new ConcurrentDictionary<string, X509Certificate2>(StringComparer.OrdinalIgnoreCase);

private readonly IOptions<LetsEncryptOptions> options;
private readonly IOptions<LetsEncryptOptions> _options;

public CertificateSelector(IOptions<LetsEncryptOptions> options)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public void Use(string hostName, X509Certificate2 certificate)
Expand All @@ -28,7 +31,7 @@ public X509Certificate2 Select(ConnectionContext features, string hostName)
{
if (!_certs.TryGetValue(hostName, out var retVal))
{
return options.Value.FallbackCertificate;
return _options.Value.FallbackCertificate;
}

return retVal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace McMaster.AspNetCore.LetsEncrypt.Internal
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Threading.Tasks;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Security.Cryptography.X509Certificates;

namespace McMaster.AspNetCore.LetsEncrypt.Internal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace McMaster.AspNetCore.LetsEncrypt.Internal
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace McMaster.AspNetCore.LetsEncrypt.Internal
{
internal interface IHttpChallengeResponseStore
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Concurrent;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Concurrent;

namespace McMaster.AspNetCore.LetsEncrypt.Internal
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Options;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Certes.Acme;
using Microsoft.Extensions.Logging;

Expand Down
5 changes: 3 additions & 2 deletions src/McMaster.AspNetCore.LetsEncrypt/Internal/X509CertStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using McMaster.AspNetCore.LetsEncrypt.Internal;
using Microsoft.AspNetCore.Builder;

Expand Down
8 changes: 4 additions & 4 deletions src/McMaster.AspNetCore.LetsEncrypt/LetsEncryptOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Security.Cryptography.X509Certificates;
using Certes.Acme;
Expand Down Expand Up @@ -51,12 +54,9 @@ public string[] HostNames
public bool UseStagingServer
{
get => AcmeServer == WellKnownServers.LetsEncryptStaging;
set
{
AcmeServer = value
set => AcmeServer = value
? WellKnownServers.LetsEncryptStaging
: WellKnownServers.LetsEncrypt;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using McMaster.AspNetCore.LetsEncrypt;
using McMaster.AspNetCore.LetsEncrypt.Internal;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using McMaster.AspNetCore.LetsEncrypt;
using McMaster.AspNetCore.LetsEncrypt.Internal;
using Microsoft.AspNetCore.LetsEncrypt;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Hosting
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>true</IsPackable>
Expand All @@ -13,8 +13,8 @@

<ItemGroup>
<PackageReference Include="Certes" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0-rc1-final" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 6e5d6d9

Please sign in to comment.