32 lines
1.5 KiB
C#
32 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using BrokerApiCore;
|
|
|
|
namespace BrokerApiServer;
|
|
|
|
/// <summary>
|
|
/// 엑세스 토큰 인증이 필요한 컨트롤러를 지정하는 애노테이션(Attribute) 정의
|
|
/// 밴 상태 체크 이슈
|
|
/// </summary>
|
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
|
public class RequirePlanetAuthAttribute : System.Attribute, IAsyncActionFilter
|
|
{
|
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
{
|
|
// require 서비스 가져오기
|
|
var planet_service = context.HttpContext.RequestServices.GetRequiredService<PlanetService>();
|
|
Guard.Against.isNull(planet_service, ServerErrorCode.InternalServerError, ()=>"PlanetService가 di에 등록돼 있지 않음");
|
|
|
|
var auth_header = context.HttpContext.Request.Headers.Authorization.FirstOrDefault() ?? string.Empty;
|
|
Guard.Against.isNullOrEmptyOrWhiteSpace(auth_header, ServerErrorCode.InvalidPlanetJwt, ()=>"empty jwt");
|
|
Guard.Against.isFalse(auth_header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase),
|
|
ServerErrorCode.InvalidUserJwt, ()=>"인증 토큰 오류");
|
|
|
|
// "Bearer " 이후의 토큰 부분을 추출합니다.
|
|
var token = auth_header["Bearer ".Length..].Trim();
|
|
var (planet_id, planet_server_type) = planet_service.validate(token);
|
|
context.HttpContext.Items["planet_id"] = planet_id;
|
|
context.HttpContext.Items["planet_server_type"] = planet_server_type;
|
|
await next();
|
|
}
|
|
}
|