using System.Security.Claims;

namespace Ikar.Server.Infrastructure;

public static class ClaimsPrincipalExtensions
{
    public static Guid GetRequiredUserId(this ClaimsPrincipal principal)
    {
        var value = principal.FindFirstValue(ClaimTypes.NameIdentifier);
        return Guid.TryParse(value, out var userId)
            ? userId
            : throw new InvalidOperationException("Authenticated user id claim is missing.");
    }

    public static Guid GetRequiredSessionId(this ClaimsPrincipal principal)
    {
        var value = principal.FindFirstValue("sid");
        return Guid.TryParse(value, out var sessionId)
            ? sessionId
            : throw new InvalidOperationException("Authenticated session id claim is missing.");
    }
}
