From af71383f5defec67a45a91214135265bbe9b44ea Mon Sep 17 00:00:00 2001 From: JJTech0130 Date: Thu, 24 Aug 2023 07:31:11 -0400 Subject: [PATCH] stash trying to test sms registration --- demo.py | 31 +++++++++++++++++++++++++++++-- ids/__init__.py | 24 ++++++++++++++++++++---- ids/identity.py | 19 +++++++++++++++---- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/demo.py b/demo.py index e224acf..d36e334 100644 --- a/demo.py +++ b/demo.py @@ -46,7 +46,7 @@ except FileNotFoundError: CONFIG = {} # Re-register if the commit hash has changed -if CONFIG.get("commit_hash") != commit_hash: +if CONFIG.get("commit_hash") != commit_hash or True: logging.warning("pypush commit is different, forcing re-registration...") CONFIG["commit_hash"] = commit_hash if "id" in CONFIG: @@ -86,11 +86,35 @@ async def main(): user.authenticate(username, password) + import sms_registration + phone_sig = safe_b64decode(CONFIG.get("phone", {}).get("sig")) + phone_number = CONFIG.get("phone", {}).get("number") + + if phone_sig is None or phone_number is None: + print("Registering phone number...") + phone_number, phone_sig = sms_registration.register(user.push_connection.credentials.token) + CONFIG["phone"] = { + "number": phone_number, + "sig": b64encode(phone_sig).decode(), + } + if CONFIG.get("phone", {}).get("auth_key") is not None: + phone_auth_keypair = ids._helpers.KeyPair(CONFIG["phone"]["auth_key"], CONFIG["phone"]["auth_cert"]) + else: + phone_auth_keypair = ids.profile.get_phone_cert(phone_number, user.push_connection.credentials.token, [phone_sig]) + CONFIG["phone"]["auth_key"] = phone_auth_keypair.key + CONFIG["phone"]["auth_cert"] = phone_auth_keypair.cert + + user.encryption_identity = ids.identity.IDSIdentity( encryption_key=CONFIG.get("encryption", {}).get("rsa_key"), signing_key=CONFIG.get("encryption", {}).get("ec_key"), ) + #user._auth_keypair = phone_auth_keypair + #user.handles = [f"tel:{phone_number}"] + #user.user_id = f"P:{phone_number}" + + if ( CONFIG.get("id", {}).get("cert") is not None and user.encryption_identity is not None @@ -104,7 +128,10 @@ async def main(): vd = emulated.nac.generate_validation_data() vd = b64encode(vd).decode() - user.register(vd) + user.register(vd, [("P:" + phone_number, phone_auth_keypair)], ["tel:" + phone_number, "tel:1"]) + #user.register(vd) + + print("Handles: ", user.handles) # Write config.json CONFIG["encryption"] = { diff --git a/ids/__init__.py b/ids/__init__.py index 5c78b55..4641766 100644 --- a/ids/__init__.py +++ b/ids/__init__.py @@ -50,7 +50,7 @@ class IDSUser: # Uses an existing authentication keypair def restore_authentication( - self, auth_keypair: _helpers.KeyPair, user_id: str, handles: dict + self, auth_keypair: _helpers.KeyPair, user_id: str, handles: list ): self._auth_keypair = auth_keypair self.user_id = user_id @@ -58,7 +58,7 @@ class IDSUser: self.current_handle = self.handles[0] # This is a separate call so that the user can make sure the first part succeeds before asking for validation data - def register(self, validation_data: str): + def register(self, validation_data: str, additional_keys: list[tuple[str, _helpers.KeyPair]] = [], additional_handles: list[str] = []): """ self.ec_key, self.rsa_key will be set to a randomly gnenerated EC and RSA keypair if they are not already set @@ -66,18 +66,34 @@ class IDSUser: if self.encryption_identity is None: self.encryption_identity = identity.IDSIdentity() - + auth_keys = [(self.user_id, self._auth_keypair)] + auth_keys.extend(additional_keys) + + handles_request = self.handles + + handles_request.extend(additional_handles) + + cert = identity.register( b64encode(self.push_connection.credentials.token), self.handles, self.user_id, - self._auth_keypair, + auth_keys, self._push_keypair, self.encryption_identity, validation_data, ) self._id_keypair = _helpers.KeyPair(self._auth_keypair.key, cert) + # Refresh handles + self.handles = profile.get_handles( + b64encode(self.push_connection.credentials.token), + self.user_id, + self._auth_keypair, + self._push_keypair, + ) + + def restore_identity(self, id_keypair: _helpers.KeyPair): self._id_keypair = id_keypair diff --git a/ids/identity.py b/ids/identity.py index f7ee5e3..cf8f2c3 100644 --- a/ids/identity.py +++ b/ids/identity.py @@ -90,7 +90,7 @@ class IDSIdentity: return output.getvalue() def register( - push_token, handles, user_id, auth_key: KeyPair, push_key: KeyPair, identity: IDSIdentity, validation_data + push_token, handles, user_id, auth_keys: list[tuple[str, KeyPair]], push_key: KeyPair, identity: IDSIdentity, validation_data ): logger.debug(f"Registering IDS identity for {handles}") uris = [{"uri": handle} for handle in handles] @@ -141,20 +141,31 @@ def register( }, "uris": uris, "user-id": user_id, - } + + }, + # { + # "uris": uris, + # "user-id": auth_keys[1][0] + # } ], } ], "validation-data": b64decode(validation_data), } + logger.debug(body) + body = plistlib.dumps(body) headers = { "x-protocol-version": PROTOCOL_VERSION, - "x-auth-user-id-0": user_id, + #"x-auth-user-id-0": user_id, } - add_auth_signature(headers, body, "id-register", auth_key, push_key, push_token, 0) + for i, (user_id, keypair) in enumerate(auth_keys): + headers[f"x-auth-user-id-{i}"] = user_id + add_auth_signature(headers, body, "id-register", keypair, push_key, push_token, i) + + print(headers) r = requests.post( "https://identity.ess.apple.com/WebObjects/TDIdentityService.woa/wa/register",