Behaviour for WebSocket authentication pattern implementations.
All WS auth patterns implement callbacks for building auth messages
and handling auth responses. Patterns reuse CCXT.Signing crypto
primitives (hmac_sha256/2, hmac_sha384/2, hmac_sha512/2,
encode_hex/1, encode_base64/1, timestamp_ms/0, etc.).
Auth Flow
- Caller invokes
CCXT.WS.Auth.pre_auth/4(REST round-trip when needed) - Caller invokes
CCXT.WS.Auth.build_auth_message/4 - Caller sends the returned frame via
ZenWebsocket.Client.send_message/2 - Caller feeds the response back through
CCXT.WS.Auth.handle_auth_response/3
Pattern Types
- Direct HMAC — sign payload, send auth message (bybit, okx, bitfinex, …)
- Pre-auth — REST call first, then WS (binance
listen_key, krakenrest_token) - Inline — auth included in each subscribe message (coinbase)
Implementing a Pattern
defmodule CCXT.WS.Auth.DirectHmacExpiry do
@behaviour CCXT.WS.Auth.Behaviour
@impl true
def pre_auth(_credentials, _config, _opts), do: {:ok, %{}}
@impl true
def build_auth_message(credentials, config, opts) do
{:ok, %{"op" => "auth", "args" => [api_key, expires, signature]}}
end
@impl true
def handle_auth_response(response, _state) do
if response["success"], do: :ok, else: {:error, :auth_failed}
end
end
Summary
Callbacks
Builds the WebSocket authentication frame.
Optional: build auth data to include in subscribe frames.
Handles the authentication response frame.
Performs pre-authentication (REST call for token/listen key).
Types
Callbacks
@callback build_auth_message( credentials :: CCXT.Credentials.t(), config :: auth_config(), opts :: opts() ) :: build_result()
Builds the WebSocket authentication frame.
Returns {:ok, message} where message is a JSON-encodable map,
:no_message when the pattern doesn't send a standalone auth frame
(e.g. listen_key, rest_token, inline_subscribe), or {:error, reason}.
@callback build_subscribe_auth( credentials :: CCXT.Credentials.t(), config :: auth_config(), channel :: String.t() | nil, symbols :: [String.t()] | nil ) :: map() | nil
Optional: build auth data to include in subscribe frames.
For inline patterns (coinbase) and token patterns (kraken), auth data is attached to each private subscribe. Default is no inline auth.
@callback handle_auth_response( response :: auth_response(), state :: auth_state() ) :: handle_result()
Handles the authentication response frame.
Returns :ok on success, {:ok, auth_meta} with metadata such as
%{ttl_ms: 900_000} for expiry scheduling (see CCXT.WS.Auth.Expiry),
or {:error, reason} on failure.
@callback pre_auth( credentials :: CCXT.Credentials.t(), config :: auth_config(), opts :: opts() ) :: pre_auth_result()
Performs pre-authentication (REST call for token/listen key).
Returns {:ok, data} with pre-auth data the caller needs to drive the
REST call and embed its result in the WS URL or subscribe frames.
Patterns that don't need pre-auth return {:ok, %{}}.