From 969c5c4927f6ab7664abf155098201e0fb1a93c2 Mon Sep 17 00:00:00 2001 From: Lucino772 Date: Sat, 13 Nov 2021 17:33:39 +0100 Subject: [PATCH] Removed AuthenticationInfo class --- mojang/account/auth/yggdrasil.py | 94 ++++++++++++------------------- mojang/account/ext/_profile.py | 6 +- mojang/account/ext/session.py | 6 +- mojang/account/structures/auth.py | 19 +------ 4 files changed, 42 insertions(+), 83 deletions(-) diff --git a/mojang/account/auth/yggdrasil.py b/mojang/account/auth/yggdrasil.py index 7e109709..5bb6e3c2 100644 --- a/mojang/account/auth/yggdrasil.py +++ b/mojang/account/auth/yggdrasil.py @@ -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) diff --git a/mojang/account/ext/_profile.py b/mojang/account/ext/_profile.py index cf54faa1..eff5f895 100644 --- a/mojang/account/ext/_profile.py +++ b/mojang/account/ext/_profile.py @@ -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): diff --git a/mojang/account/ext/session.py b/mojang/account/ext/session.py index d14151d8..4c24ed75 100644 --- a/mojang/account/ext/session.py +++ b/mojang/account/ext/session.py @@ -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) diff --git a/mojang/account/structures/auth.py b/mojang/account/structures/auth.py index 63dbb5c3..06145c7d 100644 --- a/mojang/account/structures/auth.py +++ b/mojang/account/structures/auth.py @@ -1,24 +1,6 @@ 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): """ @@ -26,5 +8,6 @@ class ChallengeInfo(NamedTuple): id (int): The id of the challenge challenge (str): The challenge to complete """ + id: int challenge: str