From 080603f73db8cc0096cfc1c89788123d76a1ade6 Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Mon, 30 Dec 2024 14:03:35 -0600 Subject: [PATCH] Adjustments to AuthenticationStub for maybe some authentication internals changes in AspNetCore 9 --- src/Alba/Security/AuthenticationStub.cs | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Alba/Security/AuthenticationStub.cs b/src/Alba/Security/AuthenticationStub.cs index 0e3f930..aca09cb 100644 --- a/src/Alba/Security/AuthenticationStub.cs +++ b/src/Alba/Security/AuthenticationStub.cs @@ -38,7 +38,9 @@ IHostBuilder IAlbaExtension.Configure(IHostBuilder builder) return builder.ConfigureServices(services => { services.AddSingleton(this); + services.AddAuthentication(OverrideSchemeTargetName ?? TestSchemaName); services.AddTransient(); + services.AddTransient(); }); } @@ -50,6 +52,46 @@ internal ClaimsPrincipal BuildPrincipal(HttpContext context) return principal; } + private sealed class StubAuthenticationHandlerProvider : IAuthenticationHandlerProvider, IAuthenticationHandler + { + private readonly AuthenticationStub _authSchemaStub; + private HttpContext _context; + + public StubAuthenticationHandlerProvider(AuthenticationStub authSchemaStub) + { + _authSchemaStub = authSchemaStub; + } + + public Task GetHandlerAsync(HttpContext context, string authenticationScheme) + { + _context = context; + return Task.FromResult(this); + } + + public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) + { + _context = context; + return Task.CompletedTask; + } + + public Task AuthenticateAsync() + { + var principal = _authSchemaStub.BuildPrincipal(_context); + var ticket = new AuthenticationTicket(principal, TestSchemaName); + return Task.FromResult(AuthenticateResult.Success(ticket)); + } + + public Task ChallengeAsync(AuthenticationProperties? properties) + { + return Task.CompletedTask; + } + + public Task ForbidAsync(AuthenticationProperties? properties) + { + return Task.CompletedTask; + } + } + private sealed class MockSchemeProvider : AuthenticationSchemeProvider { private readonly string? _overrideSchemaTarget;