feat: implement read and write methods for json file storage provider
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace StevanFreeborn.Extensions.Configuration.Secure.Sample;
|
||||
|
||||
public sealed record ApiOptions
|
||||
{
|
||||
public string ApiKey { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using StevanFreeborn.Extensions.Configuration.Secure.Sample;
|
||||
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder();
|
||||
|
||||
const string configFileName = "appsettings.json";
|
||||
var opts = new JsonStorageOptions()
|
||||
{
|
||||
FileName = configFileName,
|
||||
};
|
||||
|
||||
var storageProvider = new JsonFileStorageProvider(opts);
|
||||
|
||||
builder.Configuration.AddSecureConfig(storageProvider);
|
||||
|
||||
builder.Services.Configure<ApiOptions>(
|
||||
builder.Configuration.GetSection(nameof(ApiOptions))
|
||||
);
|
||||
|
||||
builder.Services.AddSecureConfig()
|
||||
.UseJsonFileStorage(opt => opt.FileName = configFileName);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
var options = app.Services.GetRequiredService<IOptions<ApiOptions>>();
|
||||
var secureConfig = app.Services.GetRequiredService<ISecureConfig>();
|
||||
|
||||
Console.WriteLine(options.Value);
|
||||
|
||||
await secureConfig.SetAsync(
|
||||
nameof(ApiOptions),
|
||||
new ApiOptions { ApiKey = "apiKey" }
|
||||
);
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net11.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StevanFreeborn.Extensions.Configuration.Secure\StevanFreeborn.Extensions.Configuration.Secure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Json" Version="10.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace StevanFreeborn.Extensions.Configuration.Secure.Storage;
|
||||
|
||||
public sealed class JsonFileStorageProvider(JsonStorageOptions options)
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
private readonly JsonStorageOptions _options = options
|
||||
?? throw new ArgumentNullException(nameof(options));
|
||||
|
||||
public async Task<string> ReadAsync(string key, CancellationToken ct = default)
|
||||
{
|
||||
if (File.Exists(_options.FullPath) is false)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
using var stream = new FileStream(_options.FullPath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
if (stream.Length is 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var data = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, cancellationToken: ct);
|
||||
|
||||
if (data is not null && data.TryGetValue(key, out var v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task WriteAsync(string key, string encryptedData, CancellationToken ct = default)
|
||||
{
|
||||
Directory.CreateDirectory(_options.DirectoryPath);
|
||||
|
||||
var data = new Dictionary<string, string>
|
||||
{
|
||||
[key] = encryptedData
|
||||
};
|
||||
|
||||
using var stream = new FileStream(
|
||||
_options.FullPath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None
|
||||
);
|
||||
|
||||
await JsonSerializer.SerializeAsync(stream, data, JsonOptions, ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace StevanFreeborn.Extensions.Configuration.Secure.Storage;
|
||||
|
||||
public sealed class JsonStorageOptions
|
||||
{
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
public string DirectoryPath { get; set; } = AppContext.BaseDirectory;
|
||||
public string FullPath => Path.Combine(DirectoryPath, FileName);
|
||||
}
|
||||
Reference in New Issue
Block a user