feat: implement secure config and machine id key generator
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
|
||||
using StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
|
||||
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
|
||||
|
||||
@@ -5,7 +7,7 @@ namespace StevanFreeborn.Extensions.Configuration.Secure.Configuration;
|
||||
|
||||
internal interface ISecureConfig
|
||||
{
|
||||
Task DeleteAsync<t>(string key, CancellationToken ct = default);
|
||||
Task<bool> DeleteAsync(string key, CancellationToken ct = default);
|
||||
Task<T?> GetAsync<T>(string key, CancellationToken ct = default);
|
||||
Task SetAsync<T>(string key, T value, CancellationToken ct = default);
|
||||
}
|
||||
@@ -23,16 +25,42 @@ internal sealed class SecureConfig(
|
||||
|
||||
public async Task SetAsync<T>(string key, T value, CancellationToken ct = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(value);
|
||||
var encryptedValue = _cryptoProvider.Encrypt(json);
|
||||
|
||||
await _storageProvider.WriteAsync(key, encryptedValue, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<T?> GetAsync<T>(string key, CancellationToken ct = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
var encryptedData = await _storageProvider.ReadAsync(key, ct).ConfigureAwait(false);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(encryptedData))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var data = _cryptoProvider.Decrypt(encryptedData);
|
||||
return JsonSerializer.Deserialize<T>(data);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<t>(string key, CancellationToken ct = default)
|
||||
public Task<bool> DeleteAsync(string key, CancellationToken ct = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return _storageProvider.DeleteAsync(key, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user