Countersign.Host is everything about your application that is not about
signing. You implement it once. Countersign.Requests and the workers call
it; Countersign reads no configuration of its own, so every switch and every
duration comes from here.
Switches and durations
| callback | answers | notes |
|---|---|---|
Countersign.Host.enabled?/1 | whether signing is on for the scope | A send for a disabled scope is refused with {:error, :disabled}. While it is off, a delivery snoozes, no reminder goes out, and no one can sign; links still expire on their date. |
Countersign.Host.link_days/1 | how long a link lives after a send | |
Countersign.Host.reminder_days/1 | days after the last send before the one reminder | 0 sends none. |
Countersign.Host.codes_per_day/1 | the scope's daily ceiling on signing codes | Counted across all its requests, on top of Countersign's own per-request limits. |
Countersign.Host.display_name/1 | the sender's name on messages and pages |
Each receives the request's scope_id, so a multi-tenant app can answer
from a per-tenant setting.
Rate limiting
Countersign.Host.check_rate/3 counts one hit against a key in a window
and answers :ok or {:error, :rate_limited}. Countersign builds the keys
(per request, per scope); you supply the counter. In a cluster it must be
shared between nodes: a per-node ETS counter lets each node grant the limit
again. A Postgres table, Redis or Hammer with a shared backend all work.
Audit and notifications
Countersign.Host.audit/3 receives every event of
Countersign.Host.event/0 with the request and a meta map. Where a
signer's HTTP request caused the event, meta carries :ip_address and
:user_agent; it never carries the delivery address. Write it to your audit
trail: together with the request row, it is the evidence of who signed what,
when, from where.
Countersign.Host.notify/2 tells your users that a request changed
(signed, declined, expired, failed). Send an in-app notification or an email
to the sender; answer :ok.
Rendering and time
Countersign.Host.html_to_pdf/1 turns the signing certificate's HTML into
a PDF. Use the renderer your app already has (ChromicPDF, a headless Chrome
service, wkhtmltopdf). Answer {:error, :busy} or {:error, :timeout} when
it is saturated: the certificate job snoozes and tries again.
Countersign.Host.now/0 is the only clock Countersign reads. Every
timestamp it writes, and every expiry it checks, comes from here, so a test
can move time by answering a fixed value.
The subject lock
Countersign.Host.lock_subject/4 is the first statement of every send,
resend and sign transaction. Your app may have its own writers of the same
subject (an editor that changes the contract, a void, a paper signature);
taking your lock here serialises them with Countersign, and always in the
same order: your lock first, then the request row. Two transactions can
therefore never wait on each other in opposite orders.
With no writers of your own to serialise, take a transaction-scoped
advisory lock on the subject, or answer :ok:
@impl true
def lock_subject(repo, _scope_id, type, id) do
repo.query!("SELECT pg_advisory_xact_lock(hashtext($1))", ["#{type}:#{id}"])
:ok
endWith writers of your own, lock the subject's row the way they do
(SELECT ... FOR UPDATE), so the editor and the signature cannot both
believe they saw the final content.
Testing a host
Answer the clock and the switches from the test, not from global state. The
package's own tests run against the host in test/support/test_host.ex,
whose state lives in an Agent started per test under that test's own scope
id, so tests stay async: true.