diff --git a/hca/util/__init__.py b/hca/util/__init__.py index f14fc5c7..bfec419e 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,