From f78ffaded03bfd875264124623612c8ffa7669ce Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 6 May 2020 12:29:01 +0300 Subject: [PATCH] Remove local PIO Account session from PIO Remote when token is broken --- platformio/commands/remote/factory/client.py | 22 ++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/platformio/commands/remote/factory/client.py b/platformio/commands/remote/factory/client.py index 202c7da6..26abe080 100644 --- a/platformio/commands/remote/factory/client.py +++ b/platformio/commands/remote/factory/client.py @@ -13,7 +13,7 @@ # limitations under the License. from twisted.cred import credentials # pylint: disable=import-error -from twisted.internet import protocol, reactor # pylint: disable=import-error +from twisted.internet import defer, protocol, reactor # pylint: disable=import-error from twisted.spread import pb # pylint: disable=import-error from platformio.app import get_host_id @@ -35,17 +35,27 @@ class RemoteClientFactory(pb.PBClientFactory, protocol.ReconnectingClientFactory self.remote_client.log.info("Successfully connected") self.remote_client.log.info("Authenticating") + auth_token = None + try: + auth_token = AccountClient().fetch_authentication_token() + except Exception as e: # pylint:disable=broad-except + d = defer.Deferred() + d.addErrback(self.clientAuthorizationFailed) + d.errback(pb.Error(e)) + return d + d = self.login( - credentials.UsernamePassword( - AccountClient().fetch_authentication_token().encode(), - get_host_id().encode(), - ), + credentials.UsernamePassword(auth_token.encode(), get_host_id().encode(),), client=self.remote_client, ) d.addCallback(self.remote_client.cb_client_authorization_made) - d.addErrback(self.remote_client.cb_client_authorization_failed) + d.addErrback(self.clientAuthorizationFailed) return d + def clientAuthorizationFailed(self, err): + AccountClient.delete_local_session() + self.remote_client.cb_client_authorization_failed(err) + def clientConnectionFailed(self, connector, reason): self.remote_client.log.warn( "Could not connect to PIO Remote Cloud. Reconnecting..."