Skip to content

Commit

Permalink
fix SMTP notification not sent
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebruyn committed Nov 26, 2024
1 parent 1a64294 commit f762617
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
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
39 changes: 17 additions & 22 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,11 +1174,21 @@ 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
)
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:
send_2sa_notification(
logger,
smtp_username,
smtp_password,
smtp_host,
smtp_port,
smtp_no_tls,
notification_email,
notification_email_from,
)

try:
icloud = authenticator(
logger,
Expand All @@ -1193,24 +1203,9 @@ def core(
)(
username,
cookie_directory,
raise_error_on_2sa,
_notify_mfa_error,
os.environ.get("CLIENT_ID"),
)
except TwoStepAuthRequiredError:
if notification_script is not None:
subprocess.call([notification_script])
if smtp_username is not None or notification_email is not None:
send_2sa_notification(
logger,
smtp_username,
smtp_password,
smtp_host,
smtp_port,
smtp_no_tls,
notification_email,
notification_email_from,
)
return 1

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

0 comments on commit f762617

Please sign in to comment.