AshHooks.Provider.Signature (AshHooks v3.0.0)

Copy Markdown View Source

Stateless webhook signature verification with caller-pinned keys and algorithms.

HMAC, RSA, ECDSA and Ed25519 functions return :ok, {:error, :invalid_signature} or {:error, :no_webhook_secret}; jws/3 and check_timestamp/3 can also return {:error, :stale_timestamp}. jws/3 returns the authenticated payload bytes. Malformed input never raises.

encoding: describes the received signature: :hex is lowercase only, :hex_upper uppercase only, :hex_any accepts either case, :base64 and :base64url accept padded or unpadded strings, and :raw accepts bytes. Keys may be a single key or a rotation list; empty keys cannot verify. HMAC evaluates all keys and compares bytes in constant time behind a length guard.

Asymmetric keys may be decoded :public_key terms or a PEM containing exactly one PUBLIC KEY, RSA PUBLIC KEY, or CERTIFICATE. A certificate is only a public key container pinned by the host: chain, expiry, and revocation are not checked. Ed25519 also accepts a raw 32-byte public key. Key types must match the algorithm. Keys are never fetched from request headers, tokens, or URLs.

Summary

Functions

Checks nonnegative Unix seconds (integer or ASCII digit string) within a symmetric positive tolerance.

Compares binary values in constant time after checking equal lengths.

Verifies ECDSA on P-256 or P-384; options: digest: (:sha256 default, :sha384, :sha512), format: (:der default or fixed-width :raw r || s), and encoding: (:base64 default). Other curves and incorrect widths fail.

Verifies Ed25519 with encoding: (default :base64).

Looks up a request header case-insensitively in ctx.headers, a map or an ordered {name, value} list such as conn.req_headers. Repeated lines in a list are joined with ", ". Returns nil when the header is absent.

Verifies HMAC; options: digest: (default :sha256, also :sha1, :sha384, :sha512) and encoding: (default :hex).

Verifies compact JWS and returns {:ok, payload_bytes}.

Verifies RSA; options: digest: (:sha256 default, :sha384, :sha512), padding: (:pkcs1_v1_5 default or :pss) and encoding: (:base64 default). PSS pins MGF1 to the digest and salt length to its byte length (RFC 7518).

Types

error()

@type error() :: {:error, :invalid_signature | :no_webhook_secret | :stale_timestamp}

Functions

check_timestamp(timestamp, ctx, tolerance_seconds)

Checks nonnegative Unix seconds (integer or ASCII digit string) within a symmetric positive tolerance.

compare(expected, actual)

Compares binary values in constant time after checking equal lengths.

ecdsa(input, signature, keys, opts \\ [])

@spec ecdsa(binary(), binary(), term(), keyword()) :: :ok | error()

Verifies ECDSA on P-256 or P-384; options: digest: (:sha256 default, :sha384, :sha512), format: (:der default or fixed-width :raw r || s), and encoding: (:base64 default). Other curves and incorrect widths fail.

ed25519(input, signature, keys, opts \\ [])

@spec ed25519(binary(), binary(), term(), keyword()) :: :ok | error()

Verifies Ed25519 with encoding: (default :base64).

header(arg1, name)

Looks up a request header case-insensitively in ctx.headers, a map or an ordered {name, value} list such as conn.req_headers. Repeated lines in a list are joined with ", ". Returns nil when the header is absent.

hmac(input, signature, keys, opts \\ [])

@spec hmac(binary(), binary(), term(), keyword()) :: :ok | error()

Verifies HMAC; options: digest: (default :sha256, also :sha1, :sha384, :sha512) and encoding: (default :hex).

jws(token, keys, opts)

@spec jws(binary(), term(), keyword()) :: {:ok, binary()} | error()

Verifies compact JWS and returns {:ok, payload_bytes}.

alg: is required and must exactly match the protected header: HS256/384/512, RS256/384/512, PS256/384/512, ES256, ES384, or EdDSA. ES algorithms pin the curve and raw signature format. none, nonempty crit, and b64: false fail, as do padded segments (RFC 7515 encodes them without =). Key-location headers are ignored. JSON object exp and nbf claims must be numeric and are checked only after verification, against now_ms: or the clock.

rsa(input, signature, keys, opts \\ [])

@spec rsa(binary(), binary(), term(), keyword()) :: :ok | error()

Verifies RSA; options: digest: (:sha256 default, :sha384, :sha512), padding: (:pkcs1_v1_5 default or :pss) and encoding: (:base64 default). PSS pins MGF1 to the digest and salt length to its byte length (RFC 7518).