Files
FindMy.py/examples/_login.py
2025-05-25 17:51:24 -06:00

93 lines
2.7 KiB
Python

# ruff: noqa: ASYNC230
from findmy.reports import (
AppleAccount,
AsyncAppleAccount,
BaseAnisetteProvider,
LoginState,
SmsSecondFactorMethod,
TrustedDeviceSecondFactorMethod,
)
ACCOUNT_STORE = "account.json"
def _login_sync(account: AppleAccount) -> None:
email = input("email? > ")
password = input("passwd? > ")
state = account.login(email, password)
if state == LoginState.REQUIRE_2FA: # Account requires 2FA
# This only supports SMS methods for now
methods = account.get_2fa_methods()
# Print the (masked) phone numbers
for i, method in enumerate(methods):
if isinstance(method, TrustedDeviceSecondFactorMethod):
print(f"{i} - Trusted Device")
elif isinstance(method, SmsSecondFactorMethod):
print(f"{i} - SMS ({method.phone_number})")
ind = int(input("Method? > "))
method = methods[ind]
method.request()
code = input("Code? > ")
# This automatically finishes the post-2FA login flow
method.submit(code)
async def _login_async(account: AsyncAppleAccount) -> None:
email = input("email? > ")
password = input("passwd? > ")
state = await account.login(email, password)
if state == LoginState.REQUIRE_2FA: # Account requires 2FA
# This only supports SMS methods for now
methods = await account.get_2fa_methods()
# Print the (masked) phone numbers
for i, method in enumerate(methods):
if isinstance(method, TrustedDeviceSecondFactorMethod):
print(f"{i} - Trusted Device")
elif isinstance(method, SmsSecondFactorMethod):
print(f"{i} - SMS ({method.phone_number})")
ind = int(input("Method? > "))
method = methods[ind]
await method.request()
code = input("Code? > ")
# This automatically finishes the post-2FA login flow
await method.submit(code)
def get_account_sync(anisette: BaseAnisetteProvider) -> AppleAccount:
"""Tries to restore a saved Apple account, or prompts the user for login otherwise. (sync)"""
acc = AppleAccount(anisette=anisette)
acc_store = "account.json"
try:
acc.from_json(acc_store)
except FileNotFoundError:
_login_sync(acc)
acc.to_json(acc_store)
return acc
async def get_account_async(anisette: BaseAnisetteProvider) -> AsyncAppleAccount:
"""Tries to restore a saved Apple account, or prompts the user for login otherwise. (async)"""
acc = AsyncAppleAccount(anisette=anisette)
acc_store = "account.json"
try:
acc.from_json(acc_store)
except FileNotFoundError:
await _login_async(acc)
acc.to_json(acc_store)
return acc