using System.Security.Claims;
namespace Web.Api.Extensions;
///
/// 공개 API (/v1/*) 호출 주체 식별.
/// OAuth2 앱 토큰: token_type=oauth2 + app_id claim / PAT: token_type=pat + NameIdentifier(소유 회원).
///
public static class PublicApiCallerExtensions
{
public static bool IsOAuth2App(this ClaimsPrincipal user)
{
return user.FindFirst("token_type")?.Value == "oauth2";
}
/// OAuth2 앱 토큰의 ApplicationID. PAT 등 다른 토큰이면 null.
public static int? GetApplicationID(this ClaimsPrincipal user)
{
if (!user.IsOAuth2App())
{
return null;
}
return int.TryParse(user.FindFirst("app_id")?.Value, out var id) ? id : null;
}
/// PAT 토큰의 소유 회원 ID. OAuth2 토큰이면 null.
public static int? GetPatOwnerMemberID(this ClaimsPrincipal user)
{
if (user.FindFirst("token_type")?.Value != "pat")
{
return null;
}
return int.TryParse(user.FindFirst(ClaimTypes.NameIdentifier)?.Value, out var id) ? id : null;
}
}