50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
import json, urllib2
|
|
|
|
################################################################################
|
|
# This software can be used privately for any purpose.
|
|
# It may be shared publickly as long as you give credit and provide
|
|
# a link to the original source.
|
|
# Do not use this for malicious purposes.
|
|
################################################################################
|
|
|
|
# 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})
|
|
|