changed token field names

This commit is contained in:
əlemi 2021-12-13 13:24:07 +01:00
parent f7911ca08f
commit 53962eaa2c
2 changed files with 34 additions and 30 deletions

View file

@ -208,7 +208,7 @@ class MinecraftClient(CallbacksHolder, Runnable):
await self.dispatcher.write( await self.dispatcher.write(
PacketLoginStart( PacketLoginStart(
340, 340,
username=self.token.profile.name if self.online_mode and self.token else self.username username=self.token.selectedProfile.name if self.online_mode and self.token else self.username
) )
) )
return True return True

View file

@ -22,7 +22,7 @@ class Profile:
id : str id : str
name : str name : str
def dict(self): def as_dict(self):
return { return {
"id": self.id, "id": self.id,
"name": self.name "name": self.name
@ -31,9 +31,9 @@ class Profile:
@dataclass @dataclass
class Token: class Token:
username : str username : str
access_token : str accessToken : str
client_token : str clientToken : str
profile : Profile selectedProfile : Profile
AGENT_NAME = "Minecraft" AGENT_NAME = "Minecraft"
AGENT_VERSION = 1 AGENT_VERSION = 1
@ -42,26 +42,26 @@ class Token:
CONTENT_TYPE = "application/json" CONTENT_TYPE = "application/json"
HEADERS = {"content-type": CONTENT_TYPE} HEADERS = {"content-type": CONTENT_TYPE}
def dict(self): def as_dict(self):
return { return {
"username":self.username, "username":self.username,
"accessToken":self.access_token, "accessToken":self.accessToken,
"clientToken":self.client_token, "clientToken":self.clientToken,
"selectedProfile": self.profile.dict(), "selectedProfile": self.profile.as_dict(),
} }
@classmethod @classmethod
def from_file(cls, fname:str): def from_file(cls, fname:str):
with open(fname) as f: with open(fname) as f:
return cls.from_json(json.load(f)) return cls.from_dict(json.load(f))
@classmethod @classmethod
def from_dict(cls, data:dict): def from_dict(cls, data:dict):
return cls( return cls(
username=data["selectedProfile"]["name"], username=data["selectedProfile"]["name"],
access_token=data["accessToken"], accessToken=data["accessToken"],
client_token=data["clientToken"], clientToken=data["clientToken"],
profile=Profile(**data["selectedProfile"]), selectedProfile=Profile(**data["selectedProfile"]),
) )
@classmethod @classmethod
@ -82,9 +82,9 @@ class Token:
return cls( return cls(
username=username, username=username,
access_token=res["accessToken"], accessToken=res["accessToken"],
client_token=res["clientToken"], clientToken=res["clientToken"],
profile=Profile(**res["selectedProfile"]) selectedProfile=Profile(**res["selectedProfile"])
) )
@classmethod @classmethod
@ -94,34 +94,38 @@ class Token:
"password": password "password": password
}) })
async def refresh(self) -> dict: async def refresh(self, requestUser:bool = False) -> dict:
res = await self._post(self.AUTH_SERVER + "/refresh", { res = await self._post(self.AUTH_SERVER + "/refresh", {
"accessToken": self.access_token, "accessToken": self.accessToken,
"clientToken": self.client_token "clientToken": self.clientToken
}) })
self.access_token = res["accessToken"] self.accessToken = res["accessToken"]
self.client_token = res["clientToken"] self.clientToken = res["clientToken"]
self.profile = Profile(**res["selectedProfile"]) self.selectedProfile = Profile(**res["selectedProfile"])
if "user" in res:
self.username = res["user"]["username"]
return res return res
async def validate(self) -> dict: async def validate(self, clientToken:bool = True) -> dict:
return await self._post(self.AUTH_SERVER + "/validate", { payload = { "accessToken": self.accessToken }
"accessToken": self.access_token if clientToken:
}) payload["clientToken"] = self.clientToken
return await self._post(self.AUTH_SERVER + "/validate", payload)
async def invalidate(self) -> dict: async def invalidate(self) -> dict:
return await self._post(self.AUTH_SERVER + "/invalidate", { return await self._post(self.AUTH_SERVER + "/invalidate", {
"accessToken": self.access_token, "accessToken": self.accessToken,
"clientToken": self.client_token "clientToken": self.clientToken
}) })
async def join(self, server_id) -> dict: async def join(self, server_id) -> dict:
return await self._post(self.SESSION_SERVER + "/join", { return await self._post(self.SESSION_SERVER + "/join", {
"serverId": server_id, "serverId": server_id,
"accessToken": self.access_token, "accessToken": self.accessToken,
"selectedProfile": self.profile.dict() "selectedProfile": self.selectedProfile.as_dict()
}) })
@classmethod @classmethod