pypush-plus-plus/ids/__init__.py

87 lines
3 KiB
Python
Raw Normal View History

2023-05-09 19:36:33 +00:00
from base64 import b64encode
2023-05-09 21:03:27 +00:00
2023-05-09 19:36:33 +00:00
import apns
2023-05-09 21:03:27 +00:00
2023-07-27 15:04:57 +00:00
from . import _helpers, identity, profile, query
2023-05-09 21:03:27 +00:00
2023-05-09 19:36:33 +00:00
class IDSUser:
2023-05-09 20:09:28 +00:00
# Sets self.user_id and self._auth_token
2023-05-09 19:36:33 +00:00
def _authenticate_for_token(
self, username: str, password: str, factor_callback: callable = None
):
2023-05-10 00:01:22 +00:00
self.user_id, self._auth_token = profile.get_auth_token(
2023-05-09 19:36:33 +00:00
username, password, factor_callback
)
2023-05-09 20:09:28 +00:00
# Sets self._auth_keypair using self.user_id and self._auth_token
2023-05-09 19:36:33 +00:00
def _authenticate_for_cert(self):
2023-05-10 00:01:22 +00:00
self._auth_keypair = profile.get_auth_cert(self.user_id, self._auth_token)
2023-05-09 19:36:33 +00:00
# Factor callback will be called if a 2FA code is necessary
def __init__(
self,
push_connection: apns.APNSConnection,
):
self.push_connection = push_connection
2023-05-09 21:03:27 +00:00
self._push_keypair = _helpers.KeyPair(
self.push_connection.private_key, self.push_connection.cert
)
2023-05-09 20:09:28 +00:00
2023-07-26 22:49:41 +00:00
self.ec_key = self.rsa_key = None
2023-05-09 20:09:28 +00:00
def __str__(self):
return f"IDSUser(user_id={self.user_id}, handles={self.handles}, push_token={b64encode(self.push_connection.token).decode()})"
2023-05-09 21:03:27 +00:00
2023-05-09 20:09:28 +00:00
# Authenticates with a username and password, to create a brand new authentication keypair
2023-05-09 21:03:27 +00:00
def authenticate(
self, username: str, password: str, factor_callback: callable = None
):
2023-05-09 19:36:33 +00:00
self._authenticate_for_token(username, password, factor_callback)
self._authenticate_for_cert()
2023-05-10 00:01:22 +00:00
self.handles = profile.get_handles(
2023-05-09 19:36:33 +00:00
b64encode(self.push_connection.token),
self.user_id,
self._auth_keypair,
2023-05-09 20:09:28 +00:00
self._push_keypair,
2023-05-09 19:36:33 +00:00
)
2023-07-31 20:30:06 +00:00
self.current_handle = self.handles[0]
2023-05-09 19:36:33 +00:00
2023-05-09 20:09:28 +00:00
# Uses an existing authentication keypair
2023-05-09 21:03:27 +00:00
def restore_authentication(
self, auth_keypair: _helpers.KeyPair, user_id: str, handles: dict
):
2023-05-09 20:09:28 +00:00
self._auth_keypair = auth_keypair
self.user_id = user_id
2023-07-26 22:49:41 +00:00
self.handles = handles
2023-07-31 20:45:45 +00:00
self.current_handle = self.handles[0]
2023-05-09 21:03:27 +00:00
2023-05-09 20:09:28 +00:00
# This is a separate call so that the user can make sure the first part succeeds before asking for validation data
2023-07-26 22:49:41 +00:00
def register(self, validation_data: str):
"""
self.ec_key, self.rsa_key will be set to a randomly gnenerated EC and RSA keypair
if they are not already set
"""
2023-07-27 15:04:57 +00:00
if self.encryption_identity is None:
self.encryption_identity = identity.IDSIdentity()
2023-07-26 22:49:41 +00:00
2023-05-09 21:03:27 +00:00
cert = identity.register(
2023-05-09 20:09:28 +00:00
b64encode(self.push_connection.token),
self.handles,
self.user_id,
self._auth_keypair,
self._push_keypair,
2023-07-27 15:04:57 +00:00
self.encryption_identity,
2023-05-09 21:03:27 +00:00
validation_data,
2023-05-09 20:09:28 +00:00
)
2023-05-09 21:03:27 +00:00
self._id_keypair = _helpers.KeyPair(self._auth_keypair.key, cert)
def restore_identity(self, id_keypair: _helpers.KeyPair):
self._id_keypair = id_keypair
2023-05-09 22:01:32 +00:00
def lookup(self, uris: list[str], topic: str = "com.apple.madrid") -> any:
2023-07-31 20:30:06 +00:00
return query.lookup(self.push_connection, self.current_handle, self._id_keypair, uris, topic)
2023-05-09 22:01:32 +00:00