|
| 1 | +import requests |
| 2 | +from mojang.exceptions import * |
| 3 | + |
| 4 | +from ..utils.auth import URLs |
| 5 | + |
| 6 | + |
| 7 | +HEADERS_ACCEPT_URL_ENCODED = { |
| 8 | + 'content-type': 'application/x-www-form-urlencoded' |
| 9 | +} |
| 10 | + |
| 11 | +HEADERS_ACCEPT_JSON = { |
| 12 | + 'content-type': 'application/json', |
| 13 | + 'accept': 'application/json' |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +def get_login_url(client_id: str, redirect_uri: str = 'http://example.com') -> str: |
| 18 | + """Returns the login url for the browser |
| 19 | + |
| 20 | + Args: |
| 21 | + client_id (str): Azure Active Directory App's client id |
| 22 | + redirect_uri (str, optional): The redirect uri of your application |
| 23 | + """ |
| 24 | + return URLs.microsoft_authorize(client_id, redirect_uri) |
| 25 | + |
| 26 | + |
| 27 | +def authorize(client_id: str, client_secret: str, auth_code: str, redirect_uri: str = 'http://example.com') -> tuple: |
| 28 | + """Retrieve the access token and refresh token from the given auth code |
| 29 | + |
| 30 | + Args: |
| 31 | + client_id (str): Azure Active Directory App's client id |
| 32 | + client_secret (str): Azure Active Directory App's client secret |
| 33 | + auth_code (str): The auth code received from the login url |
| 34 | + redirect_uri (str, optional): The redirect uri of your application |
| 35 | + |
| 36 | + Returns: |
| 37 | + A tuple containing the access token and refresh token |
| 38 | +
|
| 39 | + Raises: |
| 40 | + MicrosoftInvalidGrant: If the auth code is invalid |
| 41 | + """ |
| 42 | + |
| 43 | + data = { |
| 44 | + 'client_id': client_id, |
| 45 | + 'client_secret': client_secret, |
| 46 | + 'code': auth_code, |
| 47 | + 'grant_type': 'authorization_code', |
| 48 | + 'redirect_uri': redirect_uri |
| 49 | + } |
| 50 | + |
| 51 | + response = requests.post(URLs.microsoft_token(), headers=HEADERS_ACCEPT_URL_ENCODED, data=data) |
| 52 | + data = handle_response(response, MicrosoftInvalidGrant) |
| 53 | + |
| 54 | + return data['access_token'], data['refresh_token'] |
| 55 | + |
| 56 | +def refresh(client_id: str, client_secret: str, refresh_token: str, redirect_uri: str = 'http://example.com') -> tuple: |
| 57 | + """Refresh an access token |
| 58 | + |
| 59 | + Args: |
| 60 | + client_id (str): Azure Active Directory App's client id |
| 61 | + client_secret (str): Azure Active Directory App's client secret |
| 62 | + refresh_token (str): The refresh token |
| 63 | + redirect_uri (str, optional): The redirect uri of your application |
| 64 | + |
| 65 | + Returns: |
| 66 | + A tuple containing the access token and refresh token |
| 67 | +
|
| 68 | + Raises: |
| 69 | + MicrosoftInvalidGrant: If the auth code is invalid |
| 70 | + """ |
| 71 | + |
| 72 | + data = { |
| 73 | + 'client_id': client_id, |
| 74 | + 'client_secret': client_secret, |
| 75 | + 'refresh_token': refresh_token, |
| 76 | + 'grant_type': 'refresh_token', |
| 77 | + 'redirect_uri': redirect_uri |
| 78 | + } |
| 79 | + |
| 80 | + response = requests.post(URLs.microsoft_token(), headers=HEADERS_ACCEPT_URL_ENCODED, data=data) |
| 81 | + data = handle_response(response, MicrosoftInvalidGrant) |
| 82 | + |
| 83 | + return data['access_token'], data['refresh_token'] |
| 84 | + |
| 85 | + |
| 86 | +def authenticate_xbl(auth_token: str) -> tuple: |
| 87 | + """Authenticate with Xbox Live |
| 88 | + |
| 89 | + Args: |
| 90 | + auth_token (str): The access token received from the authentication with Microsoft |
| 91 | + |
| 92 | + Returns: |
| 93 | + A tuple containing the Xbox Live token and user hash |
| 94 | +
|
| 95 | + Raises: |
| 96 | + XboxLiveAuthenticationError: If the auth token is invalid |
| 97 | + """ |
| 98 | + |
| 99 | + data = { |
| 100 | + "Properties": { |
| 101 | + "AuthMethod": "RPS", |
| 102 | + "SiteName": "user.auth.xboxlive.com", |
| 103 | + "RpsTicket": "d={}".format(auth_token) |
| 104 | + }, |
| 105 | + "RelyingParty": "http://auth.xboxlive.com", |
| 106 | + "TokenType": "JWT" |
| 107 | + } |
| 108 | + |
| 109 | + response = requests.post(URLs.microsoft_xbl_authenticate(), headers=HEADERS_ACCEPT_JSON, json=data) |
| 110 | + data = handle_response(response, XboxLiveAuthenticationError) |
| 111 | + return data['Token'], data['DisplayClaims']['xui'][0]['uhs'] |
| 112 | + |
| 113 | +def authenticate_xsts(xbl_token: str) -> tuple: |
| 114 | + """Retrieve the XSTS Token |
| 115 | + |
| 116 | + Args: |
| 117 | + xbl_token (str): The Xbox Live token |
| 118 | + |
| 119 | + Returns: |
| 120 | + A tuple containing the XSTS token and user hash |
| 121 | +
|
| 122 | + Raises: |
| 123 | + XboxLiveAuthenticationError: If the xbl token is invalid |
| 124 | + """ |
| 125 | + |
| 126 | + data = { |
| 127 | + "Properties": { |
| 128 | + "SandboxId": "RETAIL", |
| 129 | + "UserTokens": [xbl_token] |
| 130 | + }, |
| 131 | + "RelyingParty": "rp://api.minecraftservices.com/", |
| 132 | + "TokenType": "JWT" |
| 133 | + } |
| 134 | + |
| 135 | + response = requests.post(URLs.microsoft_xbl_authorize(), headers=HEADERS_ACCEPT_JSON, json=data) |
| 136 | + data = handle_response(response, XboxLiveAuthenticationError) |
| 137 | + return data['Token'], data['DisplayClaims']['xui'][0]['uhs'] |
| 138 | + |
| 139 | +def authenticate_minecraft(userhash: str, xsts_token: str): |
| 140 | + """Login to minecraft using Xbox Live |
| 141 | + Args: |
| 142 | + user_hash (str): The user hash from Xbox Live |
| 143 | + xbl_token (str): The XSTS Token from Xbox Live |
| 144 | + |
| 145 | + Returns: |
| 146 | + The minecraft access token |
| 147 | +
|
| 148 | + Raises: |
| 149 | + XboxLiveInvalidUserHash: If the user hash is invalid |
| 150 | + Unauthorized: If the XSTS token is invalid |
| 151 | + """ |
| 152 | + |
| 153 | + data = {"identityToken": f"XBL3.0 x={userhash};{xsts_token}"} |
| 154 | + |
| 155 | + response = requests.post(URLs.login_with_microsoft(), headers=HEADERS_ACCEPT_JSON, json=data) |
| 156 | + data = handle_response(response, XboxLiveInvalidUserHash, Unauthorized) |
| 157 | + return data['access_token'] |
0 commit comments