Skip to content

Commit

Permalink
Adjustments to AuthenticationStub for maybe some authentication inter…
Browse files Browse the repository at this point in the history
…nals changes in AspNetCore 9
  • Loading branch information
jeremydmiller committed Dec 30, 2024
1 parent 2b80fdd commit 080603f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Alba/Security/AuthenticationStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ IHostBuilder IAlbaExtension.Configure(IHostBuilder builder)
return builder.ConfigureServices(services =>
{
services.AddSingleton(this);
services.AddAuthentication(OverrideSchemeTargetName ?? TestSchemaName);
services.AddTransient<IAuthenticationSchemeProvider, MockSchemeProvider>();
services.AddTransient<IAuthenticationHandlerProvider, StubAuthenticationHandlerProvider>();
});
}

Expand All @@ -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<IAuthenticationHandler?> GetHandlerAsync(HttpContext context, string authenticationScheme)
{
_context = context;
return Task.FromResult<IAuthenticationHandler?>(this);
}

public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
_context = context;
return Task.CompletedTask;
}

public Task<AuthenticateResult> 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;
Expand Down

0 comments on commit 080603f

Please sign in to comment.