Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustments to AuthenticationStub for maybe some authentication inter… #181

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading