diff --git a/mc_authn/__init__.py b/mc_authn/__init__.py index 38a0f94..35c1920 100644 --- a/mc_authn/__init__.py +++ b/mc_authn/__init__.py @@ -1,3 +1 @@ from .auth import * - -__version__ = "1.0.1" diff --git a/mc_authn/auth.py b/mc_authn/auth.py index 7a38209..4e11507 100644 --- a/mc_authn/auth.py +++ b/mc_authn/auth.py @@ -11,9 +11,10 @@ from threading import Thread import requests import uvicorn from fastapi import FastAPI -from hypy_utils import ensure_dir, write, json_stringify, printc +from hypy_utils import ensure_dir, write, json_stringify, printc, color from ruamel import yaml +__version__ = "1.0.1" config_path = ensure_dir(Path.home() / '.config' / 'mc-auth') access_token_path = config_path / 'data' / 'access_token.json' @@ -47,7 +48,7 @@ def get_login_code() -> str: return 'Login success! You can close this window now.' def run(): - uvicorn.run(app, host="0.0.0.0", port=18275, reload=False) + uvicorn.run(app, host="0.0.0.0", port=18275, reload=False, log_level='error') th = Thread(target=run) th.setDaemon(True) @@ -62,7 +63,11 @@ def get_login_code() -> str: 'scope': 'XboxLive.signin offline_access', 'state': 'NOT_NEEDED' }) - webbrowser.open(url) + print(f'&6Opening {url} in the browser...') + if webbrowser.open(url): + print(f'&6If the link does not open, please open it manually.') + else: + print(f'&cFailed to open the link automatically, please open it manually.') while 'code' not in result: time.sleep(0.01) @@ -71,7 +76,7 @@ def get_login_code() -> str: def get_access_token(login_code: str) -> str: - print('Getting access token with login code...') + printc('&6Getting access token with login code...') r = http.post("https://login.live.com/oauth20_token.srf", data={ 'client_id': config['ClientID'], @@ -82,7 +87,8 @@ def get_access_token(login_code: str) -> str: }).json() write(access_token_path, json_stringify(r, indent=2)) - print(f'> Success! Response saved to {access_token_path}') + assert 'access_token' in r, color('&c> Request failed. Token is not in the response.') + printc(f'&a> Success! Response saved to {access_token_path}') return r['access_token'] @@ -95,7 +101,7 @@ def get_refresh_token() -> str | None: def refresh_access_token(refresh_token: str) -> str: - print('Refreshing access token with refresh token...') + printc('&6Refreshing access token with refresh token...') r = http.post("https://login.live.com/oauth20_token.srf", data={ 'client_id': config['ClientID'], @@ -106,13 +112,14 @@ def refresh_access_token(refresh_token: str) -> str: }).json() write(access_token_path, json_stringify(r, indent=2)) - print(f'> Success! Response saved to {access_token_path}') + assert 'access_token' in r, color('&c> Request failed. Token is not in the response.') + printc(f'&a> Success! Response saved to {access_token_path}') return r['access_token'] def get_xbox_live_token(token: str) -> str: - print('Logging into Xbox Live with access token...') + printc('&6Logging into Xbox Live with access token...') out_path = config_path / 'debug' / 'xbox_live_token.json' r = http.post('https://user.auth.xboxlive.com/user/authenticate', json={ @@ -126,7 +133,8 @@ def get_xbox_live_token(token: str) -> str: }).json() write(out_path, json_stringify(r, indent=2)) - print(f'> Success! Response saved to {out_path}') + assert 'Token' in r, color('&c> Request failed. Token is not in the response.') + printc(f'&a> Success! Response saved to {out_path}') return r['Token'] @@ -135,7 +143,7 @@ def get_xsts_token(token: str) -> tuple[str, str]: """ :return: token, user hash """ - print('Logging into XSTS with Xbox Live token...') + printc('&6Logging into XSTS with Xbox Live token...') out_path = config_path / 'debug' / 'xsts_token.json' r = http.post('https://xsts.auth.xboxlive.com/xsts/authorize', json={ @@ -148,13 +156,14 @@ def get_xsts_token(token: str) -> tuple[str, str]: }).json() write(out_path, json_stringify(r, indent=2)) - print(f'> Success! Response saved to {out_path}') + assert 'Token' in r, color('&c> Request failed. Token is not in the response.') + printc(f'&a> Success! Response saved to {out_path}') return r['Token'], r['DisplayClaims']['xui'][0]['uhs'] def get_mc_token(xsts_token: str, user_hash: str) -> str: - print('Logging into Minecraft with XSTS token...') + printc('&6Logging into Minecraft with XSTS token...') out_path = config_path / 'debug' / 'mc_token.json' r = http.post('https://api.minecraftservices.com/authentication/login_with_xbox', json={ @@ -163,26 +172,33 @@ def get_mc_token(xsts_token: str, user_hash: str) -> str: }).json() write(out_path, json_stringify(r, indent=2)) - print(f'> Success! Response saved to {out_path}') + printc(f'&a> Success! Response saved to {out_path}') mc_token = config_path / 'mc-token.txt' write(mc_token, r['access_token']) - print(f'> Minecraft token saved to {mc_token}') + printc(f'&a> Minecraft token saved to {mc_token}') return r['access_token'] def full_login(): - refresh_token = get_refresh_token() + printc(f'&3mc-authn {__version__} by HyDEV') + print() + refresh_token = get_refresh_token() if refresh_token: t1 = refresh_access_token(refresh_token) + print() else: code = get_login_code() + print() t1 = get_access_token(code) + print() t2 = get_xbox_live_token(t1) + print() t3, uhs = get_xsts_token(t2) + print() t4 = get_mc_token(t3, uhs)