using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using ScadaLink.Security;
namespace ScadaLink.CentralUI.Auth;
///
/// Claim-lookup helpers for the Central UI. CentralUI-024: claim types are owned
/// by (the single source of truth). These helpers
/// resolve them through the JwtTokenService constants so a rename there
/// propagates here instead of silently breaking ten copy-pasted call sites.
///
public static class ClaimsPrincipalExtensions
{
/// Fallback returned when no username claim is present.
public const string UnknownUser = "unknown";
///
/// The audit username for , or
/// when the claim is absent.
///
public static string GetUsername(this ClaimsPrincipal principal)
=> principal.FindFirst(JwtTokenService.UsernameClaimType)?.Value ?? UnknownUser;
///
/// The display name for , or null when
/// the claim is absent.
///
public static string? GetDisplayName(this ClaimsPrincipal principal)
=> principal.FindFirst(JwtTokenService.DisplayNameClaimType)?.Value;
///
/// Resolves the current user's audit username from the auth state provider.
/// Replaces the GetCurrentUserAsync helper that was copy-pasted into
/// ten components (CentralUI-024).
///
public static async Task GetCurrentUsernameAsync(
this AuthenticationStateProvider authStateProvider)
{
var authState = await authStateProvider.GetAuthenticationStateAsync();
return authState.User.GetUsername();
}
}