feat: add minecraft cookie middleware

This commit is contained in:
CyberL1 2024-04-15 10:53:21 +02:00
parent f1cc6cc128
commit f0eed705d7
9 changed files with 63 additions and 1 deletions

View File

@ -0,0 +1,15 @@
namespace Minecraft_Realms_Emulator.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class RequireMinecraftCookieAttribute : Attribute
{
public RequireMinecraftCookieAttribute()
{
}
public bool HasMinecraftCookie(string cookie)
{
return cookie.Contains("sid") && cookie.Contains("user") && cookie.Contains("version");
}
}
}

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Requests;
@ -9,6 +10,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class InvitesController : ControllerBase
{
private readonly DataContext _context;

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses;
@ -6,6 +7,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class McoController : ControllerBase
{
private readonly DataContext _context;

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses;
@ -6,6 +7,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class OpsController : ControllerBase
{
private readonly DataContext _context;

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses;
@ -7,6 +8,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class SubscriptionsController : ControllerBase
{
private readonly DataContext _context;

View File

@ -1,10 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class TrialController : ControllerBase
{
private readonly DataContext _context;

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
@ -7,6 +8,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class WorldsController : ControllerBase
{
private readonly DataContext _context;

View File

@ -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);
}
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Helpers;
using Minecraft_Realms_Emulator.Middlewares;
using Npgsql;
var builder = WebApplication.CreateBuilder(args);
@ -40,7 +41,7 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseAuthorization();
app.UseMiddleware<MinecraftCookieMiddleware>();
app.MapControllers();