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

offer to save user password if it is entered interactively with the icloudpd command #661

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- fix: pypi.org license and description
- feature: offer to save user password if it is entered interactively

## 1.14.5 (2023-07-06)

Expand Down
6 changes: 6 additions & 0 deletions src/icloudpd/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def authenticate_(
"""Authenticate with iCloud username and password"""
logger = setup_logger()
logger.debug("Authenticating...")
password_was_entered = False
while True:
try:
# If password not provided on command line variable will be set to None
Expand All @@ -35,10 +36,15 @@ def authenticate_(
cookie_directory=cookie_directory,
client_id=client_id,
)
if password_was_entered and click.confirm(
"Save password in keyring? "):
pyicloud_ipd.utils.store_password_in_keyring(
username, password)
break
except pyicloud_ipd.exceptions.NoStoredPasswordAvailable:
# Prompt for password if not stored in PyiCloud's keyring
password = click.prompt("iCloud Password", hide_input=True)
password_was_entered = True

if icloud.requires_2sa:
if raise_error_on_2sa:
Expand Down
40 changes: 38 additions & 2 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,49 @@ def test_password_prompt(self):
"-d",
base_dir,
],
input="password1\n",
input="password1\n\n",
)
self.assertIn("DEBUG Authenticating...", self._caplog.text)
self.assertIn(
"DEBUG Looking up all photos and videos from album All Photos...",
self._caplog.text
self._caplog.text)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)
assert result.exit_code == 0

files_in_result = glob.glob(os.path.join(
base_dir, "**/*.*"), recursive=True)

assert sum(1 for _ in files_in_result) == 0

def test_password_prompt_save(self):
base_dir = os.path.join(self.fixtures_path, inspect.stack()[0][3])
recreate_path(base_dir)

with vcr.use_cassette(os.path.join(self.vcr_path, "listing_photos.yml")):
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--recent",
"0",
"--no-progress-bar",
"-d",
base_dir,
],
input="password1\ny\n",
)
pyicloud_ipd.utils.delete_password_in_keyring(
"[email protected]")
self.assertIn("DEBUG Authenticating...", self._caplog.text)
self.assertIn(
"DEBUG Looking up all photos and videos from album All Photos...",
self._caplog.text)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)
Expand Down