namespace SlpModularCms.Modules.Contact.Config; /// /// Per-host configuration of the contact module (section Contact). /// public class ContactOptions { public const string SectionName = "Contact"; /// /// 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 ). /// public bool Enabled { get; set; } /// Identifies the site on stored rows and in the token purpose. Never taken from a request. public string? SiteId { get; set; } /// Minimum seconds between issuing the token and submitting; faster submissions are treated as spam. public int MinSeconds { get; set; } = 3; public int TokenLifetimeMinutes { get; set; } = 120; /// Months a genuine message is kept (fixed per row at insert). public int RetentionMonths { get; set; } = 12; /// Days a message classified as spam is kept. public int SpamRetentionDays { get; set; } = 30; /// Per site and UTC day; beyond it spam is answered normally but not stored. public int MaxSpamRowsPerDay { get; set; } = 200; public int MaxBodyBytes { get; set; } = 16384; /// Base of the validation problem type URI. When empty the type is about:blank. public string? ProblemTypeBase { get; set; } public ContactCaptchaOptions Captcha { get; set; } = new(); /// /// Where the notification mail for a genuine message goes. Supply it through the environment /// (Contact__RecipientEmail); it is never part of committed configuration and never exposed to a client. /// public string? RecipientEmail { get; set; } public ContactMailOptions Mail { get; set; } = new(); public string EffectiveSiteId => string.IsNullOrWhiteSpace(SiteId) ? "default" : SiteId.Trim(); } /// How the background sender works (section Contact:Mail). public class ContactMailOptions { /// How often the sender looks for mail that is due, besides being woken by a new message. public int SweepSeconds { get; set; } = 120; /// Automatic attempts per message; after that it stays failed until an administrator resends. public int MaxAttempts { get; set; } = 6; /// Wait before attempt n+1 after attempt n failed (the last value repeats). Default 5 min, 15 min, 1 h, 6 h, 24 h. public int[] BackoffMinutes { get; set; } = [5, 15, 60, 360, 1440]; /// A claimed message is not claimed again by another instance for this long. 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)])); } } /// Start-up validation: only checks anything while the module and the captcha are switched on. public sealed class ContactOptionsValidator : Microsoft.Extensions.Options.IValidateOptions { 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; } /// The captcha provider; the only supported value is friendlycaptcha (Friendly Captcha v2). public string? Provider { get; set; } /// Public site key; it is also sent to the browser in the token response. public string? SiteKey { get; set; } /// Server-side API key; supply it through the environment (Contact__Captcha__Secret) or a secrets manager, never committed. public string? Secret { get; set; } /// Verify endpoint. Global by default; for the EU-only endpoint use https://eu.frcapi.com/api/v2/captcha/siteverify. public string VerifyUrl { get; set; } = DefaultVerifyUrl; /// A provider that does not answer within this time counts as passed (fail-open). public int TimeoutSeconds { get; set; } = 3; public bool IsFriendlyCaptcha => string.Equals(Provider?.Trim(), FriendlyCaptcha, StringComparison.OrdinalIgnoreCase); /// The problems that stop the host from starting while the captcha is on (setting names only, never values). public IReadOnlyList Problems() { if (!Enabled) return []; var problems = new List(); 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; } }