128 lines
6.0 KiB
C#
128 lines
6.0 KiB
C#
namespace SlpModularCms.Modules.Contact.Config;
|
|
|
|
/// <summary>
|
|
/// Per-host configuration of the contact module (section <c>Contact</c>).
|
|
/// </summary>
|
|
public class ContactOptions
|
|
{
|
|
public const string SectionName = "Contact";
|
|
|
|
/// <summary>
|
|
/// Master switch. While false the contact routes answer 404 and the module touches nothing.
|
|
/// It must only be switched on for a public host once per-client rate limiting is in place
|
|
/// (see <see cref="Services.ContactRateLimitPartition"/>).
|
|
/// </summary>
|
|
public bool Enabled { get; set; }
|
|
|
|
/// <summary>Identifies the site on stored rows and in the token purpose. Never taken from a request.</summary>
|
|
public string? SiteId { get; set; }
|
|
|
|
/// <summary>Minimum seconds between issuing the token and submitting; faster submissions are treated as spam.</summary>
|
|
public int MinSeconds { get; set; } = 3;
|
|
|
|
public int TokenLifetimeMinutes { get; set; } = 120;
|
|
|
|
/// <summary>Months a genuine message is kept (fixed per row at insert).</summary>
|
|
public int RetentionMonths { get; set; } = 12;
|
|
|
|
/// <summary>Days a message classified as spam is kept.</summary>
|
|
public int SpamRetentionDays { get; set; } = 30;
|
|
|
|
/// <summary>Per site and UTC day; beyond it spam is answered normally but not stored.</summary>
|
|
public int MaxSpamRowsPerDay { get; set; } = 200;
|
|
|
|
public int MaxBodyBytes { get; set; } = 16384;
|
|
|
|
/// <summary>Base of the validation problem <c>type</c> URI. When empty the type is <c>about:blank</c>.</summary>
|
|
public string? ProblemTypeBase { get; set; }
|
|
|
|
public ContactCaptchaOptions Captcha { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// Where the notification mail for a genuine message goes. Supply it through the environment
|
|
/// (<c>Contact__RecipientEmail</c>); it is never part of committed configuration and never exposed to a client.
|
|
/// </summary>
|
|
public string? RecipientEmail { get; set; }
|
|
|
|
public ContactMailOptions Mail { get; set; } = new();
|
|
|
|
public string EffectiveSiteId => string.IsNullOrWhiteSpace(SiteId) ? "default" : SiteId.Trim();
|
|
}
|
|
|
|
/// <summary>How the background sender works (section <c>Contact:Mail</c>).</summary>
|
|
public class ContactMailOptions
|
|
{
|
|
/// <summary>How often the sender looks for mail that is due, besides being woken by a new message.</summary>
|
|
public int SweepSeconds { get; set; } = 120;
|
|
|
|
/// <summary>Automatic attempts per message; after that it stays failed until an administrator resends.</summary>
|
|
public int MaxAttempts { get; set; } = 6;
|
|
|
|
/// <summary>Wait before attempt n+1 after attempt n failed (the last value repeats). Default 5 min, 15 min, 1 h, 6 h, 24 h.</summary>
|
|
public int[] BackoffMinutes { get; set; } = [5, 15, 60, 360, 1440];
|
|
|
|
/// <summary>A claimed message is not claimed again by another instance for this long.</summary>
|
|
public int ClaimMinutes { get; set; } = 5;
|
|
|
|
public int BatchSize { get; set; } = 20;
|
|
|
|
public TimeSpan BackoffAfter(int attempts)
|
|
{
|
|
var steps = BackoffMinutes is { Length: > 0 } ? BackoffMinutes : [5];
|
|
return TimeSpan.FromMinutes(Math.Max(1, steps[Math.Clamp(attempts - 1, 0, steps.Length - 1)]));
|
|
}
|
|
}
|
|
|
|
/// <summary>Start-up validation: only checks anything while the module and the captcha are switched on.</summary>
|
|
public sealed class ContactOptionsValidator : Microsoft.Extensions.Options.IValidateOptions<ContactOptions>
|
|
{
|
|
public Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, ContactOptions options)
|
|
{
|
|
if (!options.Enabled) return Microsoft.Extensions.Options.ValidateOptionsResult.Success;
|
|
var problems = options.Captcha.Problems();
|
|
return problems.Count == 0
|
|
? Microsoft.Extensions.Options.ValidateOptionsResult.Success
|
|
: Microsoft.Extensions.Options.ValidateOptionsResult.Fail(problems);
|
|
}
|
|
}
|
|
|
|
public class ContactCaptchaOptions
|
|
{
|
|
public const string FriendlyCaptcha = "friendlycaptcha";
|
|
public const string DefaultVerifyUrl = "https://global.frcapi.com/api/v2/captcha/siteverify";
|
|
|
|
public bool Enabled { get; set; }
|
|
|
|
/// <summary>The captcha provider; the only supported value is <c>friendlycaptcha</c> (Friendly Captcha v2).</summary>
|
|
public string? Provider { get; set; }
|
|
|
|
/// <summary>Public site key; it is also sent to the browser in the token response.</summary>
|
|
public string? SiteKey { get; set; }
|
|
|
|
/// <summary>Server-side API key; supply it through the environment (<c>Contact__Captcha__Secret</c>) or a secrets manager, never committed.</summary>
|
|
public string? Secret { get; set; }
|
|
|
|
/// <summary>Verify endpoint. Global by default; for the EU-only endpoint use <c>https://eu.frcapi.com/api/v2/captcha/siteverify</c>.</summary>
|
|
public string VerifyUrl { get; set; } = DefaultVerifyUrl;
|
|
|
|
/// <summary>A provider that does not answer within this time counts as passed (fail-open).</summary>
|
|
public int TimeoutSeconds { get; set; } = 3;
|
|
|
|
public bool IsFriendlyCaptcha =>
|
|
string.Equals(Provider?.Trim(), FriendlyCaptcha, StringComparison.OrdinalIgnoreCase);
|
|
|
|
/// <summary>The problems that stop the host from starting while the captcha is on (setting names only, never values).</summary>
|
|
public IReadOnlyList<string> Problems()
|
|
{
|
|
if (!Enabled) return [];
|
|
var problems = new List<string>();
|
|
if (!IsFriendlyCaptcha) problems.Add("Contact:Captcha:Provider must be 'friendlycaptcha'.");
|
|
if (string.IsNullOrWhiteSpace(SiteKey)) problems.Add("Contact:Captcha:SiteKey is required while the captcha is enabled.");
|
|
if (string.IsNullOrWhiteSpace(Secret)) problems.Add("Contact:Captcha:Secret is required while the captcha is enabled (environment variable Contact__Captcha__Secret).");
|
|
if (!Uri.TryCreate(VerifyUrl, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
|
|
problems.Add("Contact:Captcha:VerifyUrl must be an absolute https URL.");
|
|
if (TimeoutSeconds is < 1 or > 30) problems.Add("Contact:Captcha:TimeoutSeconds must be between 1 and 30.");
|
|
return problems;
|
|
}
|
|
}
|