2024-05-28 07:54:25 +02:00

31 lines
903 B
C#

using Minecraft_Realms_Emulator.Attributes;
namespace Minecraft_Realms_Emulator.Middlewares
{
public class AdminKeyMiddleware(RequestDelegate next)
{
private readonly RequestDelegate _next = next;
public async Task Invoke(HttpContext httpContext)
{
var endpoint = httpContext.GetEndpoint();
var attribute = endpoint?.Metadata.GetMetadata<RequireAdminKeyAttribute>();
if (attribute == null)
{
await _next(httpContext);
return;
}
if (!attribute.HasAdminKey(httpContext.Request.Headers.Authorization))
{
httpContext.Response.StatusCode = 403;
await httpContext.Response.WriteAsync("You don't have access to this resource");
return;
}
await _next(httpContext);
}
}
}