Server Connection Initiation¶
Module: s2auth.server.connection_initiation
Server-side helpers for the S2 Connect connection initiation flow.
The communication client starts a new S2 session by calling
/initiateSession with its current access token. The communication server
validates the existing pairing, negotiates the communication protocol and S2
message version, and returns a newly generated pending access token. The client
must persist that pending token and then confirm it with /confirmAccessToken
before the server promotes it to the active access token.
After confirmation, the previous active access token is only retained as a one-time connection token for authenticating the actual S2 communication channel. Each new S2 session repeats this renewal process.
InvalidAccessTokenError
¶
Bases: AccessError
Invalid AccessToken when authenticating a new s2 connection.
Source code in src/s2auth/common/exceptions.py
NoCompatibleCommunitcationProtocol
¶
Bases: S2ConnectErrorWithDetails
No compatible communication protocols are available between the client and server.
Source code in src/s2auth/common/exceptions.py
NoCompatibleS2ConnectVersionError
¶
Bases: S2ConnectErrorWithDetails
No compatible S2Connect versions are available between the client and server.
Source code in src/s2auth/common/exceptions.py
NoCompatibleS2VersionError
¶
Bases: S2ConnectErrorWithDetails
No compatible S2 versions are available between the client and server.
Source code in src/s2auth/common/exceptions.py
PairingNotCompleteError
¶
Bases: S2ConnectError
Pairing was not completed successfully before initiating a connection.
Source code in src/s2auth/common/exceptions.py
InvalidServerError
¶
Bases: AccessError
Unknown serverNodeId was specified for this server.
Source code in src/s2auth/common/exceptions.py
AccessToken
¶
Bases: RootModel[Base64Bytes]
Source code in src/s2auth/common/model/s2_connect_common.py
CommunicationProtocol
¶
NodeId
¶
InitiateSessionPostResponse
¶
Bases: BaseModel
Source code in src/s2auth/common/model/s2_connect_session_init.py
AuthenticationContext
¶
Bases: BaseModel
Authentication context data for a client connection.
Note: Modifications to context instances should be done carefully in multi-threaded/async environments. Consider using the storage's locking mechanisms if implementing complex state updates.
Source code in src/s2auth/server/context.py
ClientState
¶
HookRegistry
¶
Registry for server hooks that can be overridden by client code.
Source code in src/s2auth/server/hooks.py
register(original_hook, custom_hook)
¶
Register a custom hook implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_hook
|
HookFunction
|
The original hook function to override |
required |
custom_hook
|
HookFunction
|
The custom hook implementation function |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a custom implementation is already registered for this hook |
KeyError
|
If the original hook is not recognized |
Source code in src/s2auth/server/hooks.py
get(hook)
¶
Get a hook implementation (default or custom).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
HookFunction
|
The hook function reference |
required |
Returns:
| Type | Description |
|---|---|
HookFunction
|
The hook implementation function (either default or custom) |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the hook is not registered |
Source code in src/s2auth/server/hooks.py
Settings
¶
Bases: BaseSettings
Source code in src/s2auth/server/settings.py
generate_access_token()
¶
authentication_context(client_node_id=Depends[client_node_id], storage=Depends[context_storage_singleton])
async
¶
Retrieves the context for the specified client_node_id.
This is an async generator provider that yields the context while holding its per-ID lock. The lock is held for the entire duration that dependent functions use the context, ensuring thread-safe and async-safe modifications.
Works in both async and threaded environments through wepositive-di storage.
Source code in src/s2auth/server/context.py
get_server_endpoint_description(client_node_id, server_settings=Depends[settings])
async
¶
Default hook implementation for getting the server endpoint description.
This hook is called during the pairing request phase and connection initialization phase to generate the server's S2 endpoint descriptions. Override this hook to customize the server's identity or to refuse pairing by raising an S2ConnectError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_node_id
|
NodeId
|
NodeId of the client. |
required |
server_settings
|
Settings
|
Server configuration settings |
Depends[settings]
|
Returns:
| Type | Description |
|---|---|
EndpointDescription
|
EndpointDescription for the server |
Source code in src/s2auth/server/hooks.py
get_server_node_description(client_node_id, server_settings=Depends[settings])
async
¶
Default hook implementation for pairing request.
This hook is called to get the server node description the pairing and connection initiation phase to generate the server's S2 endpoint and node descriptions. Override this hook to customize the server's identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_node_id
|
NodeId
|
NodeId of the client. |
required |
server_settings
|
Settings
|
Server configuration settings |
Depends[settings]
|
Returns:
| Type | Description |
|---|---|
NodeDescription
|
NodeDescription for the server |
Source code in src/s2auth/server/hooks.py
hook_registry()
¶
settings()
¶
select_protocol(remote_protocols, local_protocols)
¶
select_version(remote_versions, local_versions)
¶
initiateConnection(server_node_id, access_token, supported_communication_protocols, supported_s2_versions, selected_s2_connect_version, server_settings=Depends[settings], authentication_ctx=Depends[authentication_context], new_access_token=Depends[generate_access_token], hooks=Depends[hook_registry])
async
¶
Handle POST /initiateSession for a paired communication client.
This implements the server side of the specification's connection initiation steps 4 through 6:
- verify that the selected S2 Connect API version is supported;
- verify that the client is paired with this server node and presented the currently active access token;
- negotiate one common S2 message version and communication protocol;
- generate and store a new pending access token; and
- return the negotiated values, pending access token, and current server endpoint/node descriptions.
The returned access token is not active yet. The client must persist it and
confirm it with confirmAccessToken before the old token is invalidated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_node_id
|
NodeId
|
Node ID of the communication server the client wants to connect to. |
required |
access_token
|
AccessToken
|
Currently active access token supplied by the communication client. |
required |
supported_communication_protocols
|
list[CommunicationProtocol]
|
Communication protocols supported by the client. |
required |
supported_s2_versions
|
list[str]
|
S2 message versions supported by the client. |
required |
selected_s2_connect_version
|
str
|
S2 Connect API version selected by the client. |
required |
server_settings
|
Settings
|
Server configuration injected by the DI container. |
Depends[settings]
|
authentication_ctx
|
AuthenticationContext
|
Authentication state for the paired client. |
Depends[authentication_context]
|
new_access_token
|
AccessToken
|
Pending access token generated by the DI provider. |
Depends[generate_access_token]
|
hooks
|
HookRegistry
|
Hook registry used to retrieve server description hooks. |
Depends[hook_registry]
|
Returns:
| Type | Description |
|---|---|
InitiateSessionPostResponse
|
Response containing the negotiated communication protocol, negotiated S2 |
InitiateSessionPostResponse
|
message version, pending access token, and server descriptions. |
Raises:
| Type | Description |
|---|---|
NoCompatibleS2ConnectVersionError
|
If the selected S2 Connect API version is not supported by this server. |
PairingNotCompleteError
|
If the client is not paired or has no active access token. |
InvalidAccessTokenError
|
If the supplied access token is not the active token for the pairing. |
InvalidServerError
|
If the request targets a different server node. |
NoCompatibleS2VersionError
|
If there is no overlap between client and server S2 message versions. |
NoCompatibleCommunitcationProtocol
|
If there is no overlap between client and server communication protocols. |
Source code in src/s2auth/server/connection_initiation.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
validate_access_token(next_access_token, authentication_ctx=Depends[authentication_context])
async
¶
Handle POST /confirmAccessToken for a pending access token.
The client calls this after successfully persisting the pending access token
returned by initiateSession. When the token matches the pending token
for the pairing, it becomes the new active access token. The previous active
access token is moved to current_connection_token so it can be used once
to authenticate the S2 communication channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
next_access_token
|
AccessToken
|
Pending access token supplied by the client in the confirmation request. |
required |
authentication_ctx
|
AuthenticationContext
|
Authentication state for the paired client. |
Depends[authentication_context]
|
Raises:
| Type | Description |
|---|---|
InvalidAccessTokenError
|
If the supplied token is not the pending access token for the pairing. |
Source code in src/s2auth/server/connection_initiation.py
validate_s2_connection_token(connection_token, authentication_ctx=Depends[authentication_context])
async
¶
Validate the one-time token used to open an S2 communication channel.
After confirmAccessToken activates a newly persisted access token, the
previous active token is retained as a one-time connection token. A
WebSocket or other selected communication protocol can use this token for
bearer-token authentication. Once accepted, the token is cleared so it
cannot be reused for another S2 session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_token
|
AccessToken
|
One-time token supplied by the communication client when opening the S2 communication channel. |
required |
authentication_ctx
|
AuthenticationContext
|
Authentication state for the paired client. |
Depends[authentication_context]
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidAccessTokenError
|
If the supplied token is not the current one-time connection token. |