Contact: Friendly Captcha v2, opstartvalidatie, ASF-host met Contact-module en hosttests (CMS-U9)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 23:06:23 +01:00
co-authored by Claude Sonnet 5.5
parent f43e00733a
commit 3275092401
20 changed files with 1052 additions and 21 deletions
@@ -73,12 +73,55 @@ public class ContactMailOptions
}
}
/// <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 secret; supply it through the environment or a secrets manager, never committed.</summary>
/// <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;
}
}