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

[easy] Choose an unused port when logging in. #353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion hca/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down