From 5f3b218412e0a5af4ff99ea5f0ba7fa94f87f4c6 Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Fri, 15 Mar 2024 16:10:30 +0800 Subject: [PATCH 1/7] debug (relogin): add debugging print to test on databricks --- graphistry/pygraphistry.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 98916f6a0..723d69e0e 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -384,6 +384,9 @@ def refresh(token=None, fail_silent=False): PyGraphistry._is_authenticated = True return PyGraphistry.api_token() except Exception as e: + print(f"what is relogin :{PyGraphistry.relogin}") + import traceback + print(f"{traceback.format_exc()}") if PyGraphistry.store_token_creds_in_memory(): logger.debug("JWT refresh via creds") logger.debug("2. @PyGraphistry refresh :relogin") From f0e3ba8f5e0ba9a0708ba0cd541c2bb3100115e0 Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Fri, 15 Mar 2024 23:29:38 +0800 Subject: [PATCH 2/7] fix (refresh sso): Fix refresh for sso login in the case JWT token is already expired. --- graphistry/arrow_uploader.py | 10 ++++++++-- graphistry/exceptions.py | 7 +++++++ graphistry/pygraphistry.py | 25 ++++++++++++++----------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/graphistry/arrow_uploader.py b/graphistry/arrow_uploader.py index 1c1f2ff93..3cff0b527 100644 --- a/graphistry/arrow_uploader.py +++ b/graphistry/arrow_uploader.py @@ -5,6 +5,8 @@ from graphistry.privacy import Mode, Privacy from .ArrowFileUploader import ArrowFileUploader + +from .exceptions import TokenExpireException from .validate.validate_encodings import validate_encodings from .util import setup_logger logger = setup_logger(__name__) @@ -362,10 +364,14 @@ def refresh(self, token=None): try: json_response = out.json() if not ('token' in json_response): + if ( + "non_field_errors" in json_response and "Token has expired." in json_response["non_field_errors"] + ): + raise TokenExpireException(out.text) raise Exception(out.text) - except Exception: + except Exception as e: logger.error('Error: %s', out, exc_info=True) - raise Exception(out.text) + raise e self.token = out.json()['token'] return self diff --git a/graphistry/exceptions.py b/graphistry/exceptions.py index 1b7c12d15..3e2aa1b56 100644 --- a/graphistry/exceptions.py +++ b/graphistry/exceptions.py @@ -10,3 +10,10 @@ class SsoRetrieveTokenTimeoutException(SsoException): Koa, 15 Sep 2022 Custom Exception to Sso retrieve token time out exception scenario """ pass + + +class TokenExpireException(Exception): + """ + Koa, 15 Mar 2024 Custom Exception for JWT Token expiry when refresh + """ + pass diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 723d69e0e..609ae74b2 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -15,7 +15,7 @@ from . import bolt_util from .plotter import Plotter from .util import in_databricks, setup_logger, in_ipython, make_iframe -from .exceptions import SsoRetrieveTokenTimeoutException +from .exceptions import SsoRetrieveTokenTimeoutException, TokenExpireException from .messages import ( MSG_REGISTER_MISSING_PASSWORD, @@ -219,7 +219,6 @@ def sso_login(org_name=None, idp_name=None, sso_timeout=SSO_GET_TOKEN_ELAPSE_SEC SSO Login logic. """ - if PyGraphistry._config['store_token_creds_in_memory']: PyGraphistry.relogin = lambda: PyGraphistry.sso_login( org_name, idp_name, sso_timeout, sso_opt_into_type @@ -232,22 +231,27 @@ def sso_login(org_name=None, idp_name=None, sso_timeout=SSO_GET_TOKEN_ELAPSE_SEC + PyGraphistry.server(), # noqa: W503 certificate_validation=PyGraphistry.certificate_validation(), ).sso_login(org_name, idp_name) - try: + # print(f"@sso_login - arrow_uploader.token: {arrow_uploader.token}") if arrow_uploader.token: PyGraphistry.api_token(arrow_uploader.token) PyGraphistry._is_authenticated = True arrow_uploader.token = None return PyGraphistry.api_token() - except Exception: # required to log on - # print("required to log on") + except (Exception, TokenExpireException) as e: # required to log on + + logger.debug(f"@sso_login - arrow_uploader.sso_state: {arrow_uploader.sso_state}") PyGraphistry.sso_state(arrow_uploader.sso_state) auth_url = arrow_uploader.sso_auth_url - # print("auth_url : {}".format(auth_url)) - if auth_url and not PyGraphistry.api_token(): + + # if isinstance(e, TokenExpireException): + # print("Token is expired, you need to relogin") + + if auth_url: # and (not PyGraphistry.api_token() or isinstance(e, TokenExpireException)): PyGraphistry._handle_auth_url(auth_url, sso_timeout, sso_opt_into_type) return auth_url + raise e @staticmethod def _handle_auth_url(auth_url, sso_timeout, sso_opt_into_type): @@ -266,7 +270,6 @@ def _handle_auth_url(auth_url, sso_timeout, sso_opt_into_type): SSO Login logic. """ - if in_ipython() or in_databricks() or sso_opt_into_type == 'display': # If run in notebook, just display the HTML # from IPython.core.display import HTML from IPython.display import display, HTML @@ -384,12 +387,12 @@ def refresh(token=None, fail_silent=False): PyGraphistry._is_authenticated = True return PyGraphistry.api_token() except Exception as e: - print(f"what is relogin :{PyGraphistry.relogin}") - import traceback - print(f"{traceback.format_exc()}") + if PyGraphistry.store_token_creds_in_memory(): logger.debug("JWT refresh via creds") logger.debug("2. @PyGraphistry refresh :relogin") + if isinstance(e, TokenExpireException): + print("Token is expired, you need to relogin") return PyGraphistry.relogin() if not fail_silent: From b07a5964403c96e9bf1e3ff857c09a2cf5060223 Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Sat, 16 Mar 2024 00:00:10 +0800 Subject: [PATCH 3/7] fix (lint): Fix lint issue --- graphistry/pygraphistry.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 609ae74b2..2d789bd9c 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -245,10 +245,7 @@ def sso_login(org_name=None, idp_name=None, sso_timeout=SSO_GET_TOKEN_ELAPSE_SEC auth_url = arrow_uploader.sso_auth_url - # if isinstance(e, TokenExpireException): - # print("Token is expired, you need to relogin") - - if auth_url: # and (not PyGraphistry.api_token() or isinstance(e, TokenExpireException)): + if auth_url: PyGraphistry._handle_auth_url(auth_url, sso_timeout, sso_opt_into_type) return auth_url raise e From 6adf6067169c82c197e5ed2849a81e8dc4939fc5 Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Sat, 30 Mar 2024 14:51:09 +0800 Subject: [PATCH 4/7] debug (databricks): Show count down in dashboard/notebook --- graphistry/pygraphistry.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 2d789bd9c..9e7eeee8b 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -296,8 +296,10 @@ def _handle_auth_url(auth_url, sso_timeout, sso_opt_into_type): try: if not token: if elapsed_time % 10 == 1: - print("Waiting for token : {} seconds ...".format(sso_timeout - elapsed_time + 1)) - + count_down = "Waiting for token : {} seconds ...".format(sso_timeout - elapsed_time + 1) + print(count_down) + from IPython.display import display, HTML + display(HTML(f'{count_down}')) time.sleep(1) elapsed_time = elapsed_time + 1 if elapsed_time > sso_timeout: From cfd5107542481c228b5f550118b014be1406889e Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Sat, 30 Mar 2024 16:00:54 +0800 Subject: [PATCH 5/7] debug (databricks): an attempt to output count down using for loop instead of infinite loop --- graphistry/pygraphistry.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 9e7eeee8b..77c6d7c5a 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -318,10 +318,23 @@ def _handle_auth_url(auth_url, sso_timeout, sso_opt_into_type): print("Successfully logged in") return PyGraphistry.api_token() else: + print("Please run graphistry.sso_get_token() to complete the authentication after you have authenticated via SSO") return None else: - print("Please run graphistry.sso_get_token() to complete the authentication") + print("Start getting token ...") + token = None + for i in range(10): + token, org_name = PyGraphistry._sso_get_token() + if token: + # set org_name to sso org + PyGraphistry._config['org_name'] = org_name + print("Successfully logged in") + return PyGraphistry.api_token() + print("Keep trying to get token ...") + time.sleep(5) + print("Please run graphistry.sso_get_token() to complete the authentication") + return None @staticmethod def sso_get_token(): From 909b28ec17c01024cd5d3ca087d2b8c33d4df1ce Mon Sep 17 00:00:00 2001 From: Vaim Dev Date: Sat, 30 Mar 2024 16:34:55 +0800 Subject: [PATCH 6/7] debug (databricks): revert it back to original code --- graphistry/pygraphistry.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/graphistry/pygraphistry.py b/graphistry/pygraphistry.py index 77c6d7c5a..3e4a96c41 100644 --- a/graphistry/pygraphistry.py +++ b/graphistry/pygraphistry.py @@ -321,17 +321,17 @@ def _handle_auth_url(auth_url, sso_timeout, sso_opt_into_type): print("Please run graphistry.sso_get_token() to complete the authentication after you have authenticated via SSO") return None else: - print("Start getting token ...") - token = None - for i in range(10): - token, org_name = PyGraphistry._sso_get_token() - if token: - # set org_name to sso org - PyGraphistry._config['org_name'] = org_name - print("Successfully logged in") - return PyGraphistry.api_token() - print("Keep trying to get token ...") - time.sleep(5) + # print("Start getting token ...") + # token = None + # for i in range(10): + # token, org_name = PyGraphistry._sso_get_token() + # if token: + # # set org_name to sso org + # PyGraphistry._config['org_name'] = org_name + # print("Successfully logged in") + # return PyGraphistry.api_token() + # print("Keep trying to get token ...") + # time.sleep(5) print("Please run graphistry.sso_get_token() to complete the authentication") return None From 56aaf31234a59afc7f73072278c3324f955eb9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Percy=20Camilo=20Trive=C3=B1o=20Aucahuasi?= Date: Sat, 6 Apr 2024 15:18:32 -0500 Subject: [PATCH 7/7] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d64f49fc2..ea4821aeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Development] +## [0.33.7 - 2024-04-06] + +* Fix refresh() for SSO + ## [0.33.6 - 2024-04-05] ### Added