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