Move CLI project into subfolder
This commit is contained in:
100
Cli/Helper/AuthHelper.cs
Normal file
100
Cli/Helper/AuthHelper.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using FsToolbox.Cli.Tasks;
|
||||
|
||||
namespace FsToolbox.Cli.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides helper methods to manage authentication and cached CSRF tokens.
|
||||
/// </summary>
|
||||
public static class AuthHelper
|
||||
{
|
||||
#region Public Method EnsureAuthenticationAsync
|
||||
|
||||
/// <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)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method StoreCsrfTokenAsync
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(csrfToken)) return;
|
||||
|
||||
var tokenInfo = new
|
||||
{
|
||||
token = csrfToken,
|
||||
expiresAt = expiration
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(tokenInfo, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
});
|
||||
|
||||
await File.WriteAllTextAsync("csrf_token.json", json);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method LoadCsrfTokenAsync
|
||||
|
||||
private static async Task<string?> LoadCsrfTokenAsync()
|
||||
{
|
||||
if (!File.Exists("csrf_token.json")) return null;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
101
Cli/Helper/Settings.cs
Normal file
101
Cli/Helper/Settings.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace FsToolbox.Cli.Helper
|
||||
{
|
||||
/// <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>
|
||||
public ApiSettings Api { get; init; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the credentials settings for authentication.
|
||||
/// </summary>
|
||||
public CredentialsSettings Credentials { get; init; } = new();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Contains settings related to the API endpoints.
|
||||
/// </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>
|
||||
/// Contains credentials and authentication settings.
|
||||
/// </summary>
|
||||
public class CredentialsSettings
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the email address used for login.
|
||||
/// </summary>
|
||||
public string Email { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the password used for login.
|
||||
/// </summary>
|
||||
public string Password { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether two-factor authentication is enabled.
|
||||
/// </summary>
|
||||
public bool TwoFactorEnabled { get; init; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides access to the current application settings loaded from configuration.
|
||||
/// </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", false, true)
|
||||
.Build();
|
||||
|
||||
var settings = new AppSettings();
|
||||
configuration.Bind(settings);
|
||||
return settings;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user