125 lines
6.1 KiB
C#
125 lines
6.1 KiB
C#
using SlpModularCms.Core.Texts;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Threading.RateLimiting;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.OpenApi;
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.Extensions.Options;
|
|
using SlpModularCms.Core.Hosting;
|
|
using SlpModularCms.Core.Modules;
|
|
using SlpModularCms.Modules.Contact.Config;
|
|
using SlpModularCms.Modules.Contact.Controllers;
|
|
using SlpModularCms.Modules.Contact.Data;
|
|
using SlpModularCms.Modules.Contact.OpenApi;
|
|
using SlpModularCms.Modules.Contact.Repositories;
|
|
using SlpModularCms.Modules.Contact.Services;
|
|
|
|
namespace SlpModularCms.Modules.Contact;
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public class ContactModule : IModule
|
|
{
|
|
public string Name => "Contact";
|
|
public string Version => "1.0.0";
|
|
|
|
public void RegisterServices(IServiceCollection services)
|
|
{
|
|
services.AddCmsTextsFrom(typeof(ContactModule).Assembly);
|
|
services.AddOptions<ContactOptions>()
|
|
.Configure<IConfiguration>((options, config) =>
|
|
config.GetSection(ContactOptions.SectionName).Bind(options))
|
|
.ValidateOnStart();
|
|
// A switched-on captcha with missing settings stops the host at start-up (setting names only, never a value).
|
|
services.AddSingleton<IValidateOptions<ContactOptions>, ContactOptionsValidator>();
|
|
|
|
services.AddDbContext<ContactDbContext>((serviceProvider, options) =>
|
|
{
|
|
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
|
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
|
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
|
});
|
|
|
|
services.TryAddSingleton(TimeProvider.System);
|
|
services.AddScoped<IContactRepository, ContactRepository>();
|
|
services.AddScoped<IContactSubmissionService, ContactSubmissionService>();
|
|
services.AddSingleton<IContactTokenService, ContactTokenService>();
|
|
// Captcha: the provider is chosen by configuration; while it is off (or unset) everything passes.
|
|
services.AddHttpClient(FriendlyCaptchaVerifier.HttpClientName);
|
|
services.AddSingleton<FriendlyCaptchaVerifier>();
|
|
services.AddSingleton<NullCaptchaVerifier>();
|
|
services.AddSingleton<ICaptchaVerifier>(sp =>
|
|
sp.GetRequiredService<IOptions<ContactOptions>>().Value.Captcha is { Enabled: true, IsFriendlyCaptcha: true }
|
|
? sp.GetRequiredService<FriendlyCaptchaVerifier>()
|
|
: sp.GetRequiredService<NullCaptchaVerifier>());
|
|
// Mail: the request only stores and signals; the background service sends (and retries) with the host's mail pieces.
|
|
services.AddScoped<IContactMailSender, ContactMailSender>();
|
|
services.AddSingleton<IContactMailSignal, ContactMailSignal>();
|
|
services.AddHostedService<ContactMailService>();
|
|
// Gate: the form is open only if the host switch AND the CMS switch are on. The host switch
|
|
// is asked first, so with it off the database is never touched.
|
|
services.AddSingleton<ContactSettingsCache>();
|
|
services.AddScoped<IContactSettingsService, ContactSettingsService>();
|
|
services.AddScoped<HostFlagContactFormGate>();
|
|
services.AddScoped<CmsFlagContactFormGate>();
|
|
services.AddScoped<IContactFormGate>(sp => new CompositeContactFormGate(
|
|
[
|
|
sp.GetRequiredService<HostFlagContactFormGate>(),
|
|
sp.GetRequiredService<CmsFlagContactFormGate>(),
|
|
]));
|
|
services.AddScoped<IContactAdminService, ContactAdminService>();
|
|
services.AddScoped<ContactRouteFilter>();
|
|
services.AddScoped<ContactAdminRouteFilter>();
|
|
services.AddHostedService<ContactRetentionService>();
|
|
|
|
// The contact details of the OpenAPI document (named options of document 'v1', so the shared
|
|
// AddOpenApi() call in Core stays untouched).
|
|
services.Configure<OpenApiOptions>("v1", openApi => openApi.AddDocumentTransformer<ContactOpenApiDocumentTransformer>());
|
|
|
|
// Rate-limit policies live in this module so the shared limiter setup stays untouched.
|
|
// The bucket key comes from one function; see ContactRateLimitPartition.
|
|
services.AddOptions<RateLimiterOptions>()
|
|
.Configure<IConfiguration>((limiter, config) =>
|
|
{
|
|
limiter.AddPolicy(ContactMessagesController.FormPolicy, context =>
|
|
Window(config, "ContactForm", 5, 600, context));
|
|
limiter.AddPolicy(ContactMessagesController.TokenPolicy, context =>
|
|
Window(config, "ContactToken", 20, 600, context));
|
|
limiter.AddPolicy(ContactMessagesAdminController.ResendPolicy, context =>
|
|
Window(config, "ContactResend", 5, 600, context));
|
|
});
|
|
}
|
|
|
|
public void UseModule(IApplicationBuilder app)
|
|
{
|
|
// Nothing is touched while the module is switched off: not even the database.
|
|
var enabled = app.ApplicationServices.GetRequiredService<IOptions<ContactOptions>>().Value.Enabled;
|
|
if (!enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ContactDbContext>();
|
|
db.Database.Migrate();
|
|
}
|
|
|
|
private static RateLimitPartition<string> Window(
|
|
IConfiguration config, string name, int defaultPermits, int defaultSeconds, Microsoft.AspNetCore.Http.HttpContext context)
|
|
{
|
|
var section = config.GetSection($"RateLimiting:{name}");
|
|
return RateLimitPartition.GetFixedWindowLimiter(
|
|
ContactRateLimitPartition.KeyFor(context),
|
|
_ => new FixedWindowRateLimiterOptions
|
|
{
|
|
PermitLimit = section.GetValue("PermitLimit", defaultPermits),
|
|
Window = TimeSpan.FromSeconds(section.GetValue("WindowSeconds", defaultSeconds)),
|
|
QueueLimit = 0,
|
|
});
|
|
}
|
|
}
|