Added Authentication for Minecraft Server

In this commit, a beautiful authentication method was added! It can be
uses to authenticate SECURLY!!!! with the McFet Plugin! This is pretty
USEFULL!!!
This commit is contained in:
sebivh
2025-10-20 15:22:58 +02:00
parent 7b16f85dd1
commit b7aaedba14
8 changed files with 77 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ env = environ.Env(
ETHERPAD_HOST=(str, ""), ETHERPAD_HOST=(str, ""),
ETHERPAD_GROUP=(str, ""), ETHERPAD_GROUP=(str, ""),
GALLERY_PATH=(str, "uploads/gallery"), GALLERY_PATH=(str, "uploads/gallery"),
MC_MASTERPASSWORD=(str, ""),
) )
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@@ -396,3 +397,6 @@ THUMBNAIL_ALIASES = {
"portrait": {"size": (200, 300), "crop": False}, "portrait": {"size": (200, 300), "crop": False},
}, },
} }
# Minecraft Auth
MC_MASTERPASSWORD = env("MC_MASTERPASSWORD")

View File

@@ -40,6 +40,7 @@ urlpatterns = [
path("posts/", include("posts.urls")), path("posts/", include("posts.urls")),
path("rental/", include("rental.urls")), path("rental/", include("rental.urls")),
path("search/", include("search.urls")), path("search/", include("search.urls")),
path("minecraft/", include("minecraft.urls")),
path( path(
"discord/", "discord/",
RedirectView.as_view(url="https://discord.com/invite/7qRuuMA"), RedirectView.as_view(url="https://discord.com/invite/7qRuuMA"),

View File

View File

View File

View File

@@ -0,0 +1,9 @@
from django.urls import path
from . import views
app_name = "Minecraft"
urlpatterns = [
path("", views.index, name="index"),
]

View File

@@ -0,0 +1,42 @@
import logging
from django.shortcuts import render
from authentications.decorators import authenticated_user
from django.conf import settings
logger = logging.getLogger(__name__)
# Function that generates a token depending on unsername and masterpassword
# The masterpassword must be 32 characters long!
def create_token(username, masterpassword):
padded_username = username.ljust(32, ':')
token = bytearray()
# xor connect with masterpassword to create token
for i in range(32):
token.append(ord(padded_username[i]) ^ ord(masterpassword[i]))
str_token = token.hex()
return str_token
@authenticated_user
def index(request):
username = request.user
context = {
"mctoken": "",
"valid_master_pwd": True
}
masterpassword = settings.MC_MASTERPASSWORD
if len(masterpassword) != 32:
context["valid_master_pwd"] = False
logger.error("Masterpassword must be 32 characters long")
else:
token = create_token(str(username), masterpassword)
context["mctoken"] = token
return render(request, "minecraft/index.html", context)

View File

@@ -0,0 +1,21 @@
{% extends 'base.html' %}
{% load static %}
{% block title %}Minecraft{% endblock %}
{% block content %}
<main>
<div class="container mx-auto w-full px-4 my-8 flex-1">
<h1 class="page-title">Minecraft</h1>
{% if valid_master_pwd %}
<h2>Dein persömlicher Token für den Minecraft-Server:</h2>
<p>{{ mctoken }}</p>
{% else %}
<h2>Ups... Das hat nicht geklappt!</h2>
<p>Bitte informiere einen Admin!</p>
{% endif %}
</div>
</main>
{% endblock %}