do not use powcapserver.aspnetcore package; use api controller for captcha endpoints

This commit is contained in:
2026-03-09 15:05:36 +01:00
parent b67c976758
commit bee37a65f3
5 changed files with 110 additions and 55 deletions
+17 -53
View File
@@ -1,11 +1,10 @@
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;
using PowCapServer;
using PowCapServer.Abstractions;
namespace zero.Security;
@@ -14,60 +13,25 @@ internal class ZeroSecurityModule : ZeroModule
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
IConfigurationSection captchaSection = configuration.GetSection("Zero:Captcha");
services.AddOptions<CaptchaOptions>().Bind(captchaSection);
services.AddPowCapServer(options =>
{
CaptchaOptions captchaOptions = captchaSection.Get<CaptchaOptions>() ?? new CaptchaOptions();
options.Default = captchaOptions;
});
services.TryAddSingleton<ICaptchaService, DefaultCaptchaService>();
services.TryAddSingleton<ICaptchaStore, DefaultCaptchaStore>();
services.TryAddTransient<ISerializer, DefaultSerializer>();
services.Configure<PowCapServerOptions>(opts => opts.Default = captchaSection.Get<CaptchaOptions>() ?? new CaptchaOptions());
services.AddOptions<CaptchaOptions>().Bind(captchaSection);
}
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
IOptionsMonitor<CaptchaOptions> options = serviceProvider.GetRequiredService<IOptionsMonitor<CaptchaOptions>>();
string endpointPrefix = options.CurrentValue.Endpoint;
// if (app is WebApplication webApp)
// {
// webApp.MapGet("/teamup/sync", async (ISchedulerFactory scheduler) =>
// {
// await SyncTeamUpDataJob.RunNow(scheduler);
// return "done";
// });
// }
app.UseRouting();
app.MapPowCapServer(endpointPrefix);
app.UseEndpoints(endpoints =>
{
endpoints.MapGet(endpointPrefix + "/cap.wasm", async context =>
{
Assembly assembly = typeof(ZeroSecurityModule).GetTypeInfo().Assembly;
Stream resource = assembly.GetManifestResourceStream("zero.Resources.cap_wasm_bg_0_0_6.wasm");
if (resource is null)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
await using (resource)
{
context.Response.ContentType = "application/wasm";
await resource.CopyToAsync(context.Response.Body, context.RequestAborted);
}
});
endpoints.MapGet(endpointPrefix + "/cap.widget.js", async context =>
{
Assembly assembly = typeof(ZeroSecurityModule).GetTypeInfo().Assembly;
Stream resource = assembly.GetManifestResourceStream("zero.Resources.cap_min_0_1_41.js");
if (resource is null)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
await using (resource)
{
context.Response.ContentType = "text/javascript";
await resource.CopyToAsync(context.Response.Body, context.RequestAborted);
}
});
});
StaticCaptchaService.Configure(serviceProvider);
}
}