yavsc/src/Yavsc.Org.Tests/TestUserMiddleware.cs
Paul Schneider 349ddc03f5
Some checks failed
Dotnet build and test / log-the-inputs (pull_request) Has been cancelled
Dotnet build and test / build (pull_request) Has been cancelled
refactor: extract WebHostFixture + TestAuthPolicyProvider to shared lib
Yavsc.Blogs.Tests will need a fixture too. Lifting the cross-cutting
plumbing (Kestrel + self-signed cert + address discovery + lazy init)
into a new Yavsc.Tests.Shared project lets the next fixture inherit
from it without copying 200+ lines of setup boilerplate, and keeps
the Org.Tests fixture focused on its IdentityServer + SMTP seed.

* New project src/Yavsc.Tests.Shared with WebHostFixture (abstract)
  and TestAuthPolicyProvider (test auth bypass via X-Test-Role).
* WebServerFixture in Org.Tests now inherits from WebHostFixture;
  BuildApp + ConfigurePipelineAsync hold only Org-specific work.
* Two shared package versions promoted to the root Directory.Packages.props.
* Tests still 30/30 green.
2026-07-06 21:33:57 +01:00

47 lines
1.7 KiB
C#

using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Yavsc.Tests.Shared;
namespace Yavsc.Org.Tests;
/// <summary>
/// Middleware that promotes the <c>X-Test-Role</c> request header to
/// an authenticated <see cref="ClaimsPrincipal"/> on
/// <see cref="HttpContext.User"/>. Lets downstream code (controllers,
/// services) call <c>User.GetUserId()</c> and other claim-based
/// helpers as if a real login had happened, while
/// <see cref="TestAuthPolicyProvider"/> independently short-circuits
/// <c>[Authorize(...)]</c> policy checks.
///
/// This middleware is added to the pipeline by
/// <see cref="TestUserStartupFilter"/> so it runs after the
/// production authentication middleware; setting User before
/// <c>UseAuthentication</c> would have it overwritten on the next
/// middleware.
/// </summary>
public class TestUserMiddleware : IMiddleware
{
public const string UserId = "test-user";
public Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var role = context.Request.Headers[TestAuthPolicyProvider.HeaderName].ToString();
if (!string.IsNullOrEmpty(role) &&
(context.User.Identity is null || !context.User.Identity.IsAuthenticated))
{
var identity = new ClaimsIdentity(
new[]
{
new Claim(
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
role),
new Claim(ClaimTypes.NameIdentifier, UserId),
},
authenticationType: "TestAuth");
context.User = new ClaimsPrincipal(identity);
}
return next(context);
}
}