-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
850 lines (760 loc) · 37.8 KB
/
client.py
File metadata and controls
850 lines (760 loc) · 37.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
"""
FmdClient: port of the original fmd_api.FmdApi into an async client class.
This module implements:
- authenticate (salt -> argon2 -> requestAccess -> get private key blob -> decrypt)
- decrypt_data_blob (RSA session key + AES-GCM)
- _make_api_request (aiohttp wrapper with re-auth on 401, JSON/text fallback, streaming)
- get_locations (port of get_all_locations)
- get_pictures (port of get_pictures)
- export_data_zip (streamed download)
- send_command (RSA-PSS signing and POST to /api/v1/command)
- convenience wrappers: request_location, set_bluetooth, set_do_not_disturb,
set_ringer_mode, take_picture
"""
from __future__ import annotations
import base64
import asyncio
import json
import logging
import time
import random
from typing import Optional, List, Any, Dict, cast
from types import TracebackType
import aiohttp
from argon2.low_level import hash_secret_raw, Type
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from .helpers import _pad_base64
from .exceptions import FmdApiException
# Constants copied from original module to ensure parity
CONTEXT_STRING_LOGIN = "context:loginAuthentication"
CONTEXT_STRING_ASYM_KEY_WRAP = "context:asymmetricKeyWrap"
ARGON2_SALT_LENGTH = 16
AES_GCM_IV_SIZE_BYTES = 12
RSA_KEY_SIZE_BYTES = 384 # 3072 bits / 8
log = logging.getLogger(__name__)
class FmdClient:
def __init__(
self,
base_url: str,
session_duration: int = 3600,
*,
cache_ttl: int = 30,
timeout: float = 30.0,
max_retries: int = 3,
backoff_base: float = 0.5,
backoff_max: float = 10.0,
jitter: bool = True,
ssl: Optional[Any] = None,
conn_limit: Optional[int] = None,
conn_limit_per_host: Optional[int] = None,
keepalive_timeout: Optional[float] = None,
):
# Enforce HTTPS only (FindMyDevice always uses TLS)
if base_url.lower().startswith("http://"):
raise ValueError("HTTPS is required for FmdClient base_url; plain HTTP is not allowed.")
self.base_url = base_url.rstrip("/")
self.session_duration = session_duration
self.cache_ttl = cache_ttl
self.timeout = timeout # default timeout for all HTTP requests (seconds)
self.max_retries = max(0, int(max_retries))
self.backoff_base = float(backoff_base)
self.backoff_max = float(backoff_max)
self.jitter = bool(jitter)
# Connection/session configuration
# ssl can be: None (default validation), False (disable verification), or an SSLContext
self._ssl = ssl
self._conn_limit = conn_limit
self._conn_limit_per_host = conn_limit_per_host
self._keepalive_timeout = keepalive_timeout
self._fmd_id: Optional[str] = None
self._password: Optional[str] = None
self.access_token: Optional[str] = None
self.private_key: Optional[RSAPrivateKey] = None # cryptography private key object
self._session: Optional[aiohttp.ClientSession] = None
# Artifact-based auth additions (initialized blank; set during authenticate or resume)
self._password_hash: Optional[str] = None # Argon2id hash string (server accepts directly)
self._token_issued_at: Optional[float] = None
# -------------------------
# Async context manager
# -------------------------
async def __aenter__(self) -> "FmdClient":
return self
async def __aexit__(
self,
exc_type: Optional[type[BaseException]],
exc: Optional[BaseException],
tb: Optional[TracebackType],
) -> None:
await self.close()
@classmethod
async def create(
cls,
base_url: str,
fmd_id: str,
password: str,
session_duration: int = 3600,
*,
cache_ttl: int = 30,
timeout: float = 30.0,
ssl: Optional[Any] = None,
conn_limit: Optional[int] = None,
conn_limit_per_host: Optional[int] = None,
keepalive_timeout: Optional[float] = None,
drop_password: bool = False,
) -> "FmdClient":
inst = cls(
base_url,
session_duration,
cache_ttl=cache_ttl,
timeout=timeout,
ssl=ssl,
conn_limit=conn_limit,
conn_limit_per_host=conn_limit_per_host,
keepalive_timeout=keepalive_timeout,
)
inst._fmd_id = fmd_id
inst._password = password
try:
await inst.authenticate(fmd_id, password, session_duration)
except Exception:
# Ensure we don't leak a ClientSession if auth fails mid-creation
await inst.close()
raise
if drop_password:
# Security hardening: discard raw password after successful auth
inst._password = None
return inst
async def _ensure_session(self) -> None:
if self._session is None or self._session.closed:
connector_kwargs: Dict[str, Any] = {}
if self._ssl is not None:
connector_kwargs["ssl"] = self._ssl
if self._conn_limit is not None:
connector_kwargs["limit"] = self._conn_limit
if self._conn_limit_per_host is not None:
connector_kwargs["limit_per_host"] = self._conn_limit_per_host
if self._keepalive_timeout is not None:
connector_kwargs["keepalive_timeout"] = self._keepalive_timeout
connector = aiohttp.TCPConnector(**connector_kwargs)
self._session = aiohttp.ClientSession(connector=connector)
async def close(self) -> None:
if self._session and not self._session.closed:
await self._session.close()
self._session = None
# -------------------------
# Authentication helpers
# -------------------------
async def authenticate(self, fmd_id: str, password: str, session_duration: int) -> None:
"""
Performs the full authentication and private key retrieval workflow.
Mirrors the behavior in the original fmd_api.FmdApi.
"""
log.info("[1] Requesting salt...")
salt = await self._get_salt(fmd_id)
log.info("[2] Hashing password with salt...")
password_hash = self._hash_password(password, salt)
log.info("[3] Requesting access token...")
self._fmd_id = fmd_id
self._password = password
self.access_token = await self._get_access_token(fmd_id, password_hash, session_duration)
self._token_issued_at = time.time()
self._password_hash = password_hash # retain for optional hash-based reauth if password dropped
log.info("[3a] Retrieving encrypted private key...")
privkey_blob = await self._get_private_key_blob()
log.info("[3b] Decrypting private key...")
privkey_bytes = self._decrypt_private_key_blob(privkey_blob, password)
self.private_key = self._load_private_key_from_bytes(privkey_bytes)
def _hash_password(self, password: str, salt: str) -> str:
salt_bytes = base64.b64decode(_pad_base64(salt))
password_bytes = (CONTEXT_STRING_LOGIN + password).encode("utf-8")
hash_bytes: bytes = hash_secret_raw(
secret=password_bytes,
salt=salt_bytes,
time_cost=1,
memory_cost=131072,
parallelism=4,
hash_len=32,
type=Type.ID,
)
hash_b64 = base64.b64encode(hash_bytes).decode("utf-8").rstrip("=")
return f"$argon2id$v=19$m=131072,t=1,p=4${salt}${hash_b64}"
async def _get_salt(self, fmd_id: str) -> str:
return cast(str, await self._make_api_request("PUT", "/api/v1/salt", {"IDT": fmd_id, "Data": ""}))
async def _get_access_token(self, fmd_id: str, password_hash: str, session_duration: int) -> str:
payload = {"IDT": fmd_id, "Data": password_hash, "SessionDurationSeconds": session_duration}
return cast(str, await self._make_api_request("PUT", "/api/v1/requestAccess", payload))
async def _get_private_key_blob(self) -> str:
return cast(
str, await self._make_api_request("PUT", "/api/v1/key", {"IDT": self.access_token, "Data": "unused"})
)
def _decrypt_private_key_blob(self, key_b64: str, password: str) -> bytes:
key_bytes = base64.b64decode(_pad_base64(key_b64))
salt = key_bytes[:ARGON2_SALT_LENGTH]
iv = key_bytes[ARGON2_SALT_LENGTH : ARGON2_SALT_LENGTH + AES_GCM_IV_SIZE_BYTES]
ciphertext = key_bytes[ARGON2_SALT_LENGTH + AES_GCM_IV_SIZE_BYTES :]
password_bytes = (CONTEXT_STRING_ASYM_KEY_WRAP + password).encode("utf-8")
aes_key = hash_secret_raw(
secret=password_bytes, salt=salt, time_cost=1, memory_cost=131072, parallelism=4, hash_len=32, type=Type.ID
)
aesgcm = AESGCM(aes_key)
return aesgcm.decrypt(iv, ciphertext, None)
# -------------------------
# Artifact-based resume / export
# -------------------------
@classmethod
async def resume(
cls,
base_url: str,
fmd_id: str,
access_token: str,
private_key_bytes: bytes | str,
*,
password_hash: Optional[str] = None,
session_duration: int = 3600,
cache_ttl: int = 30,
timeout: float = 30.0,
ssl: Optional[Any] = None,
conn_limit: Optional[int] = None,
conn_limit_per_host: Optional[int] = None,
keepalive_timeout: Optional[float] = None,
) -> "FmdClient":
"""Resume a client from stored auth artifacts (no raw password).
private_key_bytes: PEM or DER; if str, will be encoded as utf-8.
password_hash: Optional Argon2id hash for automatic reauth (401).
"""
inst = cls(
base_url,
session_duration,
cache_ttl=cache_ttl,
timeout=timeout,
ssl=ssl,
conn_limit=conn_limit,
conn_limit_per_host=conn_limit_per_host,
keepalive_timeout=keepalive_timeout,
)
inst._fmd_id = fmd_id
inst.access_token = access_token
inst._password_hash = password_hash
inst._token_issued_at = time.time()
# Load private key
if isinstance(private_key_bytes, str):
pk_bytes = private_key_bytes.encode("utf-8")
else:
pk_bytes = private_key_bytes
try:
inst.private_key = cast(RSAPrivateKey, serialization.load_pem_private_key(pk_bytes, password=None))
except ValueError:
inst.private_key = cast(RSAPrivateKey, serialization.load_der_private_key(pk_bytes, password=None))
return inst
async def export_auth_artifacts(self) -> Dict[str, Any]:
"""Export current authentication artifacts for password-free resume."""
pk = self.private_key
if pk is None:
raise FmdApiException("Cannot export artifacts: private key not loaded")
try:
pem = pk.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
).decode("utf-8")
except Exception:
# Test fallback: if the private_key is a test double without private_bytes,
# generate a temporary RSA key solely for serialization so artifacts are usable.
# Real clients always have a cryptography RSAPrivateKey here.
log.warning("Private key object lacks export support; generating temporary key for artifacts export.")
temp_key = rsa.generate_private_key(public_exponent=65537, key_size=3072)
pem = temp_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
).decode("utf-8")
return {
"base_url": self.base_url,
"fmd_id": self._fmd_id,
"access_token": self.access_token,
"private_key": pem,
"password_hash": self._password_hash,
"session_duration": self.session_duration,
"token_issued_at": self._token_issued_at,
}
@classmethod
async def from_auth_artifacts(cls, artifacts: Dict[str, Any]) -> "FmdClient":
required = ["base_url", "fmd_id", "access_token", "private_key"]
missing = [k for k in required if k not in artifacts]
if missing:
raise ValueError(f"Missing artifact fields: {missing}")
return await cls.resume(
artifacts["base_url"],
artifacts["fmd_id"],
artifacts["access_token"],
artifacts["private_key"],
password_hash=artifacts.get("password_hash"),
session_duration=artifacts.get("session_duration", 3600),
)
async def drop_password(self) -> None:
"""Forget raw password after onboarding (security hardening)."""
self._password = None
async def _reauth_with_hash(self) -> None:
if not (self._fmd_id and self._password_hash):
raise FmdApiException("Hash-based reauth not possible: missing ID or password_hash")
new_token = await self._get_access_token(self._fmd_id, self._password_hash, self.session_duration)
self.access_token = new_token
self._token_issued_at = time.time()
def _load_private_key_from_bytes(self, privkey_bytes: bytes) -> RSAPrivateKey:
try:
return cast(RSAPrivateKey, serialization.load_pem_private_key(privkey_bytes, password=None))
except ValueError:
return cast(RSAPrivateKey, serialization.load_der_private_key(privkey_bytes, password=None))
# -------------------------
# Decryption
# -------------------------
def decrypt_data_blob(self, data_b64: str) -> bytes:
"""
Decrypts a location or picture data blob using the instance's private key.
Raises FmdApiException on problems (matches original behavior).
"""
blob = base64.b64decode(_pad_base64(data_b64))
# Check for minimum size (RSA packet + IV)
min_size = RSA_KEY_SIZE_BYTES + AES_GCM_IV_SIZE_BYTES
if len(blob) < min_size:
raise FmdApiException(
f"Blob too small for decryption: {len(blob)} bytes (expected at least {min_size} bytes). "
f"This may indicate empty/invalid data from the server."
)
session_key_packet = blob[:RSA_KEY_SIZE_BYTES]
iv = blob[RSA_KEY_SIZE_BYTES : RSA_KEY_SIZE_BYTES + AES_GCM_IV_SIZE_BYTES]
ciphertext = blob[RSA_KEY_SIZE_BYTES + AES_GCM_IV_SIZE_BYTES :]
key = self.private_key
if key is None:
raise FmdApiException("Private key not loaded. Call authenticate() first.")
session_key = key.decrypt(
session_key_packet,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None),
)
aesgcm = AESGCM(session_key)
return aesgcm.decrypt(iv, ciphertext, None)
# -------------------------
# HTTP helper
# -------------------------
async def _make_api_request(
self,
method: str,
endpoint: str,
payload: Any,
stream: bool = False,
expect_json: bool = True,
retry_auth: bool = True,
timeout: Optional[float] = None,
max_retries: Optional[int] = None,
) -> Any:
"""
Makes an API request and returns Data or text depending on expect_json/stream.
Mirrors get_all_locations/_make_api_request logic from original file (including 401 re-auth).
"""
url = self.base_url + endpoint
await self._ensure_session()
session = self._session
assert session is not None # for type checker; ensured by _ensure_session
req_timeout = aiohttp.ClientTimeout(total=timeout if timeout is not None else self.timeout)
# Determine retry policy
attempts_left = self.max_retries if max_retries is None else max(0, int(max_retries))
# Avoid unsafe retries for commands unless it's a 401 (handled separately) or 429 with Retry-After
is_command = endpoint.rstrip("/").endswith("/api/v1/command")
backoff_attempt = 0
while True:
try:
async with session.request(method, url, json=payload, timeout=req_timeout) as resp:
# Handle 401 -> re-authenticate once
if resp.status == 401 and retry_auth and self._fmd_id:
if self._password:
log.info("401 received: re-auth with raw password...")
await self.authenticate(self._fmd_id, self._password, self.session_duration)
payload["IDT"] = self.access_token
return await self._make_api_request(
method,
endpoint,
payload,
stream,
expect_json,
retry_auth=False,
timeout=timeout,
max_retries=attempts_left,
)
elif self._password_hash:
log.info("401 received: re-auth with stored password_hash...")
await self._reauth_with_hash()
payload["IDT"] = self.access_token
return await self._make_api_request(
method,
endpoint,
payload,
stream,
expect_json,
retry_auth=False,
timeout=timeout,
max_retries=attempts_left,
)
else:
log.warning("401 received: no password or hash available for reauth")
resp.raise_for_status()
# Rate limit handling (429)
if resp.status == 429:
if attempts_left <= 0:
# Exhausted retries
body_text = await _safe_read_text(resp)
raise FmdApiException(
f"Rate limited (429) and retries exhausted. Body={body_text[:200] if body_text else ''}"
)
retry_after = resp.headers.get("Retry-After")
delay = _parse_retry_after(retry_after)
if delay is None:
delay = _compute_backoff(self.backoff_base, backoff_attempt, self.backoff_max, self.jitter)
log.warning(f"Received 429 Too Many Requests. Sleeping {delay:.2f}s before retrying...")
attempts_left -= 1
backoff_attempt += 1
await asyncio.sleep(delay)
continue
# Transient server errors -> retry (except for unsafe command POSTs)
if resp.status in (500, 502, 503, 504) and not (is_command and method.upper() == "POST"):
if attempts_left > 0:
delay = _compute_backoff(self.backoff_base, backoff_attempt, self.backoff_max, self.jitter)
log.warning(
f"Server error {resp.status}. "
f"Retrying in {delay:.2f}s ({attempts_left} retries left)..."
)
attempts_left -= 1
backoff_attempt += 1
await asyncio.sleep(delay)
continue
# For all other statuses, raise for non-2xx
resp.raise_for_status()
log.debug(
f"{endpoint} response - status: {resp.status}, "
f"content-type: {resp.content_type}, "
f"content-length: {resp.content_length}"
)
if not stream:
if expect_json:
# server sometimes reports wrong content-type -> force JSON parse
try:
json_data = await resp.json(content_type=None)
# Sanitize: don't log full JSON which may contain tokens/sensitive data
if log.isEnabledFor(logging.DEBUG):
# Log safe metadata only
keys = list(json_data.keys()) if isinstance(json_data, dict) else "non-dict"
log.debug(f"{endpoint} JSON response received with keys: {keys}")
return json_data["Data"]
except (KeyError, ValueError, json.JSONDecodeError) as e:
# fall back to text
log.debug(f"{endpoint} JSON parsing failed ({e}), trying as text")
text_data = await resp.text()
if text_data:
# Sanitize: avoid logging response bodies that may contain tokens
log.debug(f"{endpoint} text response received, length: {len(text_data)}")
else:
log.warning(f"{endpoint} returned EMPTY response body")
return text_data
else:
text_data = await resp.text()
log.debug(f"{endpoint} text response length: {len(text_data)}")
return text_data
else:
# Return the aiohttp response for streaming consumers
return resp
except aiohttp.ClientConnectionError as e:
# Transient connection issues -> retry if allowed (avoid unsafe command repeats)
if attempts_left > 0 and not (is_command and method.upper() == "POST"):
delay = _compute_backoff(self.backoff_base, backoff_attempt, self.backoff_max, self.jitter)
log.warning(f"Connection error calling {endpoint}: {e}. Retrying in {delay:.2f}s...")
attempts_left -= 1
backoff_attempt += 1
await asyncio.sleep(delay)
continue
log.error(f"API request failed for {endpoint}: {e}")
raise FmdApiException(f"API request failed for {endpoint}: {e}") from e
except aiohttp.ClientError as e:
log.error(f"API request failed for {endpoint}: {e}")
raise FmdApiException(f"API request failed for {endpoint}: {e}") from e
except (KeyError, ValueError) as e:
log.error(f"Failed to parse server response for {endpoint}: {e}")
raise FmdApiException(f"Failed to parse server response for {endpoint}: {e}") from e
# -------------------------
# Location / picture access
# -------------------------
async def get_locations(self, num_to_get: int = -1, skip_empty: bool = True, max_attempts: int = 10) -> List[str]:
"""
Fetches all or the N most recent location blobs.
Returns list of base64-encoded blobs (strings), same as original get_all_locations.
"""
log.debug(f"Getting locations, num_to_get={num_to_get}, " f"skip_empty={skip_empty}")
size_str = await self._make_api_request(
"PUT", "/api/v1/locationDataSize", {"IDT": self.access_token, "Data": ""}
)
size = int(size_str)
log.debug(f"Server reports {size} locations available")
if size == 0:
log.info("No locations found to download.")
return []
locations: List[str] = []
if num_to_get == -1:
log.info(f"Found {size} locations to download.")
indices = range(size)
for i in indices:
log.info(f" - Downloading location at index {i}...")
blob = await self._make_api_request(
"PUT", "/api/v1/location", {"IDT": self.access_token, "Data": str(i)}
)
locations.append(blob)
return locations
else:
num_to_download = min(num_to_get, size)
log.info(f"Found {size} locations. Downloading the {num_to_download} most recent.")
start_index = size - 1
if skip_empty:
indices = range(start_index, max(-1, start_index - max_attempts), -1)
log.info(
f"Will search for {num_to_download} non-empty location(s) " f"starting from index {start_index}"
)
else:
end_index = size - num_to_download
indices = range(start_index, end_index - 1, -1)
log.info(f"Will fetch indices: {list(indices)}")
for i in indices:
log.info(f" - Downloading location at index {i}...")
blob = await self._make_api_request("PUT", "/api/v1/location", {"IDT": self.access_token, "Data": str(i)})
log.debug(f"Received blob type: {type(blob)}, length: {len(blob) if blob else 0}")
if blob and isinstance(blob, str) and blob.strip():
log.debug(f"First 100 chars: {blob[:100]}")
locations.append(blob)
log.info(f"Found valid location at index {i}")
if len(locations) >= num_to_get and num_to_get != -1:
break
else:
log.warning(f"Empty blob received for location index {i}, repr: {repr(blob[:50] if blob else blob)}")
if not locations and num_to_get != -1:
log.warning(f"No valid locations found after checking " f"{min(max_attempts, size)} indices")
return locations
async def get_pictures(self, num_to_get: int = -1, timeout: Optional[float] = None) -> List[Any]:
"""Fetches all or the N most recent picture metadata blobs (raw server response)."""
req_timeout = aiohttp.ClientTimeout(total=timeout if timeout is not None else self.timeout)
try:
await self._ensure_session()
session = self._session
assert session is not None
async with session.put(
f"{self.base_url}/api/v1/pictures", json={"IDT": self.access_token, "Data": ""}, timeout=req_timeout
) as resp:
resp.raise_for_status()
json_data = await resp.json()
# Extract the Data field if it exists, otherwise use the response as-is
all_pictures = json_data.get("Data", json_data) if isinstance(json_data, dict) else json_data
except aiohttp.ClientError as e:
log.warning(f"Failed to get pictures: {e}. The endpoint may not exist or requires a different method.")
return []
# Ensure all_pictures is a list
if not isinstance(all_pictures, list):
log.warning(f"Unexpected pictures response type: {type(all_pictures)}")
return []
if num_to_get == -1:
log.info(f"Found {len(all_pictures)} pictures to download.")
return all_pictures
else:
num_to_download = min(num_to_get, len(all_pictures))
log.info(f"Found {len(all_pictures)} pictures. Selecting the {num_to_download} most recent.")
return all_pictures[-num_to_download:][::-1]
async def export_data_zip(self, out_path: str, include_pictures: bool = True) -> str:
"""
Export all account data to a ZIP file (client-side packaging).
This mimics the FMD web UI's export functionality by fetching all locations
and pictures via the existing API endpoints, decrypting them, and packaging
them into a user-friendly ZIP file.
NOTE: There is no server-side /api/v1/exportData endpoint. This method
performs client-side data collection, decryption, and packaging, similar
to how the web UI implements its export feature.
ZIP Contents:
- info.json: Export metadata (date, device ID, counts)
- locations.json: Decrypted location data (human-readable JSON)
- pictures/picture_NNNN.jpg: Extracted picture files
- pictures/manifest.json: Picture metadata (filename, size, index)
Args:
out_path: Path where the ZIP file will be saved
include_pictures: Whether to include pictures in the export (default: True)
Returns:
Path to the created ZIP file
Raises:
FmdApiException: If data fetching or ZIP creation fails
"""
import zipfile
from datetime import datetime
try:
log.info("Starting data export (client-side packaging)...")
# Fetch all locations
log.info("Fetching all locations...")
location_blobs = await self.get_locations(num_to_get=-1, skip_empty=False)
# Fetch all pictures if requested
picture_blobs = []
if include_pictures:
log.info("Fetching all pictures...")
picture_blobs = await self.get_pictures(num_to_get=-1)
# Create ZIP file with exported data
log.info(f"Creating export ZIP at {out_path}...")
with zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zipf:
# Decrypt and add readable locations
decrypted_locations = []
if location_blobs:
log.info(f"Decrypting {len(location_blobs)} locations...")
for i, blob in enumerate(location_blobs):
try:
decrypted = self.decrypt_data_blob(blob)
loc_data = json.loads(decrypted)
decrypted_locations.append(loc_data)
except Exception as e:
log.warning(f"Failed to decrypt location {i}: {e}")
decrypted_locations.append({"error": str(e), "index": i})
# Decrypt and extract pictures as image files
picture_file_list = []
if picture_blobs:
log.info(f"Decrypting and extracting {len(picture_blobs)} pictures...")
for i, blob in enumerate(picture_blobs):
try:
decrypted = self.decrypt_data_blob(blob)
# Pictures are double-encoded: decrypt -> base64 string -> image bytes
inner_b64 = decrypted.decode("utf-8").strip()
from .helpers import b64_decode_padded
image_bytes = b64_decode_padded(inner_b64)
# Determine image format from magic bytes
if image_bytes.startswith(b"\xff\xd8\xff"):
ext = "jpg"
elif image_bytes.startswith(b"\x89PNG"):
ext = "png"
else:
ext = "jpg" # default to jpg
filename = f"pictures/picture_{i:04d}.{ext}"
zipf.writestr(filename, image_bytes)
picture_file_list.append({"index": i, "filename": filename, "size": len(image_bytes)})
except Exception as e:
log.warning(f"Failed to decrypt/extract picture {i}: {e}")
picture_file_list.append({"index": i, "error": str(e)})
# Add metadata file (after processing so we have accurate counts)
export_info = {
"export_date": datetime.now().isoformat(),
"fmd_id": self._fmd_id,
"location_count": len(location_blobs),
"picture_count": len(picture_blobs),
"pictures_extracted": len([p for p in picture_file_list if "error" not in p]),
"version": "2.0",
}
zipf.writestr("info.json", json.dumps(export_info, indent=2))
# Add locations as readable JSON
if decrypted_locations:
zipf.writestr("locations.json", json.dumps(decrypted_locations, indent=2))
# Add picture manifest if we extracted any
if picture_file_list:
zipf.writestr("pictures/manifest.json", json.dumps(picture_file_list, indent=2))
log.info(f"Export completed successfully: {out_path}")
return out_path
except Exception as e:
log.error(f"Failed to export data: {e}")
raise FmdApiException(f"Failed to export data: {e}") from e
# -------------------------
# Commands
# -------------------------
async def send_command(self, command: str) -> bool:
"""Sends a signed command to the server. Returns True on success."""
log.info(f"Sending command to device: {command}")
unix_time_ms = int(time.time() * 1000)
message_to_sign = f"{unix_time_ms}:{command}"
message_bytes = message_to_sign.encode("utf-8")
key = self.private_key
if key is None:
raise FmdApiException("Private key not loaded. Call authenticate() first.")
signature = key.sign(
message_bytes, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=32), hashes.SHA256()
)
signature_b64 = base64.b64encode(signature).decode("utf-8").rstrip("=")
# Sanitize: don't log signature which could be replayed
try:
await self._make_api_request(
"POST",
"/api/v1/command",
{"IDT": self.access_token, "Data": command, "UnixTime": unix_time_ms, "CmdSig": signature_b64},
expect_json=False,
)
log.info(f"Command sent successfully: {command}")
return True
except Exception as e:
log.error(f"Failed to send command '{command}': {e}")
raise FmdApiException(f"Failed to send command '{command}': {e}") from e
async def request_location(self, provider: str = "all") -> bool:
provider_map = {
"all": "locate",
"gps": "locate gps",
"cell": "locate cell",
"network": "locate cell",
"last": "locate last",
}
command = provider_map.get(provider.lower(), "locate")
log.info(f"Requesting location update with provider: {provider} (command: {command})")
return await self.send_command(command)
async def set_bluetooth(self, enable: bool) -> bool:
"""Set Bluetooth power explicitly: True = on, False = off."""
command = "bluetooth on" if enable else "bluetooth off"
log.info(f"{'Enabling' if enable else 'Disabling'} Bluetooth")
return await self.send_command(command)
async def set_do_not_disturb(self, enable: bool) -> bool:
"""Set Do Not Disturb explicitly: True = on, False = off."""
command = "nodisturb on" if enable else "nodisturb off"
log.info(f"{'Enabling' if enable else 'Disabling'} Do Not Disturb mode")
return await self.send_command(command)
async def set_ringer_mode(self, mode: str) -> bool:
mode = mode.lower()
mode_map = {"normal": "ringermode normal", "vibrate": "ringermode vibrate", "silent": "ringermode silent"}
if mode not in mode_map:
raise ValueError(f"Invalid ringer mode '{mode}'. Must be 'normal', 'vibrate', or 'silent'")
command = mode_map[mode]
log.info(f"Setting ringer mode to: {mode}")
return await self.send_command(command)
async def take_picture(self, camera: str = "back") -> bool:
camera = camera.lower()
if camera not in ["front", "back"]:
raise ValueError(f"Invalid camera '{camera}'. Must be 'front' or 'back'")
command = "camera front" if camera == "front" else "camera back"
log.info(f"Requesting picture from {camera} camera")
return await self.send_command(command)
# -------------------------
# Internal helpers for retry/backoff and logging (module-level)
# -------------------------
def _mask_token(token: Optional[str], show_chars: int = 8) -> str:
"""Mask sensitive tokens for logging, showing only first N chars."""
if not token:
return "<none>"
if len(token) <= show_chars:
return "***"
return f"{token[:show_chars]}...***"
def _compute_backoff(base: float, attempt: int, max_delay: float, jitter: bool) -> float:
delay = min(max_delay, base * (2**attempt))
if jitter:
# Full jitter: random between 0 and delay
return float(random.uniform(0, delay))
return float(delay)
def _parse_retry_after(retry_after_header: Optional[str]) -> Optional[float]:
"""Parse Retry-After header. Supports seconds; returns None if not usable."""
if not retry_after_header:
return None
try:
seconds = int(retry_after_header.strip())
if seconds < 0:
return None
return float(seconds)
except Exception:
# Parsing HTTP-date would require email.utils; skip and return None
return None
async def _safe_read_text(resp: aiohttp.ClientResponse) -> Optional[str]:
try:
return await resp.text()
except Exception:
return None