63 lines
2.4 KiB
C#
63 lines
2.4 KiB
C#
using DigitalData.Core.Security.Cryptographer;
|
|
|
|
namespace DigitalData.Core.Security.Config
|
|
{
|
|
public class AsymCryptParams : RSAFactoryParams
|
|
{
|
|
public string PemDirectory { get; init; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Represents the separator used to concatenate the components of a token string.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The resulting token string is constructed as follows:
|
|
/// <c>string.Join(Separator, Issuer, Audience, Secret_version)</c>.
|
|
/// If <c>Secret_version</c> is not null, it will be included in the concatenation.
|
|
/// </remarks>
|
|
/// <example>
|
|
/// For example, if <c>Separator = "_-_"</c>, the output might look like:
|
|
/// <c>"Issuer_-_Audience_-_Secret_version"</c>.
|
|
/// </example>
|
|
public string Separator { get; init; } = "_-_";
|
|
|
|
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
|
|
|
|
public override void OnDeserialized()
|
|
{
|
|
base.OnDeserialized();
|
|
|
|
// Create root folder if it does not exist
|
|
if (!Directory.Exists(PemDirectory))
|
|
Directory.CreateDirectory(PemDirectory);
|
|
|
|
foreach (var crypt in Decryptors)
|
|
{
|
|
// set default path
|
|
if (crypt.IsPemNull)
|
|
{
|
|
var file_name_params = new List<object> { crypt.Issuer, crypt.Audience };
|
|
if (crypt.Encrypt)
|
|
file_name_params.Add(Secrets.Version);
|
|
|
|
var path = Path.Combine(PemDirectory, string.Join(Separator, file_name_params));
|
|
|
|
if (File.Exists(path))
|
|
crypt.SetPem(File.ReadAllText(path));
|
|
else
|
|
{
|
|
var pem = crypt.Encrypt
|
|
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
|
|
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
|
|
|
|
crypt.SetPem(File.ReadAllText(pem));
|
|
|
|
// Save file in background
|
|
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
|
}
|
|
}
|
|
|
|
crypt.Init();
|
|
}
|
|
}
|
|
}
|
|
} |