From 4bddc1b4910cc49c56ec54e50563a066467951b2 Mon Sep 17 00:00:00 2001 From: Andrey Nikiforov Date: Tue, 18 Jun 2024 10:55:06 -0700 Subject: [PATCH] fix keyring handling in `icloud` (#874) --- CHANGELOG.md | 2 ++ src/pyicloud_ipd/cmdline.py | 30 ++++++++++++++++++------------ src/pyicloud_ipd/utils.py | 4 +--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e6def1a9..0ae7b8faf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/pyicloud_ipd/cmdline.py b/src/pyicloud_ipd/cmdline.py index 5318fb692..251e7b5fe 100644 --- a/src/pyicloud_ipd/cmdline.py +++ b/src/pyicloud_ipd/cmdline.py @@ -4,6 +4,7 @@ command line scripts, and related. """ import argparse +import getpass import pickle import sys from typing import NoReturn, Optional, Sequence @@ -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: @@ -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?") ): diff --git a/src/pyicloud_ipd/utils.py b/src/pyicloud_ipd/utils.py index 196cb4c0e..02c754ca2 100644 --- a/src/pyicloud_ipd/utils.py +++ b/src/pyicloud_ipd/utils.py @@ -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(