Server Hooks¶
Module: s2auth.server.hooks
Server hooks for customizing S2 authentication behavior.
This module provides hooks that can be overridden to customize the behavior of the S2 authentication server. Each hook is registered in a HookRegistry and can be replaced by client code.
See docs/server/hooks.md for detailed documentation on each hook and how to override them.
HookFunction = Callable[..., Awaitable[Any]]
module-attribute
¶
NodeId
¶
Role
¶
EndpointDescription
¶
NodeDescription
¶
ReadOnlyAuthenticationContext
¶
Bases: AuthenticationContext
Read-only view of AuthenticationContext for passing to hooks.
This class prevents accidental modification of context state in hooks. Any attempt to modify attributes will raise a ValidationError.
Source code in src/s2auth/server/context.py
ReadOnlyPairingAttemptContext
¶
Bases: PairingAttemptContext
Read-only view of PairingAttemptContext for passing to hooks.
This class prevents accidental modification of context state in hooks. Any attempt to modify attributes will raise a ValidationError.
Source code in src/s2auth/server/context.py
Settings
¶
Bases: BaseSettings
Source code in src/s2auth/server/settings.py
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()
¶
get_server_connection_initiation_endpoint(authentication_context, server_settings=Depends[settings])
async
¶
Default hook implementation to get the server connection initiation endpoint.
This hook is called during the pairing phase to retrieve the server's endpoint so the S2 Client Node can connect to the server side to establish an S2 connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
authentication_context
|
ReadOnlyAuthenticationContext
|
Read-only view of the authentication context (contains client_node_id, state, etc.) |
required |
server_settings
|
Settings
|
Server configuration settings |
Depends[settings]
|
Source code in src/s2auth/server/hooks.py
pairing_attempt_request(authentication_context, pairing_context, server_settings=Depends[settings])
async
¶
Default hok implementation during pairing attempt requests.
This hook is called during pairing attempt requests to allow custom code to accept/refuse the pairing attempt. If it returns True, the pairing attempt request is allowed. On errors or False it is refused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
authentication_context
|
ReadOnlyAuthenticationContext
|
Read-only view of the authentication context (contains client_node_id, state, etc.) |
required |
pairing_context
|
ReadOnlyPairingAttemptContext
|
Read-only view of the pairing attempt context (contains pairing_attempt_id, pairing_token, etc.) |
required |
server_settings
|
Settings
|
Server configuration settings |
Depends[settings]
|
Returns:
| Type | Description |
|---|---|
bool
|
Boolean whether the pairing is allowed |
Raises:
| Type | Description |
|---|---|
S2ConnectError
|
To refuse the pairing attempt with a specific error message |
Source code in src/s2auth/server/hooks.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()
¶
register_hook(original_hook, hook_registry=Depends[hook_registry])
¶
Decorator to register a custom hook implementation.
This decorator allows client code to override default hook implementations. Each hook can only be overridden once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_hook
|
HookFunction
|
The original hook function to override |
required |
Returns:
| Type | Description |
|---|---|
Callable[[HookFunction], HookFunction]
|
Decorator function |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the hook is already registered by custom code |
KeyError
|
If the hook is not recognized |
Example
from s2auth.server import hooks
from wepositive_di import Depends, inject
@register_hook(hooks.pairing_attempt_request)
@inject
async def my_pairing_hook(
authentication_context: AuthenticationContext,
pairing_context: PairingAttemptContext,
server_settings: Settings = Depends[settings],
) -> tuple[EndpointDescription, NodeDescription]:
# Custom implementation
...