Skip to content
Merged
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
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ case "\$1" in
exec /app/icloudpd "\$@"
;;
*)
# Default to icloudpd
exec /app/icloudpd "\$@"
echo "Error: You must specify either 'icloud' or 'icloudpd' as the first argument."
echo "Usage: docker run <image> icloudpd [options]"
echo " or: docker run <image> icloud [options]"
exit 1
;;
esac
EOF
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ RUN \
RUN \
. .venv/bin/activate && \
echo "Building binaries..." && \
scripts/build_bin2 icloudpd icloud && \
scripts/build_bin1 icloudpd && \
scripts/build_bin1 icloud && \
scripts/build_static icloudpd && \
scripts/build_static icloud && \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.build-musl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ RUN \
pip3 install --disable-pip-version-check . --group dev --group devlinux
RUN \
. .venv/bin/activate && \
scripts/build_bin2 icloudpd icloud && \
scripts/build_bin1 icloudpd && \
scripts/build_bin1 icloud && \
scripts/build_whl

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ classifiers = [
dependencies = [
"requests==2.32.3",
"schema==0.7.7",
"click==8.1.8",
"tqdm==4.67.1",
"piexif==1.1.3",
"python-dateutil==2.9.0.post0",
Expand Down
68 changes: 43 additions & 25 deletions src/icloudpd/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from functools import partial
from typing import Any, Callable, Dict, List, Mapping, Tuple

import click

from icloudpd.mfa_provider import MFAProvider
from icloudpd.status import Status, StatusExchange
from pyicloud_ipd.base import PyiCloudService
Expand All @@ -16,6 +14,30 @@
from pyicloud_ipd.raw_policy import RawTreatmentPolicy


def prompt_int_range(message: str, default: str, min_val: int, max_val: int) -> int:
"""Prompt user for integer input within a range, similar to click.IntRange"""
while True:
try:
response = input(f"{message} [{default}]: ").strip() or default
value = int(response)
if min_val <= value <= max_val:
return value
else:
print(f"Invalid input: {value} is not in the range {min_val}-{max_val}")
except ValueError:
print(f"Invalid input: '{response}' is not a valid integer")


def prompt_string(message: str) -> str:
"""Prompt user for string input"""
return input(f"{message}: ")


def echo(message: str) -> None:
"""Print message to stdout, similar to click.echo"""
print(message)


def authenticator(
logger: logging.Logger,
domain: str,
Expand Down Expand Up @@ -93,18 +115,16 @@
number = device["phoneNumber"]
alt_name = f"SMS to {number}"
name = device.get("deviceName", alt_name)
click.echo(f" {i}: {name}")
echo(f" {i}: {name}")

device_index = click.prompt(
"Please choose an option:", default="0", type=click.IntRange(0, devices_count - 1)
)
device_index = prompt_int_range("Please choose an option:", "0", 0, devices_count - 1)

device = devices[device_index]
if not icloud.send_verification_code(device):
logger.error("Failed to send two-step authentication code")
sys.exit(1)

code = click.prompt("Please enter two-step authentication code")
code = prompt_string("Please enter two-step authentication code")
if not icloud.validate_verification_code(device, code):
logger.error("Failed to verify two-step authentication code")
sys.exit(1)
Expand All @@ -127,47 +147,47 @@
raise PyiCloudFailedMFAException("Too many trusted devices for authentication")

for i, device in enumerate(devices):
click.echo(f" {device_index_alphabet[i]}: {device.obfuscated_number}")
echo(f" {device_index_alphabet[i]}: {device.obfuscated_number}")

index_str = f"..{device_index_alphabet[devices_count - 1]}" if devices_count > 1 else ""
index_or_code: str = ""
while True:
index_or_code = (
click.prompt(
f"Please enter two-factor authentication code or device index ({device_index_alphabet[0]}{index_str}) to send SMS with a code",
prompt_string(
f"Please enter two-factor authentication code or device index ({device_index_alphabet[0]}{index_str}) to send SMS with a code"
)
.strip()
.lower()
)

if index_or_code == "":
click.echo("Empty string. Try again")
echo("Empty string. Try again")
continue

if len(index_or_code) == 1:
if index_or_code in device_index_alphabet:
if device_index_alphabet.index(index_or_code) > devices_count - 1:
click.echo(
f"Invalid index, should be ({device_index_alphabet[0]}{index_str}). Try again",
echo(
f"Invalid index, should be ({device_index_alphabet[0]}{index_str}). Try again"
)
continue
else:
break
else:
click.echo(
f"Invalid index, should be ({device_index_alphabet[0]}{index_str}). Try again",
echo(
f"Invalid index, should be ({device_index_alphabet[0]}{index_str}). Try again"
)
continue

if len(index_or_code) == 6:
if index_or_code.isdigit():
break
else:
click.echo("Invalid code, should be six digits. Try again")
echo("Invalid code, should be six digits. Try again")
continue

click.echo(
f"Should be index ({device_index_alphabet[0]}{index_str}) or six-digit code. Try again",
echo(
f"Should be index ({device_index_alphabet[0]}{index_str}) or six-digit code. Try again"
)

if index_or_code in device_index_alphabet:
Expand All @@ -177,12 +197,12 @@
if not icloud.send_2fa_code_sms(device.id):
raise PyiCloudFailedMFAException("Failed to send two-factor authentication code")
while True:
code: str = click.prompt(
"Please enter two-factor authentication code that you received over SMS",
code: str = prompt_string(
"Please enter two-factor authentication code that you received over SMS"
).strip()
if len(code) == 6 and code.isdigit():
break
click.echo("Invalid code, should be six digits. Try again")
echo("Invalid code, should be six digits. Try again")

if not icloud.validate_2fa_code_sms(device.id, code):
raise PyiCloudFailedMFAException("Failed to verify two-factor authentication code")
Expand All @@ -191,12 +211,10 @@
raise PyiCloudFailedMFAException("Failed to verify two-factor authentication code")
else:
while True:
code = click.prompt(
"Please enter two-factor authentication code",
).strip()
code = prompt_string("Please enter two-factor authentication code").strip()
if len(code) == 6 and code.isdigit():
break
click.echo("Invalid code, should be six digits. Try again")
echo("Invalid code, should be six digits. Try again")
if not icloud.validate_2fa_code(code):
raise PyiCloudFailedMFAException("Failed to verify two-factor authentication code")
logger.info(
Expand Down
Loading
Loading