Skip to content

Commit

Permalink
fix keyring handling in icloud (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov authored Jun 18, 2024
1 parent 06ab9e7 commit 4bddc1b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- fix: keyring handling in `icloud`. Rel to [#871](https://github.com/icloud-photos-downloader/icloud_photos_downloader/issues/871)

## 1.20.0 (2024-06-16)

- feature: customize choice and the order of checking for password with `--password-provider` parameter
Expand Down
30 changes: 18 additions & 12 deletions src/pyicloud_ipd/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
command line scripts, and related.
"""
import argparse
import getpass
import pickle
import sys
from typing import NoReturn, Optional, Sequence
Expand Down Expand Up @@ -178,26 +179,31 @@ def main(args:Optional[Sequence[str]]=None) -> NoReturn:

command_line = parser.parse_args(args)

username = command_line.username
password = command_line.password
username: Optional[str] = command_line.username.strip() or None
password: Optional[str] = command_line.password.strip() or None
domain = command_line.domain

if username and command_line.delete_from_keyring:
if username is not None and command_line.delete_from_keyring:
utils.delete_password_in_keyring(username)
print("Password delete from keyring")

failure_count = 0
while True:
# Which password we use is determined by your username, so we
# do need to check for this first and separately.
if not username:
if username is None:
parser.error("No username supplied")

# if not password:
# password = utils.get_password(
# username, interactive=command_line.interactive
# )
got_from_keyring = False

if not password:
if password is None:
password = utils.get_password_from_keyring(username)
got_from_keyring = password is not None

if password is None:
password = getpass.getpass(f'Enter iCloud password for {username}: ').strip() or None

if password is None:
parser.error("No password supplied")

try:
Expand All @@ -207,11 +213,11 @@ def main(args:Optional[Sequence[str]]=None) -> NoReturn:
domain,
RawTreatmentPolicy.AS_IS,
FileMatchPolicy.NAME_SIZE_DEDUP_WITH_SUFFIX,
username.strip(),
password.strip(),
username,
password,
)
if (
not utils.password_exists_in_keyring(username)
not got_from_keyring
and command_line.interactive
and confirm("Save password in keyring?")
):
Expand Down
4 changes: 1 addition & 3 deletions src/pyicloud_ipd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@

def password_exists_in_keyring(username:str) -> bool:
try:
get_password_from_keyring(username)
return get_password_from_keyring(username) is not None
except PyiCloudNoStoredPasswordAvailableException:
return False

return True


def get_password_from_keyring(username:str) -> Optional[str]:
result = keyring.get_password(
Expand Down

0 comments on commit 4bddc1b

Please sign in to comment.