Skip to content

Commit

Permalink
Removed AuthenticationInfo class
Browse files Browse the repository at this point in the history
Lucino772 committed Nov 13, 2021
1 parent 9749e10 commit 969c5c4
Showing 4 changed files with 42 additions and 83 deletions.
94 changes: 35 additions & 59 deletions mojang/account/auth/yggdrasil.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Optional
from typing import Optional, Tuple

import requests

from ...exceptions import *
from ..structures.auth import AuthenticationInfo
from ..utils.auth import URLs


def authenticate(username: str, password: str, client_token: Optional[str] = None) -> AuthenticationInfo:
def authenticate(
username: str, password: str, client_token: Optional[str] = None
) -> Tuple[str, str]:
"""Authenticate a user with name and password
Args:
@@ -35,34 +36,26 @@ def authenticate(username: str, password: str, client_token: Optional[str] = Non
```
"""
payload = {
'username': username,
'password': password,
'clientToken': client_token,
'agent': {
'name': 'Minecraft',
'version': 1
}
"username": username,
"password": password,
"clientToken": client_token,
"agent": {"name": "Minecraft", "version": 1},
}
response = requests.post(URLs.authenticate(), json=payload)
data = handle_response(response, PayloadError, CredentialsError, MigratedAccount)

_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)
data = handle_response(
response, PayloadError, CredentialsError, MigratedAccount
)

return data["accessToken"], data["clientToken"]

def refresh(access_token: str, client_token: str) -> AuthenticationInfo:

def refresh(access_token: str, client_token: str) -> Tuple[str, str]:
"""Refresh an invalid access token
Args:
access_token (str): The access token to refresh
client_token (str): The client token used to generate the access token
Returns:
AuthenticationInfo
@@ -82,34 +75,24 @@ def refresh(access_token: str, client_token: str) -> AuthenticationInfo:
AuthenticationInfo(access_token='NEW_ACCESS_TOKEN', client_token='CLIENT_TOKEN', uuid='...', name='...', legacy=False, demo=False)
```
"""
payload = {
'accessToken': access_token,
'clientToken': client_token
}
payload = {"accessToken": access_token, "clientToken": client_token}
response = requests.post(URLs.refresh(), json=payload)
data = handle_response(response, PayloadError, TokenError)

_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)
return data["accessToken"], data["clientToken"]


def validate(access_token: str, client_token: str):
"""Validate an access token
Args:
access_token (str): The access token to validate
client_token (str): The client token used to generate the access token
Raises:
TokenError: If client token is not the one used to generate the access token
PayloadError: If the tokens are not formated correctly
Example:
```python
@@ -118,20 +101,18 @@ def validate(access_token: str, client_token: str):
yggdrasil.validate('CURRENT_ACCESS_TOKEN','CLIENT_TOKEN')
```
"""
payload = {
'accessToken': access_token,
'clientToken': client_token
}
payload = {"accessToken": access_token, "clientToken": client_token}
response = requests.post(URLs.validate(), json=payload)
handle_response(response, PayloadError, TokenError)


def signout(username: str, password: str):
"""Signout user with name and password
Args:
username (str): The username or email if account is not legacy
password (str): The user password
Raises:
CredentialsError: If username and password are invalid
PayloadError: If credentials are not formated correctly
@@ -140,39 +121,34 @@ def signout(username: str, password: str):
```python
from mojang.account.auth import yggdrasil
yggdrasil.signout('USERNAME_OR_EMAIL','PASSWORD')
```
"""
payload = {
'username': username,
'password': password
}
payload = {"username": username, "password": password}
response = requests.post(URLs.signout(), json=payload)
handle_response(response, PayloadError, CredentialsError)


def invalidate(access_token: str, client_token: str):
"""Invalidate an access token
Args:
access_token (str): The access token to invalidate
client_token (str): The client token used to generate the access token
Raises:
TokenError: If client token is not the one used to generate the access token
PayloadError: If the tokens are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
yggdrasil.invalidate('CURRENT_ACCESS_TOKEN','CLIENT_TOKEN')
```
"""
payload = {
'accessToken': access_token,
'clientToken': client_token
}
payload = {"accessToken": access_token, "clientToken": client_token}
response = requests.post(URLs.invalidate(), json=payload)
handle_response(response, PayloadError, TokenError)
6 changes: 2 additions & 4 deletions mojang/account/ext/_profile.py
Original file line number Diff line number Diff line change
@@ -113,10 +113,8 @@ def reset_skin(self):

class MojangAuthenticatedUser(AuthenticatedUser):
def refresh(self):
auth = yggdrasil.refresh(self._access_token, self._refresh_token)
self._access_token, self._refresh_token = (
auth.access_token,
auth.client_token,
self._access_token, self._refresh_token = yggdrasil.refresh(
self._access_token, self._refresh_token
)

def close(self):
6 changes: 4 additions & 2 deletions mojang/account/ext/session.py
Original file line number Diff line number Diff line change
@@ -39,5 +39,7 @@ def connect(
)
```
"""
auth = yggdrasil.authenticate(username, password, client_token)
return MojangAuthenticatedUser(auth.access_token, auth.client_token)
access_token, client_token = yggdrasil.authenticate(
username, password, client_token
)
return MojangAuthenticatedUser(access_token, client_token)
19 changes: 1 addition & 18 deletions mojang/account/structures/auth.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
from typing import NamedTuple


## Authentication
class AuthenticationInfo(NamedTuple):
"""
Attributes:
access_token (str): The session's access token
client_token (str): The session's client token
uuid (str): The uuid of the player
name (str): The name of the player
legacy (bool): Wether the account has migrated
demo (bool): Wether the account is demo
"""
access_token: str
client_token: str
uuid: str
name: str
legacy: bool
demo: bool

## Security
class ChallengeInfo(NamedTuple):
"""
Attributes:
id (int): The id of the challenge
challenge (str): The challenge to complete
"""

id: int
challenge: str

0 comments on commit 969c5c4

Please sign in to comment.