Skip to content

Instantly share code, notes, and snippets.

@six519
Created January 5, 2016 04:00
Show Gist options
  • Save six519/3d17f4802a457001c474 to your computer and use it in GitHub Desktop.
Save six519/3d17f4802a457001c474 to your computer and use it in GitHub Desktop.
Minecraft Authentication Server In Django
"""
Django App Name
---------------
minecraft_yggdrasil
"""
# models.py
# ---------
from django.db import models
from django.contrib.auth.models import User
class ClientTokenStorage(models.Model):
created = models.DateTimeField(db_column='created', auto_now_add=True, blank=True)
user = models.ForeignKey(User, db_column='django_user')
token = models.CharField(db_column='token', max_length=255, null=True, blank=True)
atoken = models.CharField(db_column='atoken', max_length=255, null=True, blank=True)
class Meta:
db_table = 'client_token_storage'
# urls.py
# -------
from django.conf.urls import patterns, include, url
urlpatterns = patterns('minecraft_yggdrasil.views',
url(r'^$', 'minecraft_index', {}, name='minecraft_index'),
url(r'^authenticate/$', 'minecraft_authenticate', {}, name='minecraft_authenticate'),
url(r'^refresh/$', 'minecraft_refresh', {}, name='minecraft_refresh'),
url(r'^join/$', 'minecraft_join', {}, name='minecraft_join'),
url(r'^hasJoined/$', 'minecraft_hasJoined', {}, name='minecraft_hasJoined'),
)
# util.py
# -------
import json
from django.shortcuts import HttpResponse
from models import ClientTokenStorage
import uuid
def render_json(dict_param={}):
if len(dict_param) == 0:
dict_param = {
"errorMessage" : "Invalid JSON request.",
"error" : "Method Not Allowed"
}
return HttpResponse(json.dumps(dict_param), content_type="application/json")
def checkAccessAndClientToken(request, refresh=True):
response = {
'errorMessage': 'Invalid credentials. Invalid username or password.',
'error': 'ForbiddenOperationException'
}
if request.method == "POST":
request_json = json.loads(request.body)
print "The request data are:"
print str(request_json)
try:
if 'clientToken' in request_json:
tokenStorage = ClientTokenStorage.objects.get(token=str(request_json['clientToken']), atoken=str(request_json['accessToken']))
clientToken = str(request_json['clientToken'])
else:
tokenStorage = ClientTokenStorage.objects.get(atoken=str(request_json['accessToken']))
clientToken = tokenStorage.token
if refresh:
accessToken = str(uuid.uuid4())
tokenStorage.atoken = accessToken
tokenStorage.save()
else:
accessToken = str(request_json['accessToken'])
response = {
"accessToken": accessToken,
"clientToken": clientToken,
"selectedProfile": {
"id": clientToken,
"name": str(tokenStorage.user.username)
}
}
except Exception as e:
response['errorMessage'] = str(e)
print "The response is: %s" % response
return render_json(response)
# views.py
# --------
from django.contrib import messages
from django.shortcuts import HttpResponse, redirect, render_to_response, render, get_object_or_404
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.views.decorators.csrf import csrf_exempt
from util import render_json, checkAccessAndClientToken
from models import ClientTokenStorage
import json
import uuid
import os
def minecraft_index(request):
return HttpResponse("index")
@csrf_exempt
def minecraft_authenticate(request):
response = {
'errorMessage': 'Invalid credentials. Invalid username or password.',
'error': 'ForbiddenOperationException'
}
if request.method == "POST":
request_json = json.loads(request.body)
print "The request is: %s" % request_json
usr = authenticate(username=request_json['username'], password=request_json['password'])
accessToken = None
if usr:
try:
tokenStorage = ClientTokenStorage.objects.get(user=usr, token=str(request_json['clientToken']))
accessToken = str(tokenStorage.atoken)
except:
accessToken = str(uuid.uuid4())
tokenStorage = ClientTokenStorage()
tokenStorage.user = usr
tokenStorage.atoken = accessToken
tokenStorage.token = str(request_json['clientToken'])
tokenStorage.save()
response = {
"accessToken": accessToken,
"clientToken": str(request_json['clientToken']),
"availableProfiles": [
{
"id": str(request_json['clientToken']),
"name": str(usr.username)
}
],
"selectedProfile": {
"id": str(request_json['clientToken']),
"name": str(usr.username)
}
}
return render_json(response)
@csrf_exempt
def minecraft_refresh(request):
return checkAccessAndClientToken(request)
@csrf_exempt
def minecraft_join(request):
response = {
"id": str(uuid.uuid4()),
"properties": []
}
return render_json(response)
@csrf_exempt
def minecraft_hasJoined(request):
response = {
"id": str(uuid.uuid4()),
"properties": []
}
return render_json(response)
@escsun
Copy link

escsun commented Jul 23, 2016

Thank you! Its work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment