Skip to content

Commit

Permalink
Use sso token and cookies to query and to refresh token #TASK-7102
Browse files Browse the repository at this point in the history
  • Loading branch information
dapregi committed Dec 20, 2024
1 parent a6e50d7 commit 17dd0c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
42 changes: 26 additions & 16 deletions opencga-client/src/main/python/pyopencga/opencga_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


class OpencgaClient(object):
def __init__(self, configuration, token=None, on_retry=None, auto_refresh=True):
def __init__(self, configuration, token=None, refresh_token=None, on_retry=None, auto_refresh=True):
"""
:param on_retry: callback to be called with client retries an operation.
It must accept parameters: client, exc_type, exc_val, exc_tb, call
Expand All @@ -44,9 +44,11 @@ def __init__(self, configuration, token=None, on_retry=None, auto_refresh=True):
self.clients = []
self._login_handler = None
self.token = token
self.refreshToken = None
self.refresh_token = refresh_token
self._create_clients()
self._check_versions()
if self.configuration.sso_login:
self._sso_login()

def __enter__(self):
return self
Expand Down Expand Up @@ -102,7 +104,7 @@ def _create_clients(self):
for client in self.clients:
client.on_retry = self.on_retry

def _make_login_handler(self, user, password, organization):
def _make_login_handler(self, user=None, password=None, organization=None):
"""
Returns a closure that performs the log-in. This will be called on retries
if the current session ever expires.
Expand All @@ -113,39 +115,47 @@ def _make_login_handler(self, user, password, organization):
"""
def login_handler(refresh=False):
if refresh:
# TODO Change to refreshToken whenever it is properly implemented in OpenCGA
# data = {'refreshToken': self.refresh_token}
data = {'user': user, 'password': password}
data = {'refreshToken': self.refresh_token}
else:
data = {'user': user, 'password': password}
if organization:
data.update({'organization': organization})
tokens = User(self.configuration).login(data=data).get_result(0)
self.token = tokens['token']
self.refresh_token = tokens['refreshToken']
return self.token
return self.token, self.refresh_token
return login_handler

def login(self, user=None, password=None, organization=None):
if user is not None:
if password is None:
password = getpass.getpass()

try:
assert user and password
except AssertionError:
raise ValueError("User and password required")
if user is not None and password is None:
password = getpass.getpass()
if not (user and password):
raise ValueError('User and password required')

self._login_handler = self._make_login_handler(user, password, organization)
self._login_handler = self._make_login_handler(user=user, password=password, organization=organization)
self._login_handler()
for client in self.clients:
client.token = self.token
client.refresh_token = self.refresh_token
client.login_handler = self._login_handler

def _sso_login(self):
# Getting token and refresh token from configuration
self.token = self.configuration.token
self.refresh_token = self.configuration.token

self._login_handler = self._make_login_handler()
for client in self.clients:
client.token = self.token
client.refresh_token = self.refresh_token
client.login_handler = self._login_handler

def logout(self):
self.token = None
self.refresh_token = None
for client in self.clients:
client.token = self.token
client.refresh_token = self.refresh_token

def wait_for_job(self, response=None, study_id=None, job_id=None, retry_seconds=10):
if response is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ def __init__(self, configuration, token=None, login_handler=None,
self.auto_refresh = auto_refresh
self._cfg = configuration
self.token = token
self.refresh_token = None
self.login_handler = login_handler
self.on_retry = None

def _client_login_handler(self):
if self.login_handler:
self.token = self.login_handler()
self.token, self.refresh_token = self.login_handler()

def _refresh_token_client(self):
if self.login_handler:
self.token = self.login_handler(refresh=True)
self.token, self.refresh_token = self.login_handler(refresh=True)

@staticmethod
def _get_query_id_str(query_ids):
Expand Down

0 comments on commit 17dd0c0

Please sign in to comment.