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

Commit

Permalink
Added MIT license and formatted for PyPI
Browse files Browse the repository at this point in the history
Separated each function to its own file, and added MIT license
  • Loading branch information
samcarsonx committed Aug 6, 2019
1 parent 4db64f1 commit 0270196
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 84 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/__pycache__
__pycache__
yggdrasil/__pycache__
.DS_Store
Yggdrasil/.DS_Store
9 changes: 9 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2019 Sam Carson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from distutils.core import setup
setup(
name = 'yggdrasil-py',
packages = ['yggdrasil'],
version = '1.0',
license='MIT',
description = 'Python wrapper for Mojang\'s Yggdrasil authentication service.',
author = 'Sam Carson',
author_email = 'me@samcarson.xyz',
url = 'https://github.com/samcarsonx/yggdrasil-py',
download_url = 'https://github.com/samcarsonx/yggdrasil-py/archive/v1.0.tar.gz',
keywords = ['mojang','yggdrasil','minecraft','scrolls','authentication'],
install_requires = [],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
)
83 changes: 0 additions & 83 deletions yggdrasil.py

This file was deleted.

10 changes: 10 additions & 0 deletions yggdrasil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'''
Python wrapper for Mojang's Yggdrasil authentication service by Sam Carson.
Works on Python 3.6+ due to f-strings but can be changed to %s or .format()
'''

from yggdrasil.authenticate import authenticate
from yggdrasil.refresh import refresh
from yggdrasil.validate import validate
from yggdrasil.signout import signout
from yggdrasil.invalidate import invalidate
20 changes: 20 additions & 0 deletions yggdrasil/authenticate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import requests, json

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):
'''
Authenticates a user using their password.
Parameters:
username - Username of agent/Mojang email (if migrated)
password - Password for the account used
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
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)
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
else: return response
18 changes: 18 additions & 0 deletions yggdrasil/invalidate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests, json

url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def invalidate(username:str, password:str):
'''
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
Returns: True if success, otherwise the error
'''
data = json.dumps({"accessToken":accessToken, "clientToken":clientToken})
try: response = json.loads(requests.post(url + '/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
18 changes: 18 additions & 0 deletions yggdrasil/refresh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests, json

url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def refresh(accessToken:str, clientToken:str, requestUser:bool = False):
'''
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
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)
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
else: return response
18 changes: 18 additions & 0 deletions yggdrasil/signout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests, json

url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def signout(username:str, password:str):
'''
Invalidates accessTokens using an account's username and password.
Parameters:
username - Username of agent/Mojang email (if migrated)
password - Password for the account used
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)
except json.decoder.JSONDecodeError: return True
if 'error' in response: raise Exception(f"{response['error']}: {response['errorMessage']}")
return False
17 changes: 17 additions & 0 deletions yggdrasil/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests, json

url = 'https://authserver.mojang.com'
headers = {'Content-Type': 'application/json'}

def validate(accessToken:str, clientToken:str = None):
'''
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
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)
except json.decoder.JSONDecodeError: return True
return False

0 comments on commit 0270196

Please sign in to comment.