moved urls into constants

This commit is contained in:
əlemi 2022-02-11 02:28:58 +01:00
parent 3e4b10521d
commit b1e95b43bd
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -26,6 +26,11 @@ class MicrosoftAuthenticator(AuthInterface):
accessToken : str accessToken : str
selectedProfile : GameProfile selectedProfile : GameProfile
OAUTH_LOGIN = "https://login.live.com/oauth20"
XBL_LOGIN = "https://user.auth.xboxlive.com/user/authenticate"
XSTS_LOGIN = "https://xsts.auth.xboxlive.com/xsts/authorize"
MINECRAFT_API = "https://api.minecraftservices.com"
def __init__(self, def __init__(self,
client_id:str, client_id:str,
client_secret:str, client_secret:str,
@ -46,8 +51,8 @@ class MicrosoftAuthenticator(AuthInterface):
def url(self, state:str=""): def url(self, state:str=""):
"""Builds MS OAuth url for the user to login""" """Builds MS OAuth url for the user to login"""
return ( return (
f"https://login.live.com/oauth20_authorize.srf?" + self.OAUTH_LOGIN + "_authorize.srf" +
f"client_id={self.client_id}" + f"?client_id={self.client_id}" +
f"&response_type=code" + f"&response_type=code" +
f"&redirect_uri={self.redirect_uri}" + f"&redirect_uri={self.redirect_uri}" +
f"&scope=XboxLive.signin%20offline_access" + f"&scope=XboxLive.signin%20offline_access" +
@ -87,7 +92,7 @@ class MicrosoftAuthenticator(AuthInterface):
else: else:
raise InvalidStateError("Missing auth code and refresh token") raise InvalidStateError("Missing auth code and refresh token")
auth_response = await self._post( auth_response = await self._post(
"https://login.live.com/oauth20_token.srf", self.OAUTH_LOGIN + "_token.srf",
headers={ "Content-Type": "application/x-www-form-urlencoded" }, headers={ "Content-Type": "application/x-www-form-urlencoded" },
data=urlencode(payload) data=urlencode(payload)
) )
@ -100,7 +105,7 @@ class MicrosoftAuthenticator(AuthInterface):
if not self.ms_token: if not self.ms_token:
raise InvalidStateError("Missing MS access token") raise InvalidStateError("Missing MS access token")
auth_response = await self._post( auth_response = await self._post(
"https://user.auth.xboxlive.com/user/authenticate", self.XBL_LOGIN,
headers={ headers={
"Content-Type": "application/json", "Content-Type": "application/json",
"Accept": "application/json" "Accept": "application/json"
@ -123,7 +128,7 @@ class MicrosoftAuthenticator(AuthInterface):
if not self.xbl_token: if not self.xbl_token:
raise InvalidStateError("Missing XBL Token") raise InvalidStateError("Missing XBL Token")
auth_response = await self._post( auth_response = await self._post(
"https://xsts.auth.xboxlive.com/xsts/authorize", self.XSTS_LOGIN,
headers={ headers={
"Content-Type": "application/json", "Content-Type": "application/json",
"Accept": "application/json" "Accept": "application/json"
@ -150,7 +155,7 @@ class MicrosoftAuthenticator(AuthInterface):
if not self.xsts_token: if not self.xsts_token:
raise InvalidStateError("Missing XSTS Token") raise InvalidStateError("Missing XSTS Token")
auth_response = await self._post( auth_response = await self._post(
"https://api.minecraftservices.com/authentication/login_with_xbox", self.MINECRAFT_API + "/authentication/login_with_xbox",
headers={ headers={
"Content-Type": "application/json", "Content-Type": "application/json",
"Accept": "application/json" "Accept": "application/json"
@ -164,14 +169,14 @@ class MicrosoftAuthenticator(AuthInterface):
async def fetch_mcstore(self): async def fetch_mcstore(self):
"""Get the store information""" """Get the store information"""
self.mcstore = await self._get( self.mcstore = await self._get(
"https://api.minecraftservices.com/entitlements/mcstore", self.MINECRAFT_API + "/entitlements/mcstore",
headers={ "Authorization": f"Bearer {self.access_token}" }, headers={ "Authorization": f"Bearer {self.access_token}" },
) )
async def fetch_profile(self): async def fetch_profile(self):
"""Get player profile""" """Get player profile"""
p = await self._get( p = await self._get(
"https://api.minecraftservices.com/minecraft/profile", self.MINECRAFT_API + "/minecraft/profile",
headers={ "Authorization": f"Bearer {self.access_token}" }, headers={ "Authorization": f"Bearer {self.access_token}" },
) )
self.profile = GameProfile(id=p['id'], name=p['name']) self.profile = GameProfile(id=p['id'], name=p['name'])