mirror of
https://github.com/CyberL1/MyMcRealms.git
synced 2025-06-28 17:39:42 -04:00
Import from Github
This commit is contained in:
@ -0,0 +1,34 @@
|
||||
using Minecraft_Realms_Emulator.Attributes;
|
||||
using System.Text;
|
||||
|
||||
namespace Minecraft_Realms_Emulator.Middlewares
|
||||
{
|
||||
public class MinecraftCookieMiddleware(RequestDelegate next)
|
||||
{
|
||||
private readonly RequestDelegate _next = next;
|
||||
|
||||
public async Task Invoke(HttpContext httpContext)
|
||||
{
|
||||
var endpoint = httpContext.GetEndpoint();
|
||||
var attribute = endpoint?.Metadata.GetMetadata<RequireMinecraftCookieAttribute>();
|
||||
|
||||
if (attribute == null)
|
||||
{
|
||||
await _next(httpContext);
|
||||
return;
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.Cookie.ToString() == "")
|
||||
{
|
||||
httpContext.Response.StatusCode = 401;
|
||||
await httpContext.Response.Body.WriteAsync(Encoding.UTF8.GetBytes("Authorization required"));
|
||||
return;
|
||||
}
|
||||
|
||||
string cookie = httpContext.Request.Headers.Cookie.ToString();
|
||||
if (!attribute.HasMinecraftCookie(cookie)) throw new UnauthorizedAccessException("Malformed cookie header");
|
||||
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user