feat(RSACryptographer) Init-Methode zur Verwaltung des pem-Importprozesses hinzugefügt

This commit is contained in:
Developer 02 2024-12-06 15:12:21 +01:00
parent 0ff89b4906
commit bea57a25e8
2 changed files with 16 additions and 27 deletions

View File

@ -1,8 +1,9 @@
using System.Security.Cryptography;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSACryptographer
public interface IRSACryptographer : IJsonOnDeserialized
{
public string Pem { get; init; }
@ -11,5 +12,7 @@ namespace DigitalData.Core.Abstractions.Security
public string Issuer { get; init; }
public string Audience { get; init; }
public void Init();
}
}

View File

@ -10,28 +10,9 @@ namespace DigitalData.Core.Security.Cryptographer
private string? _pemPath;
public virtual string Pem
{
get => _pem!;
init
{
ValidatePemInit();
_pem = value;
}
}
public virtual string Pem { get => _pem; init => _pem = value; }
public string? PemPath
{
get => _pemPath;
init
{
_pemPath = value;
if (value is null)
return;
ValidatePemInit();
_pem = File.ReadAllText(value);
}
}
public string? PemPath { get => _pemPath; init => _pemPath = value; }
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
@ -45,14 +26,19 @@ namespace DigitalData.Core.Security.Cryptographer
public void OnDeserialized()
{
if (Pem is null)
throw new InvalidOperationException($"Pem must be initialized. Issuer: {Issuer} and Audience: {Audience}");
Init();
}
private void ValidatePemInit()
// TODO: make file read asynchronous, consider multiple routing
public virtual void Init()
{
if (_pem is not null)
throw new InvalidOperationException($"Pem can only be initilized once. Remove one of the Pem or Pem file initilizations. Issuer: {Issuer} and Audience: {Audience}");
if(_pem is null)
{
if (File.Exists(PemPath))
_pem = File.ReadAllText(PemPath);
else
throw new FileNotFoundException($"Pem is not assigned. Furthermore Pem file is not found in {PemPath}. Issuer is {Issuer} and audience {Audience}.");
}
}
}
}