|
| 1 | +import logging |
| 2 | +import uuid |
| 3 | +from typing import Tuple |
| 4 | +import requests |
| 5 | + |
| 6 | +from jwskate import JweCompact |
| 7 | +from jwskate.jwk.rsa import RSAJwk |
| 8 | +from binapy.binapy import BinaPy |
| 9 | + |
| 10 | +from s2python.generated.gen_s2_pairing import (Protocols, |
| 11 | + PairingRequest, |
| 12 | + S2NodeDescription, |
| 13 | + PairingResponse, |
| 14 | + ConnectionRequest, |
| 15 | + ConnectionDetails) |
| 16 | + |
| 17 | + |
| 18 | +logger = logging.getLogger("s2python") |
| 19 | + |
| 20 | +class S2Pairing: # pylint: disable=too-many-instance-attributes |
| 21 | + paired: bool |
| 22 | + s2_server_node_id: str |
| 23 | + server_node_description: str |
| 24 | + selected_protocol: Protocols |
| 25 | + connection_uri: str |
| 26 | + challenge: BinaPy |
| 27 | + |
| 28 | + _request_pairing_endpoint: str |
| 29 | + _token: str |
| 30 | + _s2_client_node_description: S2NodeDescription |
| 31 | + _verify_certificate: bool | str # pylint: disable=E1131 |
| 32 | + _client_node_id: str |
| 33 | + _supported_protocols: Tuple[Protocols] |
| 34 | + _rsa_key_pair: RSAJwk |
| 35 | + def __init__( # pylint: disable=too-many-arguments |
| 36 | + self, |
| 37 | + request_pairing_endpoint: str, |
| 38 | + token: str, |
| 39 | + s2_client_node_description: S2NodeDescription, |
| 40 | + verify_certificate: bool | str = False, # pylint: disable=E1131 |
| 41 | + client_node_id: str = str(uuid.uuid4()), |
| 42 | + supported_protocols: Tuple[Protocols] = (Protocols.WebSocketSecure, ) |
| 43 | + ) -> None: |
| 44 | + self.paired = False |
| 45 | + |
| 46 | + self._request_pairing_endpoint = request_pairing_endpoint |
| 47 | + self._token = token |
| 48 | + self._s2_client_node_description = s2_client_node_description |
| 49 | + self._verify_certificate = verify_certificate |
| 50 | + self._client_node_id = client_node_id |
| 51 | + self._supported_protocols = supported_protocols |
| 52 | + self._rsa_key_pair = RSAJwk(self._rsa_key_pair) |
| 53 | + |
| 54 | + def pair(self) -> bool: |
| 55 | + self.paired = False |
| 56 | + pairing_request: PairingRequest = PairingRequest(token=self._token, |
| 57 | + publicKey=self._rsa_key_pair.public_jwk().to_pem(), |
| 58 | + s2ClientNodeId=self._client_node_id, |
| 59 | + s2ClientNodeDescription=self._s2_client_node_description, |
| 60 | + supportedProtocols=self._supported_protocols) |
| 61 | + |
| 62 | + response = requests.post(self._request_pairing_endpoint, |
| 63 | + json=pairing_request.model_dump_json(), |
| 64 | + timeout=10, |
| 65 | + verify = self._verify_certificate) |
| 66 | + response.raise_for_status() |
| 67 | + pairing_response: PairingResponse = PairingResponse.parse_raw(response.json()) |
| 68 | + self.s2_server_node_id = pairing_response.s2ServerNodeId |
| 69 | + self.server_node_description = pairing_response.serverNodeDescription |
| 70 | + |
| 71 | + connection_request: ConnectionRequest = ConnectionRequest(s2ClientNodeId=self._client_node_id, |
| 72 | + supportedProtocols=self._supported_protocols) |
| 73 | + |
| 74 | + response = requests.post(pairing_response.requestConnectionUri, |
| 75 | + json=connection_request.model_dump_json(), |
| 76 | + timeout=10, |
| 77 | + verify = self._verify_certificate) |
| 78 | + response.raise_for_status() |
| 79 | + connection_details: ConnectionDetails = ConnectionDetails.parse_raw(response.json()) |
| 80 | + |
| 81 | + self.selected_protocol = connection_details.selectedProtocol |
| 82 | + self.connection_uri = connection_details.connectionUri |
| 83 | + self.challenge = JweCompact(connection_details.challenge).decrypt(self._rsa_key_pair) |
| 84 | + |
| 85 | + self.paired = True |
| 86 | + return self.paired |
0 commit comments