Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatic password login #36

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/igtcloud/client/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


@contextmanager
def smart_auth(domain, username=None, key=None, auto_refresh=True, auto_logout=True):
def smart_auth(domain, username=None, encoded_password=None, key=None, auto_refresh=True, auto_logout=True):
url = urlparse(domain)
if not url.scheme:
url = urlparse('https://' + domain)
Expand All @@ -43,6 +43,7 @@ def smart_auth(domain, username=None, key=None, auto_refresh=True, auto_logout=T
if auth is None:
with AuthRefresher(domain=domain,
username=username,
encoded_password=encoded_password,
key=key,
auto_refresh=auto_refresh,
auto_logout=auto_logout) as auth:
Expand Down Expand Up @@ -272,7 +273,7 @@ def _extract_jwt_payload(jwt_token: str) -> dict:

return json.loads(base64.b64decode(jwt_payload))

def _login(self, username=None, key=None):
def _login(self, username=None, encoded_password=None, key=None):
if self._token_data is None or self._token_data.get('refresh_token') is None:
logger.info("Login to IGT Cloud ({})".format(self._host))

Expand All @@ -293,7 +294,11 @@ def _login(self, username=None, key=None):
raise RuntimeError('Error during login')
else:
username = username or os.environ.get('CLOUD_USERNAME') or input("Username: ")
password = getpass('{}@{}\'s password: '.format(username, self._host))

if not encoded_password:
password = getpass('{}@{}\'s password: '.format(username, self._host))
else:
password = base64.b64decode(encoded_password).decode('utf-8')

try:
self._oauth_login(username, password)
Expand Down Expand Up @@ -367,7 +372,7 @@ def stop(self):
self._running = False
logger.info("Stopped IGT Cloud authentication handler")

def start(self, domain=None, username=None, key=None, blocking=True, auto_refresh=True, auto_logout=True):
def start(self, domain=None, username=None, encoded_password=None, key=None, blocking=True, auto_refresh=True, auto_logout=True):
self._setup_mmap()
try:
logger.info("Starting IGT Cloud authentication handler...")
Expand All @@ -383,7 +388,7 @@ def start(self, domain=None, username=None, key=None, blocking=True, auto_refres
self._domain = url.geturl()
self._host = url.netloc

self._login(username=username, key=key)
self._login(username=username, encoded_password=encoded_password, key=key)

if auto_refresh:
self._rt.start()
Expand Down
20 changes: 12 additions & 8 deletions src/igtcloud/client/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def cli():
case_sensitive=False))
@click.option('--domain', default=None, help='Overwrites the environment setting')
@click.option('--user', default=None, help='Username')
@click.option('--password', default=None, help='Base64 encoded password')
@click.option('--ext', default=None, help='filter on specific file extension (i.e.: \'.txt\')')
@click.option(
'--start',
Expand Down Expand Up @@ -60,7 +61,7 @@ def cli():
type=click.Choice(['flat', 'hierarchical'], case_sensitive=False),
help='Folder structure of the data to be downloaded.'
)
def download(target_folder, project, institute, environment, domain, user, ext, start, end, category,
def download(target_folder, project, institute, environment, domain, user, password, ext, start, end, category,
include_modified_date, project_files, debug, concurrent_studies, concurrent_files, folder_structure):
"""Download data from Philips Interventional Cloud.

Expand All @@ -79,7 +80,7 @@ def download(target_folder, project, institute, environment, domain, user, ext,
institute = None

domain = _get_domain(domain, environment)
with smart_auth(domain, username=user) as auth:
with smart_auth(domain, username=user, encoded_password=password) as auth:
set_auth(auth)
logger.info(f"Using url: {auth.domain}")

Expand Down Expand Up @@ -111,12 +112,13 @@ def download(target_folder, project, institute, environment, domain, user, ext,
case_sensitive=False))
@click.option('--domain', default=None, help='Overwrites the environment setting')
@click.option('--user', default=None, help='Username')
@click.option('--password', default=None, help='Base64 encoded password')
@click.option('--ext', default=None, help='filter on specific file extension (i.e.: \'.txt\')')
@click.option('--start', default=None, help='start date (YYYY-MM-DD) of date range filter. Default value is 1900-01-01.')
@click.option('--end', default=None, help='end date (YYYY-MM-DD) of date range filter. Default value is 9999-12-31.')
@click.option('--debug', flag_value=True, help='Enable debug logging')
@click.option('--files', flag_value=True, help='Output all files under patient studies')
def csv(target_folder, project, institute, environment, domain, user, ext, start, end, debug, files):
def csv(target_folder, project, institute, environment, domain, user, password, ext, start, end, debug, files):
"""List data from Philips Interventional Cloud in CSV file.

This tool will export all studies/files from INSTITUTE (or all institutes) in project PROJECT to
Expand All @@ -134,7 +136,7 @@ def csv(target_folder, project, institute, environment, domain, user, ext, start
institute = None

domain = _get_domain(domain, environment)
with smart_auth(domain, username=user) as auth:
with smart_auth(domain, username=user, encoded_password=password) as auth:
set_auth(auth)
logger.info(f"Using url: {auth.domain}")
list_project(project, target_folder, institute_name=institute, list_files=files,
Expand All @@ -160,6 +162,7 @@ def _get_domain(domain, environment):
case_sensitive=False))
@click.option('--domain', default=None, help='Overwrites the environment setting')
@click.option('--user', default=None, help='Username')
@click.option('--password', default=None, help='Base64 encoded password')
@click.option('--submit', flag_value=True, help='Set electronic record state to submitted state')
@click.option('--debug', flag_value=True, help='Enable debug logging')
@click.option('--concurrent-studies', type=int, default=None, help='Maximum number of concurrent studies upload')
Expand All @@ -176,7 +179,7 @@ def _get_domain(domain, environment):
type=click.Choice(['annotations'], case_sensitive=False),
help='Uploads annotation files'
)
def upload(local_folder, project, institute, environment, domain, user, submit, debug, concurrent_studies,
def upload(local_folder, project, institute, environment, domain, user, password, submit, debug, concurrent_studies,
concurrent_files, folder_structure, category):
"""Upload data to Philips Interventional Cloud.

Expand All @@ -198,7 +201,7 @@ def upload(local_folder, project, institute, environment, domain, user, submit,
if institute == "*":
institute = None

with smart_auth(domain, username=user) as auth:
with smart_auth(domain, username=user, encoded_password=password) as auth:
set_auth(auth)
logger.info(f"Using url: {auth.domain}")
upload_project(local_folder, project, institute, submit, concurrent_studies, concurrent_files, folder_structure, category)
Expand All @@ -207,16 +210,17 @@ def upload(local_folder, project, institute, environment, domain, user, submit,
@click.command(short_help="Login to Philips Interventional Cloud")
@click.option('-d', '--domain', default=None, type=str)
@click.option('-u', '--user', default=None, type=str)
@click.option('-p', '--password', default=None, type=str, help='Base64 encoded password')
@click.option('-s', '--service-file', default=None, type=str, help='Use a service certificate to perform login')
def login(domain, user, service_file):
def login(domain, user, password, service_file):
from igtcloud.client.core.auth import AuthRefresher
auth_refresher = AuthRefresher()
key = None
if service_file:
user = user or os.path.splitext(os.path.basename(service_file))[0]
with open(service_file) as fp:
key = fp.read()
auth_refresher.start(domain=domain, username=user, key=key)
auth_refresher.start(domain=domain, username=user, encoded_password=password, key=key)


@click.command('get-token', short_help="Get token for Philips Interventional Cloud")
Expand Down
Loading