def get_profile(self): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = self._session.get( self.api_base_url + "/v1/profile", headers={"Authorization": "Bearer %s" % token}, ) return self.raise_error_from_response(response)
def get_account_info(self, offline): if offline: account = app.get_state_item("account") if not account: raise exception.AccountNotAuthenticated() return { "profile": { "email": account.get("email"), "username": account.get("username"), } } try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = self._session.get( self.api_base_url + "/v1/summary", headers={"Authorization": "Bearer %s" % token}, ) return self.raise_error_from_response(response)
def auth_token(self, password, regenerate): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = self._session.post( self.api_base_url + "/v1/token", headers={"Authorization": "Bearer %s" % token}, data={"password": password, "regenerate": 1 if regenerate else 0}, ) return self.raise_error_from_response(response).get("auth_token")
def change_password(self, old_password, new_password): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = self._session.post( self.api_base_url + "/v1/password", headers={"Authorization": "Bearer %s" % token}, data={"old_password": old_password, "new_password": new_password}, ) self.raise_error_from_response(response) return True
def update_profile(self, profile, current_password): try: token = self.fetch_authentication_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() profile["current_password"] = current_password response = self._session.put( self.api_base_url + "/v1/profile", headers={"Authorization": "Bearer %s" % token}, data=profile, ) return self.raise_error_from_response(response)
def logout(self): try: refresh_token = self.get_refresh_token() except: # pylint:disable=bare-except raise exception.AccountNotAuthenticated() response = requests.post( self.api_base_url + "/v1/logout", data={"refresh_token": refresh_token}, ) try: self.raise_error_from_response(response) except exception.AccountError: pass app.delete_state_item("account") return True
def fetch_authentication_token(self): if "PLATFORMIO_AUTH_TOKEN" in os.environ: return os.environ["PLATFORMIO_AUTH_TOKEN"] auth = app.get_state_item("account", {}).get("auth", {}) if auth.get("access_token") and auth.get("access_token_expire"): if auth.get("access_token_expire") > time.time(): return auth.get("access_token") if auth.get("refresh_token"): response = self._session.post( self.api_base_url + "/v1/login", headers={"Authorization": "Bearer %s" % auth.get("refresh_token")}, ) result = self.raise_error_from_response(response) app.set_state_item("account", result) return result.get("auth").get("access_token") raise exception.AccountNotAuthenticated()
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()