Developer 02 7da93c6719 refactor(DIExtensions): Verbesserung der Registrierung von AsymCrypt-Diensten und Vereinfachung von Overloads
- `AddAsymCryptService` aktualisiert, um eine Standardimplementierung mit `AsymCryptParams` ohne generische Typen bereitzustellen.
- Neue Überladung von `AddAsymCryptService` hinzugefügt, die eine `IConfigurationSection` für Standardparameter akzeptiert.
- Lebensdauer der Service-Registrierungen für `IAsymCryptService` von `Scoped` auf `Singleton` geändert, um Konsistenz und geringeren Overhead zu gewährleisten.
2024-12-16 09:44:51 +01:00

47 lines
1.9 KiB
C#

using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections;
namespace DigitalData.Core.Security
{
public class AsymCryptService<TAsymCryptParams> : RSAFactory<TAsymCryptParams>, IAsymCryptService<TAsymCryptParams>, IRSAFactory<TAsymCryptParams>, IEnumerable<IRSADecryptor>
where TAsymCryptParams : AsymCryptParams
{
public IEnumerable<IRSADecryptor> Decryptors => _params.Decryptors;
public IRSADecryptor this[string key]
{
get
{
var key_params = key.Split(_params.KeyNameSeparator);
if (key_params.Length != 2)
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 AsymCryptService(IOptions<TAsymCryptParams> options, ILogger<AsymCryptService<TAsymCryptParams>>? logger = null) : base(options)
{
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
}
public IEnumerator<IRSADecryptor> GetEnumerator() => Decryptors.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Decryptors.GetEnumerator();
public IEnumerable<IRSAEncryptor> Encryptors
{
get
{
foreach (var decryptor in Decryptors)
yield return decryptor.Encryptor;
}
}
}
}