I finally wrote a (somewhat) clean and feature-complete authentication handler that is not hacked together

backup
LinuxMint4Ever 2019-10-08 02:50:57 +02:00
parent 6958634cb4
commit cefae868c3
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import json, urllib2
# This allows for easier addition of new actions
discardResponse = {
"validate": True,
"invalidate": True,
"signout": True,
"authenticate": False,
"refresh": False
}
# Interaction with the Mojang authentication server
def interact(action, data):
if discardResponse[action]:
try:
urllib2.urlopen(urllib2.Request(url="https://authserver.mojang.com/"+action, data=json.dumps(data).encode(),headers={"Content-Type": "application/json"})).read().decode()
return True, {}
except urllib2.HTTPError:
return False, {}
else:
try:
serverResponse = json.loads(urllib2.urlopen(urllib2.Request(url="https://authserver.mojang.com/"+action, data=json.dumps(data).encode(),headers={"Content-Type": "application/json"})).read().decode())
return True, serverResponse
except urllib2.HTTPError:
return False, {}
# Wrapper functions for interact
def authenticateUser(logname, passwd):
return interact("authenticate", {'username': logname, 'password': passwd, 'agent': {'version': 1, 'name': 'Minecraft'}})
def validateToken(accessToken, clientToken):
return interact("validate", {'accessToken': accessToken, 'clientToken': clientToken})
def refreshToken(accessToken, clientToken):
return interact("refresh", {'accessToken': accessToken, 'clientToken': clientToken})
def deauthenticateUser(logname, passwd):
return interact("signout", {'username': logname, 'password': passwd})
def invalidateToken(accessToken, clientToken):
return interact("invalidate", {'accessToken': accessToken, 'clientToken': clientToken})