using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.Secrets.Abstractions;
namespace ZB.MOM.WW.Secrets.Ui;
///
/// Resolves the principal to record as the audit Actor for a UI-driven secret mutation or
/// reveal. The resolution order is: an explicitly wired (if the
/// host registered one and it reports a non-blank actor); otherwise the authenticated user's
/// from the cascading authentication state;
/// otherwise the literal "unknown" so an action is never attributed to a blank actor.
///
internal static class SecretActorResolver
{
/// The fallback actor used when no accessor and no authenticated identity name is available.
public const string Unknown = "unknown";
///
/// Resolves the audit actor. Never returns null or blank.
///
/// The component's service provider, used to optionally resolve an .
/// The cascading authentication-state task, or null if unavailable.
/// The resolved actor identity string.
public static async Task ResolveAsync(IServiceProvider services, Task? authState)
{
string? fromAccessor = services.GetService()?.CurrentActor;
if (!string.IsNullOrWhiteSpace(fromAccessor))
{
return fromAccessor;
}
if (authState is not null)
{
AuthenticationState state = await authState.ConfigureAwait(false);
string? name = state.User.Identity?.Name;
if (!string.IsNullOrWhiteSpace(name))
{
return name;
}
}
return Unknown;
}
}