| 12345678910111213141516171819202122232425262728293031323334353637 |
- using System.Security.Claims;
- namespace Web.Api.Extensions;
- /// <summary>
- /// 공개 API (/v1/*) 호출 주체 식별.
- /// OAuth2 앱 토큰: token_type=oauth2 + app_id claim / PAT: token_type=pat + NameIdentifier(소유 회원).
- /// </summary>
- public static class PublicApiCallerExtensions
- {
- public static bool IsOAuth2App(this ClaimsPrincipal user)
- {
- return user.FindFirst("token_type")?.Value == "oauth2";
- }
- /// <summary>OAuth2 앱 토큰의 ApplicationID. PAT 등 다른 토큰이면 null.</summary>
- 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;
- }
- /// <summary>PAT 토큰의 소유 회원 ID. OAuth2 토큰이면 null.</summary>
- 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;
- }
- }
|