From a8b5c707394f607159fef3ac27e3a1719920fffe Mon Sep 17 00:00:00 2001 From: alemidev Date: Thu, 17 Feb 2022 10:22:15 +0100 Subject: [PATCH] handle code better, refresh fixes --- aiocraft/client.py | 11 ++++++----- aiocraft/mc/auth/microsoft.py | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/aiocraft/client.py b/aiocraft/client.py index c70c25c..8fe43b5 100644 --- a/aiocraft/client.py +++ b/aiocraft/client.py @@ -76,7 +76,7 @@ class MinecraftClient(CallbacksHolder, Runnable): self.options = ClientOptions(**kwargs) - self.code = login_code + self.code = login_code or None # TODO put this directly in the authenticator maybe? self._username = username self._authenticator = MicrosoftAuthenticator(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri) self.online_mode = online_mode @@ -122,12 +122,13 @@ class MinecraftClient(CallbacksHolder, Runnable): try: await self._authenticator.validate() # will raise an exc if token is invalid except AuthException: - if self._authenticator.refreshable: + if self.code: + await self._authenticator.login(self.code) + self.code = None + self._logger.info("Logged in with OAuth code") + elif self._authenticator.refreshable: await self._authenticator.refresh() self._logger.warning("Refreshed Token") - elif self.code: - await self._authenticator.login(self.code) - self._logger.info("Logged in with OAuth code") else: raise ValueError("No refreshable auth or code to login") self._authenticated = True diff --git a/aiocraft/mc/auth/microsoft.py b/aiocraft/mc/auth/microsoft.py index 2bccbe8..8ec3b12 100644 --- a/aiocraft/mc/auth/microsoft.py +++ b/aiocraft/mc/auth/microsoft.py @@ -94,12 +94,13 @@ class MicrosoftAuthenticator(AuthInterface): "client_id": self.client_id, "client_secret": self.client_secret, "redirect_uri": self.redirect_uri, - "grant_type": "authorization_code", } if code: payload['code'] = code + payload['grant_type'] = 'authorization_code' elif self.refreshToken: payload['refresh_token'] = self.refreshToken + payload['grant_type'] = 'refresh_token' else: raise InvalidStateError("Missing auth code and refresh token") auth_response = await self._post(