Skip to content

Commit

Permalink
Moved mojang.api.auth.yggdrasil to mojang.account.auth.yggdrasil
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed Apr 27, 2021
1 parent f901059 commit f501c02
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mojang/account/_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,12 @@ class Skin(_SkinCapeBase):

class Cape(_SkinCapeBase):
pass

## Authentication
class AuthenticationInfo(NamedTuple):
access_token: str
client_token: str
uuid: str
name: str
legacy: bool = False
demo: bool = False
66 changes: 66 additions & 0 deletions mojang/account/auth/yggdrasil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import requests

from ._urls import URLs
from .._structures import AuthenticationInfo

def authenticate(username: str, password: str, client_token: str = None):
payload = {
'username': username,
'password': password,
'clientToken': client_token,
'agent': {
'name': 'Minecraft',
'version': 1
}
}
response = requests.post(URLs.authenticate(), json=payload)

data = response.json()
_dict = {
'access_token': data['accessToken'],
'client_token': data['clientToken'],
'uuid': data['selectedProfile']['id'],
'name': data['selectedProfile']['name'],
'legacy': data['selectedProfile'].get('legacy', False),
'demo': not data['selectedProfile'].get('paid', True)
}
return AuthenticationInfo(**_dict)

def refresh(access_token: str, client_token: str) -> dict:
payload = {
'accessToken': access_token,
'clientToken': client_token
}
response = requests.post(URLs.refresh(), json=payload)

data = response.json()
_dict = {
'access_token': data['accessToken'],
'client_token': data['clientToken'],
'uuid': data['selectedProfile']['id'],
'name': data['selectedProfile']['name'],
'legacy': data['selectedProfile'].get('legacy', False),
'demo': not data['selectedProfile'].get('paid', True)
}
return AuthenticationInfo(**_dict)

def validate(access_token: str, client_token: str):
payload = {
'accessToken': access_token,
'clientToken': client_token
}
response = requests.post(URLs.validate(), json=payload)

def signout(username: str, password: str):
payload = {
'username': username,
'password': password
}
response = requests.post(URLs.signout(), json=payload)

def invalidate(access_token: str, client_token: str):
payload = {
'accessToken': access_token,
'clientToken': client_token
}
response = requests.post(URLs.invalidate(), json=payload)

0 comments on commit f501c02

Please sign in to comment.