refactor(AsymCryptService): Indexer und IEnumerable-Implementierung zur Vereinfachung entfernt

This commit is contained in:
Developer 02 2024-12-21 10:10:50 +01:00
parent 0dd897625a
commit a3931414e3
2 changed files with 6 additions and 37 deletions

View File

@ -1,13 +1,11 @@
namespace DigitalData.Core.Abstractions.Security namespace DigitalData.Core.Abstractions.Security
{ {
public interface IAsymCryptService : IRSAFactory, IEnumerable<IRSADecryptor> public interface IAsymCryptService : IRSAFactory
{ {
IEnumerable<IRSADecryptor> Decryptors { get; } IEnumerable<IRSADecryptor> Decryptors { get; }
IRSADecryptor Vault { get; } IRSADecryptor Vault { get; }
IRSADecryptor this[string key] { get; }
IEnumerable<IRSAEncryptor> Encryptors { get; } IEnumerable<IRSAEncryptor> Encryptors { get; }
} }
} }

View File

@ -3,11 +3,10 @@ using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer; using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Collections;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security
{ {
public class AsymCryptService : RSAFactory<AsymCryptParams>, IAsymCryptService, IRSAFactory, IEnumerable<IRSADecryptor> public class AsymCryptService : RSAFactory<AsymCryptParams>, IAsymCryptService, IRSAFactory
{ {
public IEnumerable<IRSADecryptor> Decryptors { get; } public IEnumerable<IRSADecryptor> Decryptors { get; }
@ -16,26 +15,9 @@ namespace DigitalData.Core.Security
/// </summary> /// </summary>
public IRSADecryptor Vault { get; } public IRSADecryptor Vault { get; }
public IRSADecryptor this[string key] private readonly Lazy<IEnumerable<IRSAEncryptor>> _lazyEncryptors;
{
get
{
var key_params = key.Split(_params.KeyNameSeparator);
if (key_params.Length != 2) public IEnumerable<IRSAEncryptor> Encryptors => _lazyEncryptors.Value;
throw new ArgumentException($"Invalid key format. Expected two segments separated by '{_params.KeyNameSeparator}', but received: '{key}'.", nameof(key));
return _params.Decryptors.FirstOrDefault(d => d.Issuer == key_params[0] && d.Audience == key_params[1])
?? throw new KeyNotFoundException($"No decryptor found matching the issuer '{key_params[0]}' and audience '{key_params[1]}'.");
}
}
public IRSADecryptor this[int index] => index < 0 || index >= Decryptors.Count()
? Decryptors.ElementAt(index)
: throw new ArgumentOutOfRangeException(
nameof(index),
index,
$"The index {index} is out of range. The valid indices for {GetType()} are between 0 and {Decryptors.Count() - 1} (inclusive). Please ensure the index is within this range.");
public AsymCryptService(IOptions<AsymCryptParams> options, ILogger<AsymCryptService>? logger = null) : base(options) public AsymCryptService(IOptions<AsymCryptParams> options, ILogger<AsymCryptService>? logger = null) : base(options)
{ {
@ -51,19 +33,8 @@ namespace DigitalData.Core.Security
Decryptors = _params.Decryptors; Decryptors = _params.Decryptors;
Vault = _params.Vault ?? Decryptors.First(); Vault = _params.Vault ?? Decryptors.First();
}
public IEnumerator<IRSADecryptor> GetEnumerator() => Decryptors.GetEnumerator(); _lazyEncryptors = new(Decryptors.Select(decryptor => decryptor.Encryptor));
IEnumerator IEnumerable.GetEnumerator() => Decryptors.GetEnumerator();
public IEnumerable<IRSAEncryptor> Encryptors
{
get
{
foreach (var decryptor in Decryptors)
yield return decryptor.Encryptor;
}
} }
} }
} }