From ed8d8897ddc3b25c2da768b4eaddbb1381703421 Mon Sep 17 00:00:00 2001 From: Daniel McKnight <34697904+NeonDaniel@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:49:17 -0700 Subject: [PATCH] Fix bug causing old token to be used with requests after refreshing (#530) * Fix bug causing old token to be used with requests after refreshing * Improved exception handling * Update token cache to use human-readable URL --------- Co-authored-by: Daniel McKnight --- neon_utils/hana_utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/neon_utils/hana_utils.py b/neon_utils/hana_utils.py index 2da152d6..b4d16666 100644 --- a/neon_utils/hana_utils.py +++ b/neon_utils/hana_utils.py @@ -61,7 +61,7 @@ def set_default_backend_url(url: Optional[str] = None): def _get_client_config_path(url: str): - url_key = hash(url) + url_key = url.split('/')[2] return join(xdg_cache_home(), "neon", f"hana_token_{url_key}.json") @@ -132,6 +132,11 @@ def _refresh_token(backend_address: str): raise ServerException(f"Error updating token from {backend_address}. " f"{update.status_code}: {update.text}") _client_config = update.json() + + # Update request headers with new token + global _headers + _headers['Authorization'] = f"Bearer {_client_config['access_token']}" + client_config_path = _get_client_config_path(backend_address) with open(client_config_path, "w+") as f: json.dump(_client_config, f, indent=2) @@ -184,4 +189,7 @@ def request_backend(endpoint: str, request_data: dict, return resp.json() except Exception as e: LOG.error(e) + # Clear cached config to force re-evaluation on next request + _client_config = {} + _headers = {} raise ServerException(f"Error response {resp.status_code}: {resp.text}")