-
Notifications
You must be signed in to change notification settings - Fork 6
Ws pairing and authentication #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| [build-system] | ||
| requires = ["setuptools"] | ||
| build-backend = "setuptools.build_meta" | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "s2-python" | ||
| description = "S2 Protocol Python Implementation" | ||
| version = "0.5.0" | ||
|
|
||
| [project.optional-dependencies] | ||
| ws = ["websockets"] | ||
| fastapi = ["fastapi"] | ||
| flask = ["Flask"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any, Dict | ||
|
|
||
|
|
||
| class AbstractConnectionClient(ABC): | ||
| """Abstract class for handling the /requestConnection endpoint.""" | ||
|
|
||
| def request_connection(self) -> Any: | ||
| """Orchestrate the connection request flow: build → execute → handle.""" | ||
| request_data = self.build_connection_request() | ||
| response_data = self.execute_connection_request(request_data) | ||
| return self.handle_connection_response(response_data) | ||
|
|
||
| @abstractmethod | ||
| def build_connection_request(self) -> Dict: | ||
| """ | ||
| Build the payload for the ConnectionRequest schema. | ||
| Returns a dictionary with keys: s2ClientNodeId, supportedProtocols. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def execute_connection_request(self, request_data: Dict) -> Dict: | ||
| """ | ||
| Execute the POST request to /requestConnection. | ||
| Implementations should send the request_data to the endpoint | ||
| and return the JSON response as a dictionary. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def handle_connection_response(self, response_data: Dict) -> Any: | ||
| """ | ||
| Process the ConnectionDetails response (e.g., extract challenge and connection URI). | ||
| The response_data contains keys: selectedProtocol, challenge, connectionUri. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class AbstractPairingClient(ABC): | ||
| """Abstract class for handling the /requestPairing endpoint.""" | ||
|
|
||
| def request_pairing(self) -> Any: | ||
| """Orchestrate the pairing request flow: build → execute → handle.""" | ||
| request_data = self.build_pairing_request() | ||
| response_data = self.execute_pairing_request(request_data) | ||
| return self.handle_pairing_response(response_data) | ||
|
|
||
| @abstractmethod | ||
| def build_pairing_request(self) -> Dict: | ||
| """ | ||
| Build the payload for the PairingRequest schema. | ||
| Returns a dictionary with keys: token, publicKey, s2ClientNodeId, | ||
| s2ClientNodeDescription, supportedProtocols. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def execute_pairing_request(self, request_data: Dict) -> Dict: | ||
| """ | ||
| Execute the POST request to /requestPairing. | ||
| Implementations should send the request_data to the endpoint | ||
| and return the JSON response as a dictionary. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def handle_pairing_response(self, response_data: Dict) -> Any: | ||
| """ | ||
| Process the PairingResponse (e.g., extract server details). | ||
| The response_data contains keys: s2ServerNodeId, serverNodeDescription, requestConnectionUri. | ||
| """ | ||
| pass |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| """ | ||
| Generated classes based on s2-over-ip-pairing.yaml OpenAPI schema. | ||
| This file is auto-generated and should not be modified directly. | ||
| """ | ||
|
|
||
| import uuid | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
| from enum import Enum, auto | ||
| from typing import List, Optional | ||
|
|
||
|
|
||
| class Protocols(str, Enum): | ||
| """Supported protocol types.""" | ||
|
|
||
| WebSocketSecure = "WebSocketSecure" | ||
|
|
||
|
|
||
| class S2Role(str, Enum): | ||
| """Roles in the S2 protocol.""" | ||
|
|
||
| CEM = "CEM" | ||
| RM = "RM" | ||
|
|
||
|
|
||
| class Deployment(str, Enum): | ||
| """Deployment types.""" | ||
|
|
||
| WAN = "WAN" | ||
| LAN = "LAN" | ||
|
|
||
|
|
||
| @dataclass | ||
| class S2NodeDescription: | ||
| """Description of an S2 node.""" | ||
|
|
||
| brand: Optional[str] = None | ||
| logoUri: Optional[str] = None | ||
| type: Optional[str] = None | ||
| modelName: Optional[str] = None | ||
| userDefinedName: Optional[str] = None | ||
| role: Optional[S2Role] = None | ||
| deployment: Optional[Deployment] = None | ||
|
|
||
|
|
||
| class PairingToken(str): | ||
| """A token used for pairing. | ||
|
|
||
| Must match pattern: ^[0-9a-zA-Z]{32}$ | ||
| """ | ||
|
|
||
| def __new__(cls, content: str): | ||
| import re | ||
|
|
||
| if not re.match(r"^[0-9a-zA-Z]{32}$", content): | ||
| raise ValueError("PairingToken must be 32 alphanumeric characters") | ||
| return super().__new__(cls, content) | ||
|
|
||
|
|
||
| @dataclass | ||
| class PairingInfo: | ||
| """Information about a pairing.""" | ||
|
|
||
| pairingUri: Optional[str] = None | ||
| token: Optional[PairingToken] = None | ||
| validUntil: Optional[datetime] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class PairingRequest: | ||
| """Request to initiate pairing.""" | ||
|
|
||
| token: Optional[PairingToken] = None | ||
| publicKey: Optional[bytes] = None | ||
| s2ClientNodeId: Optional[uuid.UUID] = None | ||
| s2ClientNodeDescription: Optional[S2NodeDescription] = None | ||
| supportedProtocols: Optional[List[Protocols]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class PairingResponse: | ||
| """Response to a pairing request.""" | ||
|
|
||
| s2ServerNodeId: Optional[uuid.UUID] = None | ||
| serverNodeDescription: Optional[S2NodeDescription] = None | ||
| requestConnectionUri: Optional[str] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConnectionRequest: | ||
| """Request to establish a connection.""" | ||
|
|
||
| s2ClientNodeId: Optional[uuid.UUID] = None | ||
| supportedProtocols: Optional[List[Protocols]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConnectionDetails: | ||
| """Details for establishing a connection.""" | ||
|
|
||
| selectedProtocol: Optional[Protocols] = None | ||
| challenge: Optional[bytes] = None | ||
| connectionUri: Optional[str] = None | ||
|
|
||
|
|
||
| # Serialization/Deserialization functions | ||
|
|
||
|
|
||
| def _is_dataclass_instance(obj): | ||
| """Check if an object is a dataclass instance.""" | ||
| from dataclasses import is_dataclass | ||
|
|
||
| return is_dataclass(obj) and not isinstance(obj, type) | ||
|
|
||
|
|
||
| def to_dict(obj): | ||
| """Convert a dataclass instance to a dictionary.""" | ||
| if isinstance(obj, datetime): | ||
| return obj.isoformat() | ||
| elif isinstance(obj, uuid.UUID): | ||
| return str(obj) | ||
| elif isinstance(obj, bytes): | ||
| import base64 | ||
|
|
||
| return base64.b64encode(obj).decode("ascii") | ||
| elif isinstance(obj, Enum): | ||
| return obj.value | ||
| elif isinstance(obj, list): | ||
| return [to_dict(item) for item in obj] | ||
| elif _is_dataclass_instance(obj): | ||
| result = {} | ||
| for field in obj.__dataclass_fields__: | ||
| value = getattr(obj, field) | ||
| if value is not None: | ||
| result[field] = to_dict(value) | ||
| return result | ||
| else: | ||
| return obj | ||
|
|
||
|
|
||
| def from_dict(cls, data): | ||
| """Create a dataclass instance from a dictionary.""" | ||
| if data is None: | ||
| return None | ||
|
|
||
| if cls is datetime: | ||
| return datetime.fromisoformat(data) | ||
| elif cls is uuid.UUID: | ||
| return uuid.UUID(data) | ||
| elif cls is bytes: | ||
| import base64 | ||
|
|
||
| return base64.b64decode(data.encode("ascii")) | ||
| elif issubclass(cls, Enum): | ||
| return cls(data) | ||
| elif issubclass(cls, PairingToken): | ||
| return PairingToken(data) | ||
| elif hasattr(cls, "__dataclass_fields__"): | ||
| fieldtypes = cls.__annotations__ | ||
| instance_data = {} | ||
|
|
||
| for field, field_type in fieldtypes.items(): | ||
| if field in data and data[field] is not None: | ||
| # Handle List[Type] annotations | ||
| if hasattr(field_type, "__origin__") and field_type.__origin__ is list: | ||
| item_type = field_type.__args__[0] | ||
| instance_data[field] = [from_dict(item_type, item) for item in data[field]] | ||
| else: | ||
| instance_data[field] = from_dict(field_type, data[field]) | ||
|
|
||
| return cls(**instance_data) | ||
| else: | ||
| return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of this was already defined inyou were correct in thatsetup.cfg. Let's keep it there (see this related PR).pyproject.tomlis the way to go rather thansetup.cfg.