WebsockexNova.Auth.AuthFlow (WebsockexNova v0.1.0)
View SourceManages authentication flows for WebSocket connections.
The AuthFlow
module provides standardized functions for:
- Initial authentication when a connection is established
- Reauthentication when tokens expire or authentication fails
- Processing authentication responses from the server
- Determining when reauthentication is needed
Authentication Flow
- When a connection is established,
authenticate/2
is called - The AuthHandler generates authentication data via
generate_auth_data/1
- Authentication data is sent to the server
- Server responds with success or error
handle_auth_response/2
processes the response and updates state- During connection,
check_reauthentication/2
checks if auth renewal is needed
Example Usage
# During initial connection
{:ok, state} = AuthFlow.authenticate(state, &send_frame_fn/4)
# When a message is received that might be an auth response
{:ok, updated_state} = AuthFlow.handle_auth_response(message, state)
# Periodically check if reauthentication is needed
case AuthFlow.check_reauthentication(state, &send_frame_fn/4) do
{:ok, state} -> state # No reauthentication needed
{:reauthenticated, state} -> state # Reauthentication initiated
end
Summary
Functions
Initiates the authentication process using the configured AuthHandler.
Checks if reauthentication is needed and initiates the process if necessary.
Handles an authentication response from the server.
Functions
Initiates the authentication process using the configured AuthHandler.
This function:
- Retrieves the AuthHandler from the state
- Calls generate_auth_data/1 to get authentication data
- Sends the authentication data to the server
- Returns the updated state
The actual authentication response will be processed later via handle_auth_response/2.
Parameters
state
: The current state containing authentication handler configurationsend_frame_fn
: Function to send frames to the WebSocket server, with signature(conn, frame_type, frame_data, stream_ref) -> :ok | {:error, reason}
Returns
{:ok, updated_state}
: Authentication data sent successfully{:error, reason, updated_state}
: Error generating authentication data
@spec check_reauthentication(map(), function()) :: {:ok, map()} | {:reauthenticated, map()} | {:error, atom() | String.t(), map()}
Checks if reauthentication is needed and initiates the process if necessary.
This function:
- Retrieves the AuthHandler from the state
- Calls needs_reauthentication?/1 to check if reauthentication is needed
- If needed, initiates the authentication process
Parameters
state
: The current state containing authentication handler configurationsend_frame_fn
: Function to send frames to the WebSocket server, with signature(conn, frame_type, frame_data, stream_ref) -> :ok | {:error, reason}
Returns
{:ok, state}
: No reauthentication needed{:reauthenticated, state}
: Reauthentication initiated{:error, reason, state}
: Error during reauthentication
Handles an authentication response from the server.
This function:
- Retrieves the AuthHandler from the state
- Calls handle_auth_response/2 to process the authentication response
- Updates the state with the result
Parameters
response
: The response message received from the serverstate
: The current state containing authentication handler configuration
Returns
{:ok, updated_state}
: Authentication successful or message processed{:error, reason, updated_state}
: Authentication failed