mirror of
https://github.com/CyberL1/Minecraft-Realms-Emulator.git
synced 2024-12-22 04:18:21 -05:00
feat: add minecraft cookie middleware
This commit is contained in:
parent
f1cc6cc128
commit
f0eed705d7
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user