Tink.WebhookVerifier (Tink v1.0.0)

Copy Markdown View Source

Constant-time HMAC-SHA256 signature verification for incoming Tink webhooks.

Tink signs payloads with the secret configured in your app settings. The signature arrives in the X-Tink-Signature request header as a lowercase hex digest.

Phoenix / Plug usage

defmodule MyAppWeb.TinkWebhookController do
  use MyAppWeb, :controller

  def handle(conn, _params) do
    with {:ok, body, conn} <- Plug.Conn.read_body(conn),
         [sig | _]         <- Plug.Conn.get_req_header(conn, "x-tink-signature"),
         :ok               <- Tink.WebhookVerifier.verify_with_config(body, sig),
         {:ok, event}      <- Jason.decode(body) do
      Tink.WebhookHandler.dispatch(event)
      send_resp(conn, 200, "ok")
    else
      [] -> send_resp(conn, 401, "missing signature")
      {:error, reason} -> send_resp(conn, 401, reason)
    end
  end
end

Summary

Functions

Verify a raw body against a signature using the provided secret.

Verify an incoming Plug.Conn webhook request.

Verify using the webhook secret from application config.

Functions

verify(body, signature, secret)

@spec verify(String.t(), String.t(), String.t()) :: :ok | {:error, String.t()}

Verify a raw body against a signature using the provided secret.

verify_plug(conn, secret \\ nil)

@spec verify_plug(Plug.Conn.t(), String.t() | nil) :: :ok | {:error, String.t()}

Verify an incoming Plug.Conn webhook request.

verify_with_config(body, signature)

@spec verify_with_config(String.t(), String.t()) :: :ok | {:error, String.t()}

Verify using the webhook secret from application config.