Bifrost/tests/Bifrost.Web.Tests/Support/TestAuthHandler.cs
2026-04-21 20:08:49 +01:00

38 lines
1.3 KiB
C#

using System.Security.Claims;
using System.Text.Encodings.Web;
using Bifrost.Web.Features.Membership;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
namespace Bifrost.Web.Tests.Support;
public sealed class TestAuthHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
public const string SchemeName = "Test";
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (Request.Headers.ContainsKey("X-Test-Anonymous"))
{
return Task.FromResult(AuthenticateResult.NoResult());
}
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, "12345"),
new Claim(ClaimTypes.Name, "test-admin"),
new Claim(BifrostClaimTypes.GitHubLogin, "test-admin"),
new Claim(BifrostClaimTypes.DisplayName, "Test Admin")
};
var identity = new ClaimsIdentity(claims, SchemeName);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, SchemeName);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}