Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Added parameters for using custom authentication servers #1

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions yggdrasil/authenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def authenticate(username:str, password:str, agentName:str = 'Minecraft', clientToken:str = None, requestUser:str = False):
def authenticate(username:str, password:str, agentName:str = 'Minecraft', clientToken:str = None, requestUser:str = False, authServer:str = url):
'''
Authenticates a user using their password.
Parameters:
Expand All @@ -12,9 +12,10 @@ def authenticate(username:str, password:str, agentName:str = 'Minecraft', client
agentName - Agent, defaults to Minecraft, can also be Scrolls
clientToken - Client identifier, must be random and identical per request
requestUser - If set to True, request for user object too
authServer - Authentication server, defaults to authserver.mojang.com
Returns: Formatted JSON, see API documentation
'''
data = json.dumps({"agent":{"name":agentName,"version":1}, "username":username, "password":password, "clientToken":clientToken, "requestUser":requestUser})
response = json.loads(requests.post(url + '/authenticate', data=data, headers=headers).text)
response = json.loads(requests.post(authServer + '/authenticate', data=data, headers=headers).text)
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
else: return response
5 changes: 3 additions & 2 deletions yggdrasil/invalidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def invalidate(accessToken:str, clientToken:str):
def invalidate(accessToken:str, clientToken:str, authServer:str = url):
'''
Invalidates accessTokens using a client/access token pair.
Parameters:
accessToken - Valid accessToken, gained from authenticate()
clientToken - Identical to the clientToken used to get the accessToken in the first place
authServer - Authentication server, defaults to authserver.mojang.com
Returns: True if success, otherwise the error
'''
data = json.dumps({"accessToken":accessToken, "clientToken":clientToken})
try: response = json.loads(requests.post(url + '/invalidate', data=data, headers=headers).text)
try: response = json.loads(requests.post(authServer + '/invalidate', data=data, headers=headers).text)
except json.decoder.JSONDecodeError: return True
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
return False
5 changes: 3 additions & 2 deletions yggdrasil/refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def refresh(accessToken:str, clientToken:str, requestUser:bool = False):
def refresh(accessToken:str, clientToken:str, requestUser:bool = False, authServer:str = url):
'''
Refreshes a valid accessToken. It can be used to keep a user logged in between gaming sessions and is preferred over storing the user's password in a file.
Parameters:
accessToken - Valid accessToken, gained from authenticate()
clientToken - Identical to the clientToken used to get the accessToken in the first place
requestUser - If set to True, request for user object too
authServer - Authentication server, defaults to authserver.mojang.com
Returns: Formatted JSON, see API documentation
'''
data = json.dumps({"accessToken":accessToken, "clientToken":clientToken, "requestUser":requestUser})
response = json.loads(requests.post(url + '/refresh', data=data, headers=headers).text)
response = json.loads(requests.post(authServer + '/refresh', data=data, headers=headers).text)
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
else: return response
5 changes: 3 additions & 2 deletions yggdrasil/signout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def signout(username:str, password:str):
def signout(username:str, password:str, authServer:str = url):
'''
Invalidates accessTokens using an account's username and password.
Parameters:
username - Username of agent/Mojang email (if migrated)
password - Password for the account used
authServer - Authentication server, defaults to authserver.mojang.com
Returns: True if success, otherwise the error
'''
data = json.dumps({"username":username, "password":password})
try: response = json.loads(requests.post(url + '/signout', data=data, headers=headers).text)
try: response = json.loads(requests.post(authServer + '/signout', data=data, headers=headers).text)
except json.decoder.JSONDecodeError: return True
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
return False
5 changes: 3 additions & 2 deletions yggdrasil/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def validate(accessToken:str, clientToken:str = None):
def validate(accessToken:str, clientToken:str = None, authServer:str = url):
'''
Checks if an accessToken is usable for authentication with a Minecraft server.
Parameters:
accessToken - Valid accessToken, gained from authenticate()
clientToken - Identical to the clientToken used to get the accessToken in the first place
authServer - Authentication server, defaults to authserver.mojang.com
Returns: Boolean, if accessToken is valid
'''
data = json.dumps({"accessToken":accessToken, "clientToken":clientToken})
try: response = json.loads(requests.post(url + '/validate', data=data, headers=headers).text)
try: response = json.loads(requests.post(authServer + '/validate', data=data, headers=headers).text)
except json.decoder.JSONDecodeError: return True
return False