pypush-plus-plus/ids/identity.py

107 lines
4.4 KiB
Python
Raw Normal View History

2023-05-09 15:09:28 -05:00
import plistlib
2023-05-09 16:03:27 -05:00
from base64 import b64decode
2023-05-09 15:09:28 -05:00
import requests
2023-05-09 16:03:27 -05:00
from ._helpers import PROTOCOL_VERSION, USER_AGENT, KeyPair
from .signing import add_auth_signature, armour_cert
2023-07-25 17:46:50 -05:00
from .keydec import IdentityKeys
2023-05-09 16:03:27 -05:00
2023-07-24 08:18:21 -05:00
import logging
logger = logging.getLogger("ids")
2023-05-09 16:03:27 -05:00
def register(
2023-07-25 17:46:50 -05:00
push_token, handles, user_id, auth_key: KeyPair, push_key: KeyPair, published_keys: IdentityKeys, validation_data
2023-05-09 15:09:28 -05:00
):
2023-07-24 08:18:21 -05:00
logger.debug(f"Registering IDS identity for {handles}")
2023-05-09 15:09:28 -05:00
uris = [{"uri": handle} for handle in handles]
body = {
"hardware-version": "MacBookPro18,3",
"language": "en-US",
"os-version": "macOS,13.2.1,22D68",
"software-version": "22D68",
"services": [
{
2023-07-24 15:37:53 -05:00
"capabilities": [{"flags": 17, "name": "Messenger", "version": 1}],
2023-05-09 15:09:28 -05:00
"service": "com.apple.madrid",
"users": [
{
2023-07-24 15:37:53 -05:00
"client-data": {
'is-c2k-equipment': True,
'optionally-receive-typing-indicators': True,
2023-07-25 17:46:50 -05:00
'public-message-identity-key': published_keys.encode(),
# 'public-message-identity-key': b64decode("""MIH2gUMAQQSYmvE+hYOWVGotZUCd
# M6zoW/2clK8RIzUtE6JAmWSCwj7d
# B213vxEBNAPHefEtlxkVKlQH6bsw
# ja5qYyl3Fh28goGuAKwwgakCgaEA
# 4lw3MrXOFIWWIi3TTUGksXVCIz92
# R3AG3ghBa1ZBoZ6rIJHeuxhD2vTV
# hicpW7kvZ/+AFgE4vFFef/9TjG6C
# rsBtWUUfPtYHqc7+uaghVW13qfYC
# tdGsW8Apvf6MJqsRmITJjoYZ5kwl
# scp5Xw/1KVQzKMfZrwZeLC/UZ6O1
# 41u4Xvm+u40e+Ky/wMCOwLGBG0Ag
# ZBH91Xrq+S8izgSLmQIDAQAB""".replace("\n", "").replace(" ", "").replace("\t", "")),
'public-message-identity-version':2,
'show-peer-errors': True,
'supports-ack-v1': True,
'supports-activity-sharing-v1': True,
'supports-audio-messaging-v2': True,
"supports-autoloopvideo-v1": True,
'supports-be-v1': True,
'supports-ca-v1': True,
'supports-fsm-v1': True,
'supports-fsm-v2': True,
'supports-fsm-v3': True,
'supports-ii-v1': True,
'supports-impact-v1': True,
'supports-inline-attachments': True,
'supports-keep-receipts': True,
"supports-location-sharing": True,
'supports-media-v2': True,
'supports-photos-extension-v1': True,
'supports-st-v1': True,
'supports-update-attachments-v1': True,
2023-07-24 15:37:53 -05:00
},
2023-05-09 15:09:28 -05:00
"uris": uris,
2023-05-09 16:03:27 -05:00
"user-id": user_id,
2023-05-09 15:09:28 -05:00
}
],
}
],
"validation-data": b64decode(validation_data),
}
body = plistlib.dumps(body)
headers = {
"x-protocol-version": PROTOCOL_VERSION,
2023-05-09 16:03:27 -05:00
"x-auth-user-id-0": user_id,
2023-05-09 15:09:28 -05:00
}
2023-05-09 16:03:27 -05:00
add_auth_signature(headers, body, "id-register", auth_key, push_key, push_token, 0)
2023-05-09 15:09:28 -05:00
r = requests.post(
"https://identity.ess.apple.com/WebObjects/TDIdentityService.woa/wa/register",
headers=headers,
data=body,
verify=False,
)
r = plistlib.loads(r.content)
2023-07-24 08:18:21 -05:00
#print(f'Response code: {r["status"]}')
logger.debug(f"Recieved response to IDS registration: {r}")
2023-05-09 15:09:28 -05:00
if "status" in r and r["status"] == 6004:
raise Exception("Validation data expired!")
# TODO: Do validation of nested statuses
2023-05-09 16:03:27 -05:00
if "status" in r and r["status"] != 0:
raise Exception(f"Failed to register: {r}")
if not "services" in r:
raise Exception(f"No services in response: {r}")
if not "users" in r["services"][0]:
raise Exception(f"No users in response: {r}")
if not "cert" in r["services"][0]["users"][0]:
raise Exception(f"No cert in response: {r}")
return armour_cert(r["services"][0]["users"][0]["cert"])