Skip to content

Commit

Permalink
feat: add --oauth-host option
Browse files Browse the repository at this point in the history
* added option oauth_host to perform oauth authenticaton from remote host

* fixed tests in order to support remote backup
  • Loading branch information
AntonKorobkov committed Apr 22, 2022
1 parent 2507894 commit 0b6b8a6
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 21 deletions.
15 changes: 13 additions & 2 deletions evernote_backup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
help="OAuth local server port. (Advanced option, use with --oauth.)",
)

opt_oauth_host = click.option(
"--oauth-host",
default=config_defaults.OAUTH_HOST,
show_default=True,
help="Oauth host. (Advanced option, use with --oauth.)"
)

opt_token = click.option(
"--token",
"-t",
Expand Down Expand Up @@ -117,7 +124,7 @@ def cli(quiet: bool, verbose: bool) -> None:

@cli.command()
@opt_database
@group_options(opt_user, opt_password, opt_oauth, opt_oauth_port, opt_token)
@group_options(opt_user, opt_password, opt_oauth, opt_oauth_port, opt_oauth_host, opt_token)
@click.option(
"--force",
is_flag=True,
Expand All @@ -137,6 +144,7 @@ def init_db(
password: Optional[str],
oauth: bool,
oauth_port: int,
oauth_host: str,
token: Optional[str],
force: bool,
backend: str,
Expand All @@ -150,6 +158,7 @@ def init_db(
auth_password=password,
auth_is_oauth=oauth,
auth_oauth_port=oauth_port,
auth_oauth_host=oauth_host,
auth_token=token,
force=force,
backend=backend,
Expand Down Expand Up @@ -253,14 +262,15 @@ def export(

@cli.command()
@opt_database
@group_options(opt_user, opt_password, opt_oauth, opt_oauth_port, opt_token)
@group_options(opt_user, opt_password, opt_oauth, opt_oauth_port, opt_oauth_host, opt_token)
@opt_network_retry_count
def reauth(
database: str,
user: Optional[str],
password: Optional[str],
oauth: bool,
oauth_port: int,
oauth_host: str,
token: Optional[str],
network_retry_count: int,
) -> None:
Expand All @@ -272,6 +282,7 @@ def reauth(
auth_password=password,
auth_is_oauth=oauth,
auth_oauth_port=oauth_port,
auth_oauth_host=oauth_host,
auth_token=token,
network_retry_count=network_retry_count,
)
Expand Down
4 changes: 4 additions & 0 deletions evernote_backup/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def init_db(
auth_password: Optional[str],
auth_is_oauth: bool,
auth_oauth_port: int,
auth_oauth_host: str,
auth_token: Optional[str],
force: bool,
backend: str,
Expand All @@ -38,6 +39,7 @@ def init_db(
auth_password,
auth_is_oauth,
auth_oauth_port,
auth_oauth_host,
backend,
network_retry_count,
)
Expand All @@ -63,6 +65,7 @@ def reauth(
auth_password: Optional[str],
auth_is_oauth: bool,
auth_oauth_port: int,
auth_oauth_host: str,
auth_token: Optional[str],
network_retry_count: int,
) -> None:
Expand All @@ -78,6 +81,7 @@ def reauth(
auth_password,
auth_is_oauth,
auth_oauth_port,
auth_oauth_host,
backend,
network_retry_count,
)
Expand Down
3 changes: 2 additions & 1 deletion evernote_backup/cli_app_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def get_auth_token(
auth_password: Optional[str],
auth_is_oauth: bool,
auth_oauth_port: int,
auth_oauth_host: str,
backend: str,
network_retry_count: int,
) -> str:
logger.info("Logging in to Evernote...")

if auth_is_oauth:
return evernote_login_oauth(backend, auth_oauth_port)
return evernote_login_oauth(backend, auth_oauth_port, auth_oauth_host)

return evernote_login_password(
auth_user,
Expand Down
4 changes: 2 additions & 2 deletions evernote_backup/cli_app_auth_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def prompt_ota(delivery_hint: str) -> str:
return str(click.prompt(f"Enter one-time code{one_time_hint}"))


def evernote_login_oauth(backend: str, oauth_port: int) -> str:
def evernote_login_oauth(backend: str, oauth_port: int, oauth_host: str) -> str:
if not is_output_to_terminal():
raise ProgramTerminatedError("OAuth requires user input!")

Expand All @@ -47,7 +47,7 @@ def evernote_login_oauth(backend: str, oauth_port: int) -> str:

oauth_client = get_oauth_client(backend)

oauth_handler = EvernoteOAuthCallbackHandler(oauth_client, oauth_port)
oauth_handler = EvernoteOAuthCallbackHandler(oauth_client, oauth_port, oauth_host)

oauth_url = oauth_handler.get_oauth_url()

Expand Down
1 change: 1 addition & 0 deletions evernote_backup/config_defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
NETWORK_ERROR_RETRY_COUNT = 50
OAUTH_LOCAL_PORT = 10500
OAUTH_HOST = "localhost"
SYNC_CHUNK_MAX_RESULTS = 200
SYNC_MAX_DOWNLOAD_WORKERS = 5
SYNC_DOWNLOAD_CACHE_MEMORY_LIMIT = 256
Expand Down
4 changes: 2 additions & 2 deletions evernote_backup/evernote_client_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def run(self) -> None:


class EvernoteOAuthCallbackHandler(object):
def __init__(self, oauth_client: "EvernoteOAuthClient", oauth_port: int) -> None:
def __init__(self, oauth_client: "EvernoteOAuthClient", oauth_port: int, server_host: str) -> None:
self.client = oauth_client

self.server_host = "localhost"
self.server_host = server_host
self.server_port = oauth_port
self.oauth_token: dict = {}

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def fake_init_db(fake_storage, fake_token, mock_evernote_client):
auth_password=None,
auth_is_oauth=False,
auth_oauth_port=10500,
auth_oauth_host="localhost",
auth_token=fake_token,
force=False,
backend="evernote",
Expand Down
25 changes: 11 additions & 14 deletions tests/test_evernote_client_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
OAuthDeclinedError,
)

FAKE_OAUTH_PORT = 10500
FAKE_OAUTH_HOST = "localhost"


@pytest.fixture
def mock_evernote_oauth_client(mock_oauth_client):
Expand All @@ -19,9 +22,8 @@ def mock_evernote_oauth_client(mock_oauth_client):

@pytest.mark.usefixtures("mock_oauth_http_server")
def test_get_auth_token(mock_oauth_client, mock_evernote_oauth_client):
fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)
oauth_handler.get_oauth_url()

Expand All @@ -33,41 +35,38 @@ def test_get_auth_token(mock_oauth_client, mock_evernote_oauth_client):
def test_server_no_docker(
mock_oauth_client, mock_evernote_oauth_client, mock_oauth_http_server, mocker
):
fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)
oauth_handler.get_oauth_url()

oauth_handler.wait_for_token()

mock_oauth_http_server.assert_any_call(("localhost", fake_oauth_port), mocker.ANY)
mock_oauth_http_server.assert_any_call((FAKE_OAUTH_HOST, FAKE_OAUTH_PORT), mocker.ANY)


def test_server_yes_docker(
mock_oauth_client, mock_evernote_oauth_client, mock_oauth_http_server, mocker
):
os.environ["INSIDE_DOCKER_CONTAINER"] = "1"

fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)
oauth_handler.get_oauth_url()

oauth_handler.wait_for_token()

mock_oauth_http_server.assert_any_call(("0.0.0.0", fake_oauth_port), mocker.ANY)
mock_oauth_http_server.assert_any_call(("0.0.0.0", FAKE_OAUTH_PORT), mocker.ANY)

del os.environ["INSIDE_DOCKER_CONTAINER"]


@pytest.mark.usefixtures("mock_oauth_http_server")
def test_get_auth_token_url(mock_oauth_client, mock_evernote_oauth_client):
expected_url = "https://www.evernote.com/OAuth.action?oauth_token=fake_app.FFF"
fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)

url = oauth_handler.get_oauth_url()
Expand All @@ -79,9 +78,8 @@ def test_get_auth_token_url(mock_oauth_client, mock_evernote_oauth_client):
def test_get_auth_token_declined(mock_oauth_client, mock_evernote_oauth_client):
del mock_oauth_client.fake_callback_response["oauth_verifier"]

fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)
oauth_handler.get_oauth_url()

Expand All @@ -103,9 +101,8 @@ def test_get_auth_token_interrupted(
side_effect=KeyboardInterrupt,
)

fake_oauth_port = 10500
oauth_handler = EvernoteOAuthCallbackHandler(
mock_evernote_oauth_client, fake_oauth_port
mock_evernote_oauth_client, FAKE_OAUTH_PORT, FAKE_OAUTH_HOST
)
oauth_handler.get_oauth_url()

Expand Down

0 comments on commit 0b6b8a6

Please sign in to comment.