EncryptionService should be using ISecretProvider
Remove Async from method that was not Async
This commit is contained in:
parent
ccb06b260c
commit
3e5ce9d81d
6 changed files with 44 additions and 31 deletions
|
|
@ -40,11 +40,7 @@ void ConfigureBuilder(WebApplicationBuilder builder)
|
||||||
services.AddOptions<DbConfiguration>().Bind(configuration.GetSection("db"));
|
services.AddOptions<DbConfiguration>().Bind(configuration.GetSection("db"));
|
||||||
services.AddSingleton<ISecretProvider, ConfigurationSecretProvider>();
|
services.AddSingleton<ISecretProvider, ConfigurationSecretProvider>();
|
||||||
services.AddSingleton<KeyMapper>();
|
services.AddSingleton<KeyMapper>();
|
||||||
services.AddSingleton<IEncryptionService>(c =>
|
services.AddSingleton<IEncryptionService, EncryptionService>();
|
||||||
{
|
|
||||||
var configuration = c.GetRequiredService<IConfiguration>();
|
|
||||||
return new EncryptionService(configuration.GetValue<string>("Secrets:Master"));
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddValidatorsFromAssemblyContaining<RealmCreateRequestValidator>();
|
services.AddValidatorsFromAssemblyContaining<RealmCreateRequestValidator>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using IdentityShroud.Core.Contracts;
|
||||||
using IdentityShroud.Core.Services;
|
using IdentityShroud.Core.Services;
|
||||||
|
|
||||||
namespace IdentityShroud.Core.Tests.Services;
|
namespace IdentityShroud.Core.Tests.Services;
|
||||||
|
|
@ -10,7 +11,10 @@ public class EncryptionServiceTests
|
||||||
{
|
{
|
||||||
// setup
|
// setup
|
||||||
string key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32));
|
string key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32));
|
||||||
EncryptionService sut = new(key);
|
var secretProvider = Substitute.For<ISecretProvider>();
|
||||||
|
secretProvider.GetSecret("Master").Returns(key);
|
||||||
|
|
||||||
|
EncryptionService sut = new(secretProvider);
|
||||||
byte[] input = RandomNumberGenerator.GetBytes(16);
|
byte[] input = RandomNumberGenerator.GetBytes(16);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,5 @@ namespace IdentityShroud.Core.Contracts;
|
||||||
|
|
||||||
public interface ISecretProvider
|
public interface ISecretProvider
|
||||||
{
|
{
|
||||||
string GetSecretAsync(string name);
|
string GetSecret(string name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ public class ConfigurationSecretProvider(IConfiguration configuration) : ISecret
|
||||||
{
|
{
|
||||||
private readonly IConfigurationSection secrets = configuration.GetSection("secrets");
|
private readonly IConfigurationSection secrets = configuration.GetSection("secrets");
|
||||||
|
|
||||||
public string GetSecretAsync(string name)
|
public string GetSecret(string name)
|
||||||
{
|
{
|
||||||
return secrets.GetValue<string>(name) ?? "";
|
return secrets.GetValue<string>(name) ?? "";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
IdentityShroud.Core/Services/EncryptionService.cs
Normal file
36
IdentityShroud.Core/Services/EncryptionService.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
using IdentityShroud.Core.Contracts;
|
||||||
|
using IdentityShroud.Core.Security;
|
||||||
|
|
||||||
|
namespace IdentityShroud.Core.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public class EncryptionService : IEncryptionService
|
||||||
|
{
|
||||||
|
private readonly byte[] encryptionKey;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For easier usage in
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="encryptionKey">Encryption key as base64, must be 32 bytes</param>
|
||||||
|
// public EncryptionService(string keyBase64)
|
||||||
|
// {
|
||||||
|
// encryptionKey = Convert.FromBase64String(keyBase64);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public EncryptionService(ISecretProvider secretProvider)
|
||||||
|
{
|
||||||
|
encryptionKey = Convert.FromBase64String(secretProvider.GetSecret("Master"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Encrypt(byte[] plain)
|
||||||
|
{
|
||||||
|
return AesGcmHelper.EncryptAesGcm(plain, encryptionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Decrypt(byte[] cipher)
|
||||||
|
{
|
||||||
|
return AesGcmHelper.DecryptAesGcm(cipher, encryptionKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
using IdentityShroud.Core.Contracts;
|
|
||||||
using IdentityShroud.Core.Security;
|
|
||||||
|
|
||||||
namespace IdentityShroud.Core.Services;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="encryptionKey">Encryption key as base64, must be 32 bytes</param>
|
|
||||||
public class EncryptionService(string keyBase64) : IEncryptionService
|
|
||||||
{
|
|
||||||
private readonly byte[] encryptionKey = Convert.FromBase64String(keyBase64);
|
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] plain)
|
|
||||||
{
|
|
||||||
return AesGcmHelper.EncryptAesGcm(plain, encryptionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Decrypt(byte[] cipher)
|
|
||||||
{
|
|
||||||
return AesGcmHelper.DecryptAesGcm(cipher, encryptionKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue