From bcc006fe028d0b90c163e3fcd5e8df1ae64e23a1 Mon Sep 17 00:00:00 2001 From: ndx1905-github <64833823+ndx1905-github@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:49:49 +0200 Subject: [PATCH 1/4] Update config.example.ini Suppression serveur ajout 2fa + nom iphone --- config.example.ini | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config.example.ini b/config.example.ini index f23c82a..a28ebf5 100644 --- a/config.example.ini +++ b/config.example.ini @@ -1,13 +1,11 @@ [apple] email = apple_icloud_email password = apple_icloud_password +code_2fa = 2fa_code +iPhone_name = iPhone cookie_directory = COOKIE_DIRECTORY [nextcloud] server = https://cloud.example.com user = nextcloud_user password = nextcloud_password - -[webserver] -host = localhost -port = 8080 From d9c018fc44e7ba582036913e15d033922afa552d Mon Sep 17 00:00:00 2001 From: ndx1905-github <64833823+ndx1905-github@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:50:40 +0200 Subject: [PATCH 2/4] Update requirements.txt --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 078d9cc..6dc5187 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1 @@ -bottle -cheroot pyicloud From 224658af0d68c98aac3f9eb5b0d393cdb8565e25 Mon Sep 17 00:00:00 2001 From: ndx1905-github <64833823+ndx1905-github@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:51:59 +0200 Subject: [PATCH 3/4] Update icloud_to_nextcloud.py Suppression serveur web Modif nom iphone --- icloud_to_nextcloud.py | 104 +++++------------------------------------ 1 file changed, 12 insertions(+), 92 deletions(-) diff --git a/icloud_to_nextcloud.py b/icloud_to_nextcloud.py index 4ea94cf..ac7eb4b 100644 --- a/icloud_to_nextcloud.py +++ b/icloud_to_nextcloud.py @@ -4,78 +4,12 @@ import logging import sys import urllib.parse -import bottle import requests from pyicloud import PyiCloudService from requests.auth import HTTPBasicAuth -class StoppableCherootServer(bottle.ServerAdapter): - """ - We need a stoppable HTTP server, which can be stopped from within a route. - - This is not doable out of the box in bottle and is quite hacky using plain - WSGIRef. This is easier and cleaner with Cheroot (formally CherryPy) - backend. - """ - def run(self, handler): # pragma: no cover - from cheroot import wsgi - self.options['bind_addr'] = (self.host, self.port) - self.options['wsgi_app'] = handler - self.server = wsgi.Server(**self.options) - try: - self.server.start() - finally: - self.server.stop() - - -############################################ -# Web app to fetch 2FA code from the user. # -############################################ - -code_2fa = None # Global for passing 2FA code from web app to main script -app = bottle.Bottle() -server = None - - -@app.route('/') -def get_2fa(): - """ - Main HTTP route, display an HTML form to fetch 2FA code from user. - """ - return """ - - - - - iCloud 2FA protection - - -
-

- - -

- -
- -""" - - -@app.post('/2fa') -def set_2fa(): - """ - Handle form submission and store 2FA code to pass along the rest of the - code. - """ - global code_2fa - global server - code_2fa = bottle.request.forms.get('2FA') - server.server.stop() - return "OK" - - ############### # Main script # ############### @@ -95,23 +29,16 @@ def get_icloud_location(config): """ Fetch latest iPhone location from iCloud """ - global server - global code_2fa email = config['apple']['email'] password = config['apple']['password'] + code_2fa = config['apple']['code_2fa'] + api = PyiCloudService(email, password=password, cookie_directory=config['apple']['cookie_directory']) if api.requires_2fa: - print( - "Two-factor authentication required. " - f"Head over to http://{config['webserver']['host']}:{config['webserver']['port']} and fill-in the 2FA code." - ) - server = StoppableCherootServer( - host=config['webserver']['host'], - port=int(config['webserver']['port']) - ) - app.run(server=server) - result = api.validate_2fa_code(code_2fa) + print("Two-factor authentication required.") + code = code_2fa + result = api.validate_2fa_code(code) print("Code validation result: %s" % result) if not result: @@ -124,11 +51,7 @@ def get_icloud_location(config): print("Session trust result %s" % result) if not result: - print( - "Failed to request trust. " - "You will likely be prompted for the code again " - "in the coming weeks" - ) + print("Failed to request trust. You will likely be prompted for the code again in the coming weeks") elif api.requires_2sa: import click print("Two-step authentication required. Your trusted devices are:") @@ -136,11 +59,8 @@ def get_icloud_location(config): devices = api.trusted_devices for i, device in enumerate(devices): print( - " %s: %s" % ( - i, device.get( - 'deviceName', "SMS to %s" % device.get('phoneNumber') - ) - ) + " %s: %s" % (i, device.get('deviceName', + "SMS to %s" % device.get('phoneNumber'))) ) device = click.prompt('Which device would you like to use?', default=0) @@ -155,10 +75,10 @@ def get_icloud_location(config): sys.exit(1) iphone = next( - device - for device in api.devices - if 'iPhone' in device.status()['name'] - ) + device + for device in api.devices + if config['apple']['iPhone_name'] in device.status()['name'] + ) iphone_location = iphone.location() iphone_status = iphone.status() From d53ed1d58e5039b5b407cb5fe5f126672547839c Mon Sep 17 00:00:00 2001 From: ndx1905-github <64833823+ndx1905-github@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:08:07 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d76803..2ffabaa 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,18 @@ Run `icloud` program a first time to ensure everything is running smooth: ### Using 2FA -If you enabled 2FA on your Apple iCloud account, the script will run a simple -webserver and point you to the URL on localhost to pass it the 2FA code you -will get from one of your iDevices. This makes the script trustable for some -time and this operation is only required once in a while (every month or so). +If you enabled 2FA on your Apple iCloud account, you can request the 2FA code +beforehand by : +If your device is online: + Go to Settings > [your name]. + Tap Sign-In & Security > Two Factor Authentication. + Tap Get Verification Code +If your device is offline: + Go to Settings > [your name]. + Tap Sign-In & Security. + A message says "Account Details Unavailable." Tap Get Verification Code. +Put this 2FA code in the config.ini file, you have one minute to run the command line in the next section. + ## Usage