Restructure project with namespace updates and renamed files for better organization. Migrate project name from FsTool to FsToolbox.Cli.

This commit is contained in:
Andre Beging
2025-12-11 19:04:18 +01:00
parent 9f82cf491c
commit 46229a6dc7
14 changed files with 319 additions and 219 deletions

View File

@@ -1,81 +1,100 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using FsTool.Tasks;
using FsToolbox.Cli.Tasks;
namespace FsTool.Helpers;
public static class AuthHelper
namespace FsToolbox.Cli.Helpers
{
private static async Task<string?> LoadCsrfTokenAsync()
/// <summary>
/// Provides helper methods to manage authentication and cached CSRF tokens.
/// </summary>
public static class AuthHelper
{
if (!File.Exists("csrf_token.json")) return null;
#region Public Method EnsureAuthenticationAsync
var json = await File.ReadAllTextAsync("csrf_token.json");
var tokenInfo = JsonSerializer.Deserialize<JsonObject>(json);
if (tokenInfo == null) return null;
var token = tokenInfo["token"]?.GetValue<string>();
var expiresAtString = tokenInfo["expiresAt"]?.GetValue<string>();
if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(expiresAtString)) return null;
if (DateTime.TryParse(expiresAtString, out var expiresAt))
/// <summary>
/// Ensures the HTTP client carries a valid CSRF token, logging in when needed.
/// </summary>
/// <param name="httpClient">The HTTP client whose headers should be updated.</param>
public static async Task EnsureAuthenticationAsync(HttpClient httpClient)
{
if (DateTime.UtcNow < expiresAt.ToUniversalTime())
return token;
// Check if header already contains a CSRF token
if (httpClient.DefaultRequestHeaders.TryGetValues("X-CSRF-Token", out var existingTokens))
{
var existingToken = existingTokens.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(existingToken))
return;
}
// Try to load CSRF token from file
var csrfToken = await LoadCsrfTokenAsync();
csrfToken = null;
// If no valid token found, call login endpoint to get a new one
if (string.IsNullOrWhiteSpace(csrfToken))
csrfToken = await UserTasks.CallLoginEndpointAsync(httpClient);
// Set CSRF token in HTTP client headers
if (!string.IsNullOrWhiteSpace(csrfToken))
{
csrfToken = csrfToken.ReplaceLineEndings(string.Empty);
httpClient.DefaultRequestHeaders.Remove("X-CSRF-Token");
httpClient.DefaultRequestHeaders.Add("X-CSRF-Token", csrfToken);
}
}
return null;
}
#endregion
internal static async Task StoreCsrfTokenAsync(string? csrfToken, DateTime expiration)
{
if (string.IsNullOrWhiteSpace(csrfToken)) return;
#region Public Method StoreCsrfTokenAsync
var tokenInfo = new
/// <summary>
/// Persists the CSRF token to the local cache file until it expires.
/// </summary>
/// <param name="csrfToken">The token to store; ignored when null or whitespace.</param>
/// <param name="expiration">The UTC expiration timestamp received from the server.</param>
public static async Task StoreCsrfTokenAsync(string? csrfToken, DateTime expiration)
{
token = csrfToken,
expiresAt = expiration
};
if (string.IsNullOrWhiteSpace(csrfToken)) return;
var json = JsonSerializer.Serialize(tokenInfo, new JsonSerializerOptions
{
WriteIndented = true
});
var tokenInfo = new
{
token = csrfToken,
expiresAt = expiration
};
await File.WriteAllTextAsync("csrf_token.json", json);
}
var json = JsonSerializer.Serialize(tokenInfo, new JsonSerializerOptions
{
WriteIndented = true
});
public static async Task EnsureAuthenticationAsync(HttpClient httpClient)
{
// Check if header already contains a CSRF token
if (httpClient.DefaultRequestHeaders.TryGetValues("X-CSRF-Token", out var existingTokens))
{
var existingToken = existingTokens.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(existingToken))
return;
await File.WriteAllTextAsync("csrf_token.json", json);
}
// Try to load CSRF token from file
var csrfToken = await LoadCsrfTokenAsync();
csrfToken = null;
// If no valid token found, call login endpoint to get a new one
if (string.IsNullOrWhiteSpace(csrfToken))
csrfToken = await UserTasks.CallLoginEndpointAsync(httpClient);
#endregion
// Set CSRF token in HTTP client headers
if (!string.IsNullOrWhiteSpace(csrfToken))
#region Private Method LoadCsrfTokenAsync
private static async Task<string?> LoadCsrfTokenAsync()
{
csrfToken = csrfToken.ReplaceLineEndings(string.Empty);
if (!File.Exists("csrf_token.json")) return null;
httpClient.DefaultRequestHeaders.Remove("X-CSRF-Token");
httpClient.DefaultRequestHeaders.Add("X-CSRF-Token", csrfToken);
var json = await File.ReadAllTextAsync("csrf_token.json");
var tokenInfo = JsonSerializer.Deserialize<JsonObject>(json);
if (tokenInfo == null) return null;
var token = tokenInfo["token"]?.GetValue<string>();
var expiresAtString = tokenInfo["expiresAt"]?.GetValue<string>();
if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(expiresAtString)) return null;
if (DateTime.TryParse(expiresAtString, out var expiresAt))
if (DateTime.UtcNow < expiresAt.ToUniversalTime())
return token;
return null;
}
#endregion
}
}
}

View File

@@ -1,7 +1,7 @@
using System.Text;
using System.Text.Json.Nodes;
namespace FsTool.Helpers
namespace FsToolbox.Cli.Helpers
{
public static class Extensions
{
@@ -38,4 +38,4 @@ namespace FsTool.Helpers
#endregion
}
}
}

View File

@@ -1,12 +1,14 @@
using Microsoft.Extensions.Configuration;
namespace FsTool.Helpers
namespace FsToolbox.Cli.Helpers
{
/// <summary>
/// Represents the root configuration settings for the application, including API and credentials.
/// </summary>
public class AppSettings
{
#region Public Properties
/// <summary>
/// Gets the API-related settings.
/// </summary>
@@ -16,6 +18,8 @@ namespace FsTool.Helpers
/// Gets the credentials settings for authentication.
/// </summary>
public CredentialsSettings Credentials { get; init; } = new();
#endregion
}
/// <summary>
@@ -23,10 +27,14 @@ namespace FsTool.Helpers
/// </summary>
public class ApiSettings
{
#region Public Properties
/// <summary>
/// Gets the base URL for the API, without the /api path segment.
/// </summary>
public string BaseUrl { get; init; } = "https://beta.foodsharing.de";
#endregion
}
/// <summary>
@@ -34,6 +42,8 @@ namespace FsTool.Helpers
/// </summary>
public class CredentialsSettings
{
#region Public Properties
/// <summary>
/// Gets the email address used for login.
/// </summary>
@@ -48,6 +58,8 @@ namespace FsTool.Helpers
/// Gets a value indicating whether two-factor authentication is enabled.
/// </summary>
public bool TwoFactorEnabled { get; init; }
#endregion
}
/// <summary>
@@ -55,23 +67,35 @@ namespace FsTool.Helpers
/// </summary>
public static class SettingsProvider
{
#region Constants
private static AppSettings? _current;
#endregion
#region Public Properties
/// <summary>
/// Gets the current application settings instance.
/// </summary>
public static AppSettings Current => _current ??= Load();
#endregion
#region Private Method Load
private static AppSettings Load()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.json", false, true)
.Build();
var settings = new AppSettings();
configuration.Bind(settings);
return settings;
}
#endregion
}
}