This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactored builder extensions and service collection extensions - Refactored Settings/Configuration/Descriptor - Removed ConfigurationCommon/AuthenticatedEncryptorConfigurationExtensions - Added IAuthenticatedEncryptorFactory and implementations - Refactored IKey to have Descriptor instead of CreateEncryptorInstance() - Handled Repository/Encryptor special logic - Added samples - Updated tests
- Loading branch information
1 parent
bf7a238
commit cde3b96
Showing
121 changed files
with
2,933 additions
and
2,166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ nuget.exe | |
project.lock.json | ||
.vs | ||
.build/ | ||
.testPublish/ | ||
.testPublish/ | ||
samples/**/temp-keys/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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.DataProtection; | ||
using Microsoft.AspNetCore.DataProtection.KeyManagement; | ||
using Microsoft.AspNetCore.DataProtection.XmlEncryption; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace CustomEncryptorSample | ||
{ | ||
public static class CustomBuilderExtensions | ||
{ | ||
public static IDataProtectionBuilder UseXmlEncryptor( | ||
this IDataProtectionBuilder builder, | ||
Func<IServiceProvider, IXmlEncryptor> factory) | ||
{ | ||
builder.Services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(serviceProvider => | ||
{ | ||
var instance = factory(serviceProvider); | ||
return new ConfigureOptions<KeyManagementOptions>(options => | ||
{ | ||
options.XmlEncryptor = instance; | ||
}); | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/CustomEncryptorSample/CustomEncryptorSample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net451;netcoreapp1.1</TargetFrameworks> | ||
<!-- TODO remove when https://github.com/dotnet/sdk/issues/396 is resolved --> | ||
<RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">win7-x64</RuntimeIdentifier> | ||
<DebugType>portable</DebugType> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection\Microsoft.AspNetCore.DataProtection.csproj" /> | ||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection.Extensions\Microsoft.AspNetCore.DataProtection.Extensions.csproj" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.2.0-*" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.2.0-*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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 System.Linq; | ||
using System.Xml.Linq; | ||
using Microsoft.AspNetCore.DataProtection.XmlEncryption; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CustomEncryptorSample | ||
{ | ||
public class CustomXmlDecryptor : IXmlDecryptor | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public CustomXmlDecryptor(IServiceProvider services) | ||
{ | ||
_logger = services.GetRequiredService<ILoggerFactory>().CreateLogger<CustomXmlDecryptor>(); | ||
} | ||
|
||
public XElement Decrypt(XElement encryptedElement) | ||
{ | ||
if (encryptedElement == null) | ||
{ | ||
throw new ArgumentNullException(nameof(encryptedElement)); | ||
} | ||
|
||
return new XElement(encryptedElement.Elements().Single()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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 System.Xml.Linq; | ||
using Microsoft.AspNetCore.DataProtection.XmlEncryption; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CustomEncryptorSample | ||
{ | ||
public class CustomXmlEncryptor : IXmlEncryptor | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public CustomXmlEncryptor(IServiceProvider services) | ||
{ | ||
_logger = services.GetRequiredService<ILoggerFactory>().CreateLogger<CustomXmlEncryptor>(); | ||
} | ||
|
||
public EncryptedXmlInfo Encrypt(XElement plaintextElement) | ||
{ | ||
if (plaintextElement == null) | ||
{ | ||
throw new ArgumentNullException(nameof(plaintextElement)); | ||
} | ||
|
||
_logger.LogInformation("Not encrypting key"); | ||
|
||
var newElement = new XElement("unencryptedKey", | ||
new XComment(" This key is not encrypted. "), | ||
new XElement(plaintextElement)); | ||
var encryptedTextElement = new EncryptedXmlInfo(newElement, typeof(CustomXmlDecryptor)); | ||
|
||
return encryptedTextElement; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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 System.IO; | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CustomEncryptorSample | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var keysFolder = Path.Combine(Directory.GetCurrentDirectory(), "temp-keys"); | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddLogging(); | ||
serviceCollection.AddDataProtection() | ||
.PersistKeysToFileSystem(new DirectoryInfo(keysFolder)) | ||
.UseXmlEncryptor(s => new CustomXmlEncryptor(s)); | ||
|
||
var services = serviceCollection.BuildServiceProvider(); | ||
var loggerFactory = services.GetRequiredService<ILoggerFactory>(); | ||
loggerFactory.AddConsole(); | ||
|
||
var protector = services.GetDataProtector("SamplePurpose"); | ||
|
||
// protect the payload | ||
var protectedPayload = protector.Protect("Hello World!"); | ||
Console.WriteLine($"Protect returned: {protectedPayload}"); | ||
|
||
// unprotect the payload | ||
var unprotectedPayload = protector.Unprotect(protectedPayload); | ||
Console.WriteLine($"Unprotect returned: {unprotectedPayload}"); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
samples/CustomEncryptorSample/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:1398/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"CustomEncryptorSample": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
Oops, something went wrong.