Skip to content

Commit

Permalink
Move DataProtection and Extensions.Identity to netstandard2.0/2.1 (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK authored Jun 21, 2019
1 parent 655c29b commit f35564b
Show file tree
Hide file tree
Showing 48 changed files with 896 additions and 76 deletions.
3 changes: 3 additions & 0 deletions eng/SharedFramework.External.props
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
This references are part of Microsoft.NETCore.App, so are listed here as references to be used during compilation only.
-->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
<_CompilationOnlyReference Include="Microsoft.Win32.Registry" />
<_CompilationOnlyReference Include="System.Security.Cryptography.Cng" />
<_CompilationOnlyReference Include="System.Security.Principal.Windows" />
<_CompilationOnlyReference Include="System.Buffers" />
<_CompilationOnlyReference Include="System.ComponentModel.Annotations" />
<_CompilationOnlyReference Include="System.Runtime.CompilerServices.Unsafe" />
Expand Down
14 changes: 7 additions & 7 deletions eng/SharedFramework.Local.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
<Project>
<ItemGroup>
<!-- These assemblies are available as both a NuGet package and in the shared framework -->
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Cryptography.Internal" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection.Extensions" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Http.Features" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Metadata" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.Extensions.Identity.Core" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.Extensions.Identity.Stores" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Connections.Abstractions" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Authorization" />
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Http.Connections.Common" />
Expand All @@ -20,11 +27,6 @@

<!-- These assemblies are only in the shared framework -->
<AspNetCoreAppReference Include="Microsoft.AspNetCore" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Cryptography.Internal" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection.Extensions" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Antiforgery" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Hosting" />
Expand All @@ -40,8 +42,6 @@
<AspNetCoreAppReference Include="Microsoft.AspNetCore.WebUtilities" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Html.Abstractions" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Identity" />
<AspNetCoreAppReference Include="Microsoft.Extensions.Identity.Core" />
<AspNetCoreAppReference Include="Microsoft.Extensions.Identity.Stores" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.HttpSys" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.IISIntegration" />
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.IIS" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.Abstractions.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.Abstractions.netstandard2.0.cs" />

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
Commonly used types:
Microsoft.AspNetCore.DataProtection.IDataProtectionProvider
Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<IsShippingPackage>true</IsShippingPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault.netstandard2.0.cs" />
<Reference Include="Microsoft.AspNetCore.DataProtection" />
<Reference Include="Microsoft.Azure.KeyVault" />
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Microsoft Azure KeyVault key encryption support.</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection;azure;keyvault</PackageTags>
<IsShippingPackage>true</IsShippingPackage>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureStorage.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureStorage.netstandard2.0.cs" />
<Reference Include="Microsoft.AspNetCore.DataProtection" />
<Reference Include="Microsoft.Azure.Storage.Blob" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Microsoft Azure Blob storage support as key store.</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection;azure;blob</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.Cryptography.Internal.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.Cryptography.Internal.netstandard2.0.cs" />

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<Description>Infrastructure for ASP.NET Core cryptographic packages. Applications and libraries should not reference this package directly.</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<IsShippingPackage>true</IsShippingPackage>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netstandard2.0.cs" />
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netcoreapp2.0.cs" />
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Cryptography.KeyDerivation
{
public static partial class KeyDerivation
{
public static byte[] Pbkdf2(string password, byte[] salt, Microsoft.AspNetCore.Cryptography.KeyDerivation.KeyDerivationPrf prf, int iterationCount, int numBytesRequested) { throw null; }
}
public enum KeyDerivationPrf
{
HMACSHA1 = 0,
HMACSHA256 = 1,
HMACSHA512 = 2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<Description>ASP.NET Core utilities for key derivation.</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netcoreapp2.0</TargetFrameworks>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<IsShippingPackage>true</IsShippingPackage>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#if NETCOREAPP2_0
using System;
using System.Diagnostics;
using System.Security.Cryptography;
Expand Down Expand Up @@ -61,3 +62,4 @@ private static byte[] DeriveKeyImpl(string password, byte[] salt, KeyDerivationP
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ private static IPbkdf2Provider GetPbkdf2Provider()
}
else
{
#if NETSTANDARD2_0
return new ManagedPbkdf2Provider();
#elif NETCOREAPP2_0
// fastest implementation on .NET Core for Linux/macOS.
// Not supported on .NET Framework
return new NetCorePbkdf2Provider();
#else
#error Update target frameworks
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!-- This file is automatically generated. -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.netcoreapp3.0.cs" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="Microsoft.AspNetCore.DataProtection.netstandard2.0.cs" />
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
<Reference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
<Reference Include="Microsoft.Extensions.Options" />
<Reference Include="Microsoft.Win32.Registry" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -77,7 +77,7 @@ private static void AddDataProtectionServices(IServiceCollection services)

services.TryAddSingleton<IKeyManager, XmlKeyManager>();
services.TryAddSingleton<IApplicationDiscriminator, HostingApplicationDiscriminator>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, DataProtectionStartupFilter>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, DataProtectionHostedService>());

// Internal services
services.TryAddSingleton<IDefaultKeyResolver, DefaultKeyResolver>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// 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 System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.AspNetCore.DataProtection.Internal
{
internal class DataProtectionStartupFilter : IStartupFilter
internal class DataProtectionHostedService : IHostedService
{
private readonly IKeyRingProvider _keyRingProvider;
private readonly ILogger<DataProtectionStartupFilter> _logger;
private readonly ILogger<DataProtectionHostedService> _logger;

public DataProtectionStartupFilter(IKeyRingProvider keyRingProvider)
public DataProtectionHostedService(IKeyRingProvider keyRingProvider)
: this(keyRingProvider, NullLoggerFactory.Instance)
{ }

public DataProtectionStartupFilter(IKeyRingProvider keyRingProvider, ILoggerFactory loggerFactory)
public DataProtectionHostedService(IKeyRingProvider keyRingProvider, ILoggerFactory loggerFactory)
{
_keyRingProvider = keyRingProvider;
_logger = loggerFactory.CreateLogger<DataProtectionStartupFilter>();
_logger = loggerFactory.CreateLogger<DataProtectionHostedService>();
}

public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
public Task StartAsync(CancellationToken token)
{
try
{
Expand All @@ -42,7 +43,9 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
_logger.KeyRingFailedToLoadOnStartup(ex);
}

return next;
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken token) => Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<Description>ASP.NET Core logic to protect and unprotect data, similar to DPAPI.</Description>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<IsShippingPackage>true</IsShippingPackage>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -17,13 +18,14 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
<Reference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
<Reference Include="Microsoft.Extensions.Options" />
<Reference Include="Microsoft.Win32.Registry" />
<Reference Include="System.Security.Cryptography.Xml" />
<Reference Include="System.Security.Principal.Windows" />
<SuppressBaselineReference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
</ItemGroup>

</Project>
32 changes: 30 additions & 2 deletions src/DataProtection/DataProtection/test/HostingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
Expand All @@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test
public class HostingTests
{
[Fact]
public async Task LoadsKeyRingBeforeServerStarts()
public async Task WebhostLoadsKeyRingBeforeServerStarts()
{
var tcs = new TaskCompletionSource<object>();
var mockKeyRing = new Mock<IKeyRingProvider>();
Expand All @@ -46,6 +47,33 @@ public async Task LoadsKeyRingBeforeServerStarts()
mockKeyRing.VerifyAll();
}

[Fact]
public async Task GenericHostLoadsKeyRingBeforeServerStarts()
{
var tcs = new TaskCompletionSource<object>();
var mockKeyRing = new Mock<IKeyRingProvider>();
mockKeyRing.Setup(m => m.GetCurrentKeyRing())
.Returns(Mock.Of<IKeyRing>())
.Callback(() => tcs.TrySetResult(null));

var builder = new HostBuilder()
.ConfigureServices(s =>
s.AddDataProtection()
.Services
.Replace(ServiceDescriptor.Singleton(mockKeyRing.Object))
.AddSingleton<IServer>(
new FakeServer(onStart: () => tcs.TrySetException(new InvalidOperationException("Server was started before key ring was initialized")))))
.ConfigureWebHost(b => b.UseStartup<TestStartup>());

using (var host = builder.Build())
{
await host.StartAsync();
}

await tcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10));
mockKeyRing.VerifyAll();
}

[Fact]
public async Task StartupContinuesOnFailureToLoadKey()
{
Expand Down
Loading

0 comments on commit f35564b

Please sign in to comment.