From bea57a25e89ea1ea6b3a1a7ce88e96bc976fc544 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 6 Dec 2024 15:12:21 +0100 Subject: [PATCH] =?UTF-8?q?feat(RSACryptographer)=20Init-Methode=20zur=20V?= =?UTF-8?q?erwaltung=20des=20pem-Importprozesses=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/IRSACryptographer.cs | 5 ++- .../Cryptographer/RSACryptographer.cs | 38 ++++++------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs index 9bbeb98..4472344 100644 --- a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs +++ b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs @@ -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(); } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs index 3e08d47..06e53be 100644 --- a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs +++ b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs @@ -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}."); + } } } } \ No newline at end of file