Countersign.Delivery carries a signing link or a signing code to the
signer by the request's delivery_channel: email or sms. What each
channel means is yours: Swoosh, Postmark, Twilio, a queue in front of them.
Before it calls any callback, Countersign decrypts the stored address into
the request's virtual address field. That field is redacted in
inspect/1; do not log it yourself either.
The three callbacks
| callback | when |
|---|---|
Countersign.Delivery.deliver_link/3 | the first send, every resend (:initial) and the one reminder (:reminder) |
Countersign.Delivery.deliver_code/2 | the signer asks for a code on the landing page |
Countersign.Delivery.reachable/1 | before a code is issued: can the address still be reached (an SMS number that has not opted out); an error refuses the code with {:error, {:unreachable, reason}} |
The answers, and what each one does
Each delivery answers one of Countersign.Delivery.result/0:
:ok: handed to your provider.{:error, :retry}: a transient failure (a timeout, a 5xx). The delivery worker retries with Oban's backoff; after its last attempt the request is markedfailed.{:error, {:permanent, reason}}: the address cannot take this message (a hard bounce, an invalid number, an unsupported channel). The pending request is markedfailedwithfailed_reason, and:failis audited and notified, so the sender sees it at once and can correct the address.
Tell the two apart honestly: a permanent failure reported as :retry keeps
the sender waiting on a link that will never arrive, and a transient one
reported as permanent fails a request a minute's wait would have saved.
What the messages say
Keep them short and plain, and name the sender (Countersign.Host.display_name/1)
so the signer recognises who sent it. A link message should say that a code
will be sent to the same address, and that the signer may ignore a message
they did not expect. A code message should say how long the code lives (10
minutes).
Never put the document itself in the message: the signer reads it on the signing page, after proving control of the address.
Late failures
A provider that reports a bounce after accepting the message (a webhook)
can fail the request then with Countersign.Requests.fail/3: look the
request up by your own message id and pass the bounce reason, so the sender
sees the failure instead of a link waiting forever. A request that is no
longer pending is left as it is.
Example: email through Swoosh
defmodule MyApp.Signing.Delivery do
@behaviour Countersign.Delivery
import Swoosh.Email
@impl true
def deliver_link(%Countersign.Request{delivery_channel: :email} = request, url, kind) do
prefix = if kind == :reminder, do: "Reminder: ", else: ""
new()
|> to({request.signer_name, request.address})
|> from({"My App", "noreply@myapp.example"})
|> subject(prefix <> "My App sent you a document to sign")
|> text_body("""
A document is waiting for your signature:
#{url}
You'll be asked for a code we send to this address.
If you were not expecting this, you can ignore it.
""")
|> send_email()
end
def deliver_link(_request, _url, _kind), do: {:error, {:permanent, :channel_not_supported}}
@impl true
def deliver_code(%Countersign.Request{delivery_channel: :email} = request, code) do
new()
|> to({request.signer_name, request.address})
|> from({"My App", "noreply@myapp.example"})
|> subject("Your signing code: #{code}")
|> text_body("Your signing code is #{code}. It expires in 10 minutes.\n")
|> send_email()
end
def deliver_code(_request, _code), do: {:error, {:permanent, :channel_not_supported}}
@impl true
def reachable(%Countersign.Request{delivery_channel: :email}), do: :ok
def reachable(_request), do: {:error, :channel_not_supported}
defp send_email(email) do
case MyApp.Mailer.deliver(email) do
{:ok, _} -> :ok
{:error, _} -> {:error, :retry}
end
end
end