Concept tally export and its CORS policy move out of Program.cs
One owner for the route and the policy, so Stryker can mutate them: it flags every mutant of Program.cs as a compile error (safe mode), which left the export untested by mutation. Program.cs keeps UseCors and the two registration calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cc00c64c9e
commit
624cfd1567
2 changed files with 46 additions and 27 deletions
44
src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs
Normal file
44
src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using Bifrost.Web.Configuration;
|
||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Bifrost.Web.Features.ConceptVotes;
|
||||
|
||||
// The public tally export and the one cross-origin policy in Bifrost. It owns nothing: counts are derived by
|
||||
// ConceptAppraisalService, origins come from ConceptVoteOptions.
|
||||
public static class ConceptTallyExport
|
||||
{
|
||||
public const string CorsPolicy = "ConceptTallies";
|
||||
|
||||
// GET only, no credentials, and required by the export route alone: every other route carries no CORS
|
||||
// metadata and gets no CORS headers.
|
||||
public static IServiceCollection AddConceptTallyCors(this IServiceCollection services)
|
||||
{
|
||||
services.AddCors();
|
||||
services
|
||||
.AddOptions<CorsOptions>()
|
||||
.Configure<IOptions<ConceptVoteOptions>>((cors, conceptVotes) =>
|
||||
cors.AddPolicy(
|
||||
CorsPolicy,
|
||||
policy => policy.WithOrigins(conceptVotes.Value.AllowedOrigins).WithMethods("GET")));
|
||||
return services;
|
||||
}
|
||||
|
||||
// Counts only, per live subject: ConceptTally carries no voter key, account id or network tag.
|
||||
public static IEndpointConventionBuilder MapConceptTallyExport(this IEndpointRouteBuilder app) =>
|
||||
app.MapGet("/public/concepts/{collection}/tallies", async (
|
||||
string collection,
|
||||
ConceptAppraisalService appraisals,
|
||||
HttpResponse response,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var tallies = await appraisals.GetTalliesAsync(collection, cancellationToken);
|
||||
if (tallies.Count == 0)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
response.Headers.CacheControl = "public, max-age=60";
|
||||
return Results.Json(tallies);
|
||||
}).AllowAnonymous().RequireCors(CorsPolicy);
|
||||
}
|
||||
|
|
@ -17,7 +17,6 @@ using Microsoft.AspNetCore.Authentication;
|
|||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.HttpLogging;
|
||||
|
|
@ -206,16 +205,7 @@ builder.Services.AddAuthorization(options =>
|
|||
});
|
||||
});
|
||||
|
||||
// The one cross-origin surface: the public tally export, read by browsers on the configured origins only.
|
||||
// No credentials, GET only; every other route carries no CORS metadata and gets no CORS headers.
|
||||
const string ConceptTalliesCorsPolicy = "ConceptTallies";
|
||||
builder.Services.AddCors();
|
||||
builder.Services
|
||||
.AddOptions<CorsOptions>()
|
||||
.Configure<IOptions<ConceptVoteOptions>>((cors, conceptVotes) =>
|
||||
cors.AddPolicy(
|
||||
ConceptTalliesCorsPolicy,
|
||||
policy => policy.WithOrigins(conceptVotes.Value.AllowedOrigins).WithMethods("GET")));
|
||||
builder.Services.AddConceptTallyCors();
|
||||
|
||||
var app = builder.Build();
|
||||
var appJsonSerializerOptions = CreateAppJsonSerializerOptions();
|
||||
|
|
@ -257,22 +247,7 @@ app.MapGet("/readyz", async (ReadinessService readinessService, CancellationToke
|
|||
statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
}).AllowAnonymous();
|
||||
|
||||
// Counts only, per live subject: ConceptTally carries no voter key, account id or network tag.
|
||||
app.MapGet("/public/concepts/{collection}/tallies", async (
|
||||
string collection,
|
||||
ConceptAppraisalService appraisals,
|
||||
HttpResponse response,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var tallies = await appraisals.GetTalliesAsync(collection, cancellationToken);
|
||||
if (tallies.Count == 0)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
response.Headers.CacheControl = "public, max-age=60";
|
||||
return Results.Json(tallies);
|
||||
}).AllowAnonymous().RequireCors(ConceptTalliesCorsPolicy);
|
||||
app.MapConceptTallyExport();
|
||||
|
||||
app.MapPost("/github/webhooks", async (
|
||||
HttpRequest request,
|
||||
|
|
|
|||
Loading…
Reference in a new issue