def _refresh_qmi_auth_token(self) -> bool: url = f"{self.config.forest_url}/auth/qmi/refresh" headers = { "Content-Type": "application/json", "Accept": "application/json" } data = self.config.qmi_auth_token response = super().request("POST", url, json=data, headers=headers) if response.status_code == 200: self.config.update_qmi_auth_token(response.json()) self.headers.update(self.config.qcs_auth_headers) return True logger.warning( f"Failed to refresh your QMI auth token at {self.config.qmi_auth_token_path}. " f"Server response: {response.text}") return False
def _refresh_user_auth_token(self) -> bool: url = f"{self.config.forest_url}/auth/idp/oauth2/v1/token" headers = { "Content-Type": "application/x-www-form-urlencoded", "Cache-Control": "no-cache", "Accept": "application/json", } assert self.config.user_auth_token is not None data = { "grant_type": "refresh_token", "scope": self.config.user_auth_token["scope"], "refresh_token": self.config.user_auth_token["refresh_token"], } response = super().request("POST", url, data=data, headers=headers) if response.status_code == 200: self.config.update_user_auth_token(response.json()) self.headers.update(self.config.qcs_auth_headers) return True logger.warning( f"Failed to refresh your user auth token at {self.config.user_auth_token_path}. " f"Server response: {response.text}") return False
def _parse_auth_token(path: str, required_keys: Iterable[str]) -> Optional[dict]: try: with open(abspath(expanduser(path)), "r") as f: token = json.load(f) invalid_values = [k for k in required_keys if not isinstance(token.get(k), str)] if len(invalid_values) == 0: return token logger.warning(f"Failed to parse auth token at {path}.") logger.warning(f"Invalid {invalid_values}.") except json.decoder.JSONDecodeError: logger.warning(f"Failed to parse auth token at {path}. Invalid JSON.") except FileNotFoundError: logger.debug("Auth token at %s not found.", path)