diff --git a/platformio/commands/account/client.py b/platformio/commands/account/client.py index c2bc3c5f..312b9f36 100644 --- a/platformio/commands/account/client.py +++ b/platformio/commands/account/client.py @@ -43,13 +43,24 @@ class AccountClient(object): adapter = requests.adapters.HTTPAdapter(max_retries=retry) self._session.mount(api_base_url, adapter) + @staticmethod + def get_refresh_token(): + try: + return app.get_state_item("account").get("auth").get("refresh_token") + except: # pylint:disable=bare-except + raise exception.AccountNotAuthorized() + + @staticmethod + def delete_local_session(): + app.delete_state_item("account") + def login(self, username, password): try: self.fetch_authentication_token() except: # pylint:disable=bare-except pass else: - raise exception.AccountAlreadyAuthenticated( + raise exception.AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "") ) @@ -67,7 +78,7 @@ class AccountClient(object): except: # pylint:disable=bare-except pass else: - raise exception.AccountAlreadyAuthenticated( + raise exception.AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "") ) @@ -83,7 +94,8 @@ class AccountClient(object): try: refresh_token = self.get_refresh_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() + self.delete_local_session() response = requests.post( self.api_base_url + "/v1/logout", data={"refresh_token": refresh_token}, ) @@ -91,14 +103,13 @@ class AccountClient(object): self.raise_error_from_response(response) except exception.AccountError: pass - app.delete_state_item("account") return True def change_password(self, old_password, new_password): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() response = self._session.post( self.api_base_url + "/v1/password", headers={"Authorization": "Bearer %s" % token}, @@ -115,7 +126,7 @@ class AccountClient(object): except: # pylint:disable=bare-except pass else: - raise exception.AccountAlreadyAuthenticated( + raise exception.AccountAlreadyAuthorized( app.get_state_item("account", {}).get("email", "") ) @@ -135,7 +146,7 @@ class AccountClient(object): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() response = self._session.post( self.api_base_url + "/v1/token", headers={"Authorization": "Bearer %s" % token}, @@ -153,7 +164,7 @@ class AccountClient(object): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() response = self._session.get( self.api_base_url + "/v1/profile", headers={"Authorization": "Bearer %s" % token}, @@ -164,7 +175,7 @@ class AccountClient(object): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() profile["current_password"] = current_password response = self._session.put( self.api_base_url + "/v1/profile", @@ -177,7 +188,7 @@ class AccountClient(object): if offline: account = app.get_state_item("account") if not account: - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() return { "profile": { "email": account.get("email"), @@ -187,7 +198,7 @@ class AccountClient(object): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() + raise exception.AccountNotAuthorized() response = self._session.get( self.api_base_url + "/v1/summary", headers={"Authorization": "Bearer %s" % token}, @@ -211,19 +222,10 @@ class AccountClient(object): app.set_state_item("account", result) return result.get("auth").get("access_token") except exception.AccountError: - app.delete_state_item("account") - raise exception.AccountNotAuthenticated() + self.delete_local_session() + raise exception.AccountNotAuthorized() - @staticmethod - def get_refresh_token(): - try: - auth = app.get_state_item("account").get("auth").get("refresh_token") - return auth - except: # pylint:disable=bare-except - raise exception.AccountNotAuthenticated() - - @staticmethod - def raise_error_from_response(response, expected_codes=(200, 201, 202)): + def raise_error_from_response(self, response, expected_codes=(200, 201, 202)): if response.status_code in expected_codes: try: return response.json() @@ -234,5 +236,5 @@ class AccountClient(object): except (KeyError, ValueError): message = response.text if "Authorization session has been expired" in message: - app.delete_state_item("account") + self.delete_local_session() raise exception.AccountError(message) diff --git a/platformio/commands/account/command.py b/platformio/commands/account/command.py index 539f1a5b..0177d00a 100644 --- a/platformio/commands/account/command.py +++ b/platformio/commands/account/command.py @@ -167,7 +167,7 @@ def account_update(current_password, **kwargs): return None try: client.logout() - except exception.AccountNotAuthenticated: + except exception.AccountNotAuthorized: pass if email_changed: return click.secho( diff --git a/platformio/commands/account/exception.py b/platformio/commands/account/exception.py index 213be0e1..a1a0059e 100644 --- a/platformio/commands/account/exception.py +++ b/platformio/commands/account/exception.py @@ -20,11 +20,11 @@ class AccountError(PlatformioException): MESSAGE = "{0}" -class AccountNotAuthenticated(AccountError): +class AccountNotAuthorized(AccountError): - MESSAGE = "You are not authenticated! Please login to PIO Account." + MESSAGE = "You are not authorized! Please log in to PIO Account." -class AccountAlreadyAuthenticated(AccountError): +class AccountAlreadyAuthorized(AccountError): - MESSAGE = "You are already authenticated with {0} account." + MESSAGE = "You are already authorized with {0} account." diff --git a/platformio/managers/core.py b/platformio/managers/core.py index cd511e1a..677a1c81 100644 --- a/platformio/managers/core.py +++ b/platformio/managers/core.py @@ -24,7 +24,7 @@ from platformio.proc import get_pythonexe_path from platformio.project.config import ProjectConfig CORE_PACKAGES = { - "contrib-piohome": "~3.2.0", + "contrib-piohome": "~3.2.1", "contrib-pysite": "~2.%d%d.0" % (sys.version_info.major, sys.version_info.minor), "tool-unity": "~1.20500.0", "tool-scons": "~2.20501.7" if PY2 else "~3.30102.0",