From 22c57f6cc852886197f3fd14961b83f0d20f3096 Mon Sep 17 00:00:00 2001 From: DailyDreaming Date: Mon, 3 Jun 2019 12:38:58 -0700 Subject: [PATCH 1/3] Choose an unused port when logging in. --- hca/util/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hca/util/__init__.py b/hca/util/__init__.py index f14fc5c7..4046c9c9 100755 --- a/hca/util/__init__.py +++ b/hca/util/__init__.py @@ -94,6 +94,8 @@ def special_cli_command(self, required_argument, optional_argument=""): import argparse import time import jwt +import contextlib +import socket try: from inspect import signature, Signature, Parameter @@ -125,6 +127,12 @@ class RetryPolicy(retry.Retry): pass +def unused_tcp_port(): + with contextlib.closing(socket.socket()) as sock: + sock.bind(('127.0.0.1', 0)) + return sock.getsockname()[1] + + class _ClientMethodFactory(object): def __init__(self, client, parameters, path_parameters, http_method, method_name, method_data, body_props): self.__dict__.update(locals()) @@ -406,7 +414,7 @@ def login(self, access_token="", remote=False): from google_auth_oauthlib.flow import InstalledAppFlow flow = InstalledAppFlow.from_client_config(self.application_secrets, scopes=scopes) msg = "Authentication successful. Please close this tab and run HCA CLI commands in the terminal." - credentials = flow.run_local_server(success_message=msg, audience=self._audience) + credentials = flow.run_local_server(success_message=msg, audience=self._audience, port=unused_tcp_port) # TODO: (akislyuk) test token autorefresh on expiration self.config.oauth2_token = dict(access_token=credentials.token, From 84128fa5fb920d8cd66f2355d8003db16e365481 Mon Sep 17 00:00:00 2001 From: Lon Blauvelt Date: Mon, 3 Jun 2019 12:42:00 -0700 Subject: [PATCH 2/3] Update __init__.py --- hca/util/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hca/util/__init__.py b/hca/util/__init__.py index 4046c9c9..209a1f0c 100755 --- a/hca/util/__init__.py +++ b/hca/util/__init__.py @@ -414,7 +414,7 @@ def login(self, access_token="", remote=False): from google_auth_oauthlib.flow import InstalledAppFlow flow = InstalledAppFlow.from_client_config(self.application_secrets, scopes=scopes) msg = "Authentication successful. Please close this tab and run HCA CLI commands in the terminal." - credentials = flow.run_local_server(success_message=msg, audience=self._audience, port=unused_tcp_port) + credentials = flow.run_local_server(success_message=msg, audience=self._audience, port=unused_tcp_port()) # TODO: (akislyuk) test token autorefresh on expiration self.config.oauth2_token = dict(access_token=credentials.token, From 7905beb10a472d3e0856998b4e2640e0753405ea Mon Sep 17 00:00:00 2001 From: Lon Blauvelt Date: Mon, 3 Jun 2019 12:47:52 -0700 Subject: [PATCH 3/3] Update __init__.py --- hca/util/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hca/util/__init__.py b/hca/util/__init__.py index 209a1f0c..bfec419e 100755 --- a/hca/util/__init__.py +++ b/hca/util/__init__.py @@ -130,7 +130,7 @@ class RetryPolicy(retry.Retry): def unused_tcp_port(): with contextlib.closing(socket.socket()) as sock: sock.bind(('127.0.0.1', 0)) - return sock.getsockname()[1] + return sock.getsockname()[1] class _ClientMethodFactory(object):