39 lines
2.1 KiB
C#
39 lines
2.1 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using ZB.MOM.WW.Secrets.Ui;
|
|
namespace SecretsUiFixture;
|
|
internal sealed class SyntheticAuthentication(NavigationManager navigation) : AuthenticationStateProvider
|
|
{
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
{
|
|
var query = QueryHelpers.ParseQuery(new Uri(navigation.Uri).Query);
|
|
string role = query.TryGetValue("role", out var value) ? value.ToString() : "reveal";
|
|
return Task.FromResult(new AuthenticationState(SyntheticIdentity.ForRole(role)));
|
|
}
|
|
}
|
|
internal sealed record FaultSettings(bool List = false, bool Write = false, bool Audit = false, int DelayMs = 0);
|
|
|
|
internal static class SyntheticIdentity
|
|
{
|
|
internal static ClaimsPrincipal ForRole(string role)
|
|
{
|
|
var claims = new List<Claim> { new(ClaimTypes.Name, "synthetic-reviewer") };
|
|
if (role == "manage") claims.Add(new(ClaimTypes.Role, SecretsAuthorization.ManageRole));
|
|
if (role == "reveal") claims.Add(new(ClaimTypes.Role, SecretsAuthorization.RevealRole));
|
|
return new ClaimsPrincipal(new ClaimsIdentity(claims, "local-synthetic-fixture"));
|
|
}
|
|
}
|
|
internal sealed class SyntheticHandler(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions> options,
|
|
ILoggerFactory logger, System.Text.Encodings.Web.UrlEncoder encoder)
|
|
: Microsoft.AspNetCore.Authentication.AuthenticationHandler<Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions>(options, logger, encoder)
|
|
{
|
|
protected override Task<Microsoft.AspNetCore.Authentication.AuthenticateResult> HandleAuthenticateAsync()
|
|
{
|
|
string role = Request.Query.TryGetValue("role", out var value) ? value.ToString() : "reveal";
|
|
return Task.FromResult(Microsoft.AspNetCore.Authentication.AuthenticateResult.Success(
|
|
new Microsoft.AspNetCore.Authentication.AuthenticationTicket(SyntheticIdentity.ForRole(role), Scheme.Name)));
|
|
}
|
|
}
|