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

fix SMTP notification not sent #1007

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 6 additions & 13 deletions src/icloudpd/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
from icloudpd.status import Status, StatusExchange


class TwoStepAuthRequiredError(Exception):
"""
Raised when 2SA is required. base.py catches this exception
and sends an email notification.
"""


def authenticator(
logger: logging.Logger,
domain: str,
Expand All @@ -33,13 +26,13 @@ def authenticator(
],
mfa_provider: MFAProvider,
status_exchange: StatusExchange,
) -> Callable[[str, Optional[str], bool, Optional[str]], PyiCloudService]:
) -> Callable[[str, Optional[str], Optional[Callable[[], None]], Optional[str]], PyiCloudService]:
"""Wraping authentication with domain context"""

def authenticate_(
username: str,
cookie_directory: Optional[str] = None,
raise_error_on_2sa: bool = False,
mfa_error_callable: Optional[Callable[[], None]] = None,
client_id: Optional[str] = None,
) -> PyiCloudService:
"""Authenticate with iCloud username and password"""
Expand Down Expand Up @@ -74,17 +67,17 @@ def authenticate_(
_writer(username, _valid_password)

if icloud.requires_2fa:
if raise_error_on_2sa:
raise TwoStepAuthRequiredError("Two-factor authentication is required")
if mfa_error_callable:
mfa_error_callable()
logger.info("Two-factor authentication is required (2fa)")
if mfa_provider == MFAProvider.WEBUI:
request_2fa_web(icloud, logger, status_exchange)
else:
request_2fa(icloud, logger)

elif icloud.requires_2sa:
if raise_error_on_2sa:
raise TwoStepAuthRequiredError("Two-step authentication is required")
if mfa_error_callable:
mfa_error_callable()
logger.info("Two-step authentication is required (2sa)")
request_2sa(icloud, logger)

Expand Down
44 changes: 19 additions & 25 deletions src/icloudpd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from tzlocal import get_localzone

from icloudpd import constants, download, exif_datetime
from icloudpd.authentication import TwoStepAuthRequiredError, authenticator
from icloudpd.authentication import authenticator
from icloudpd.autodelete import autodelete_photos
from icloudpd.config import Config
from icloudpd.counter import Counter
Expand Down Expand Up @@ -1174,29 +1174,7 @@ def core(
) -> int:
"""Download all iCloud photos to a local directory"""

raise_error_on_2sa = (
smtp_username is not None
or notification_email is not None
or notification_script is not None
)
try:
icloud = authenticator(
logger,
domain,
filename_cleaner,
lp_filename_generator,
raw_policy,
file_match_policy,
password_providers,
mfa_provider,
status_exchange,
)(
username,
cookie_directory,
raise_error_on_2sa,
os.environ.get("CLIENT_ID"),
)
except TwoStepAuthRequiredError:
def _notify_mfa_error():
if notification_script is not None:
subprocess.call([notification_script])
if smtp_username is not None or notification_email is not None:
Expand All @@ -1210,7 +1188,23 @@ def core(
notification_email,
notification_email_from,
)
return 1

icloud = authenticator(
logger,
domain,
filename_cleaner,
lp_filename_generator,
raw_policy,
file_match_policy,
password_providers,
mfa_provider,
status_exchange,
)(
username,
cookie_directory,
_notify_mfa_error,
os.environ.get("CLIENT_ID"),
)

if auth_only:
logger.info("Authentication completed successfully")
Expand Down
Loading