refactor(CryptoFactoryParams): PemFileInitalizer erstellt, um das Lesen und Aktualisieren von Pem-Dateien zu ermöglichen.
- Minimierung der di-Erweiterungsmethoden. - AfterCreate-Methode entfernt
This commit is contained in:
@@ -44,61 +44,5 @@ namespace DigitalData.Core.Security.Config
|
||||
public IEnumerable<RSATokenDescriptor> TokenDescriptors { get; init; } = new List<RSATokenDescriptor>();
|
||||
|
||||
public RSADecryptor? VaultDecryptor { get; init; }
|
||||
|
||||
public CryptoFactoryParams()
|
||||
{
|
||||
// init decryptors
|
||||
AfterCreate += () =>
|
||||
{
|
||||
// Create root folder if it does not exist
|
||||
if (!Directory.Exists(PemDirectory))
|
||||
Directory.CreateDirectory(PemDirectory);
|
||||
|
||||
var privateKeys = new List<RSAPrivateKey>();
|
||||
privateKeys.AddRange(Decryptors);
|
||||
privateKeys.AddRange(TokenDescriptors);
|
||||
if (VaultDecryptor is not null)
|
||||
privateKeys.Add(VaultDecryptor);
|
||||
|
||||
foreach (var privateKey in privateKeys)
|
||||
{
|
||||
// set default path
|
||||
if (privateKey.IsPemNull)
|
||||
{
|
||||
// file name
|
||||
var file_name_params = new List<object>();
|
||||
|
||||
if (privateKey.Id is not null)
|
||||
file_name_params.Add(privateKey.Id);
|
||||
else if (privateKey is RSATokenDescriptor descriptor)
|
||||
file_name_params.Add(descriptor.Issuer);
|
||||
|
||||
file_name_params.Add(KeySizeInBits);
|
||||
file_name_params.Add(DateTime.Now.ToTag(DateTagFormat));
|
||||
|
||||
if (privateKey.IsEncrypted)
|
||||
file_name_params.Add(Secrets.Version);
|
||||
|
||||
var file_name = $"{string.Join(FileNameSeparator, file_name_params)}.{FileExtension}";
|
||||
|
||||
var path = Path.Combine(PemDirectory, file_name);
|
||||
|
||||
if (File.Exists(path))
|
||||
privateKey.SetPem(File.ReadAllText(path));
|
||||
else
|
||||
{
|
||||
var pem = privateKey.IsEncrypted
|
||||
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
|
||||
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
|
||||
|
||||
privateKey.SetPem(pem);
|
||||
|
||||
// Save file in background
|
||||
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Core.Security.Config
|
||||
{
|
||||
public class ParamsConfigureOptions<TParams> : IConfigureOptions<TParams> where TParams : RSAFactoryParams
|
||||
{
|
||||
public void Configure(TParams options) => options.Init();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace DigitalData.Core.Security.Config
|
||||
{
|
||||
public class RSAFactoryParams : IJsonOnDeserialized
|
||||
public class RSAFactoryParams
|
||||
{
|
||||
public int KeySizeInBits { get; init; } = 2048;
|
||||
|
||||
@@ -27,33 +27,14 @@ namespace DigitalData.Core.Security.Config
|
||||
|
||||
public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY";
|
||||
|
||||
private PbeParameters? _pbeParameters;
|
||||
private readonly Lazy<PbeParameters> _lazyPbeParameters;
|
||||
|
||||
[JsonIgnore]
|
||||
public PbeParameters PbeParameters => _pbeParameters!;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a thread-safe initialization mechanism using Lazy initialization.
|
||||
/// </summary>
|
||||
private readonly Lazy<bool> _lazyInitializer;
|
||||
|
||||
public bool IsInitialized => _lazyInitializer.IsValueCreated;
|
||||
public PbeParameters PbeParameters => _lazyPbeParameters.Value;
|
||||
|
||||
public RSAFactoryParams()
|
||||
{
|
||||
_lazyInitializer = new(() =>
|
||||
{
|
||||
AfterCreate?.Invoke();
|
||||
return true;
|
||||
});
|
||||
|
||||
AfterCreate += () => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount);
|
||||
_lazyPbeParameters = new(() => new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount));
|
||||
}
|
||||
|
||||
protected event Action AfterCreate;
|
||||
|
||||
public void Init() => _ = _lazyInitializer.Value;
|
||||
|
||||
public void OnDeserialized() => Init();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user