-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
harrison314#3 Add tests to PKCS11 data provider.
- Loading branch information
1 parent
3f08718
commit 2fcf4fc
Showing
8 changed files
with
445 additions
and
33 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
29 changes: 29 additions & 0 deletions
29
...Harrison314.EntityFrameworkCore.Encryption.Contrib/CryptoProviders/Pkcs11Data/DataInfo.cs
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,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Harrison314.EntityFrameworkCore.Encryption.Contrib.CryptoProviders.Pkcs11Data | ||
{ | ||
public struct DataInfo | ||
{ | ||
public string Id | ||
{ | ||
get; | ||
private set; | ||
} | ||
|
||
public string Label | ||
{ | ||
get; | ||
private set; | ||
} | ||
|
||
public DataInfo(string id, string label) | ||
{ | ||
this.Id = id; | ||
this.Label = label; | ||
} | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
src/src/Harrison314.EntityFrameworkCore.Encryption.Contrib/EndpointsExtensions.cs
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,104 @@ | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Builder; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Harrison314.EntityFrameworkCore.Encryption.CryptoProviders.Remote; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Harrison314.EntityFrameworkCore.Encryption.Contrib | ||
{ | ||
public static class EndpointsExtensions | ||
{ | ||
public static void MapRemoteEncryptedCryptoProvider<T>(this IEndpointRouteBuilder endpoints, string startUrl) | ||
where T : IDbContextEncryptedCryptoProvider | ||
{ | ||
endpoints.MapPost(string.Concat(startUrl.TrimEnd('/'), "/EncryptMasterKey"), async context => | ||
{ | ||
if (!context.Request.HasJsonContentType()) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType; | ||
return; | ||
} | ||
|
||
EncryptMasterKeyRequest request = await context.Request.ReadFromJsonAsync<EncryptMasterKeyRequest>(context.RequestAborted); | ||
//TODO: Validate | ||
T provider = context.RequestServices.GetRequiredService<T>(); | ||
|
||
MasterKeyData data = await provider.EncryptMasterKey(request.MasterKey, context.RequestAborted); | ||
EncryptMasterKeyResponse response = new EncryptMasterKeyResponse() | ||
{ | ||
Data = data.Data, | ||
KeyId = data.KeyId, | ||
Parameters = data.Parameters | ||
}; | ||
|
||
await context.Response.WriteAsJsonAsync<EncryptMasterKeyResponse>(response, context.RequestAborted); | ||
context.Response.StatusCode = 200; | ||
|
||
//TODO: Error handling | ||
}); | ||
//TODO: additional actions | ||
|
||
endpoints.MapPost(string.Concat(startUrl.TrimEnd('/'), "/FilterAcceptKeyIds"), async context => | ||
{ | ||
if (!context.Request.HasJsonContentType()) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType; | ||
return; | ||
} | ||
|
||
FilterAcceptKeyIdsRequest request = await context.Request.ReadFromJsonAsync<FilterAcceptKeyIdsRequest>(context.RequestAborted); | ||
//TODO: Validate | ||
T provider = context.RequestServices.GetRequiredService<T>(); | ||
|
||
string selectedKeyId = await provider.FilterAcceptKeyIds(request.KeyIds, context.RequestAborted); | ||
FilterAcceptKeyIdsResponse response = new FilterAcceptKeyIdsResponse() | ||
{ | ||
SelectedKeyId = selectedKeyId | ||
}; | ||
|
||
await context.Response.WriteAsJsonAsync<FilterAcceptKeyIdsResponse>(response, context.RequestAborted); | ||
context.Response.StatusCode = 200; | ||
|
||
//TODO: Error handling | ||
}); | ||
//TODO: additional actions | ||
|
||
endpoints.MapPost(string.Concat(startUrl.TrimEnd('/'), "/DecryptMasterKey"), async context => | ||
{ | ||
if (!context.Request.HasJsonContentType()) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType; | ||
return; | ||
} | ||
|
||
DecryptMasterKeyRequest request = await context.Request.ReadFromJsonAsync<DecryptMasterKeyRequest>(context.RequestAborted); | ||
//TODO: Validate | ||
T provider = context.RequestServices.GetRequiredService<T>(); | ||
|
||
MasterKeyData data = new MasterKeyData() | ||
{ | ||
Data = request.Data, | ||
KeyId = request.KeyId, | ||
Parameters = request.Parameters | ||
}; | ||
|
||
byte[] masterKey = await provider.DecryptMasterKey(data, context.RequestAborted); | ||
DecryptMasterKeyResponse response = new DecryptMasterKeyResponse() | ||
{ | ||
MasterKey = masterKey | ||
}; | ||
|
||
await context.Response.WriteAsJsonAsync<DecryptMasterKeyResponse>(response, context.RequestAborted); | ||
context.Response.StatusCode = 200; | ||
|
||
//TODO: Error handling | ||
}); | ||
//TODO: additional actions | ||
} | ||
} | ||
} |
Oops, something went wrong.