This guide takes a Phoenix app from no Countersign to a signed document: the
dependency, the migration, the three behaviours, the config, the workers and
the routes. examples/demo in the repository is the same app, complete, and
its CI job signs a document end to end on every push.
You need PostgreSQL 15 or later (NULLS NOT DISTINCT) and Oban.
1. Add the dependency
def deps do
[
{:countersign, "~> 0.1"}
]
end2. Create the tables
mix countersign.gen.migration
mix ecto.migrate
The migration creates signing_requests and signing_codes through
Countersign.Migrations. Every statement is idempotent, so a migration
retried after a dropped connection succeeds. A later release that changes the
tables adds a version: you generate a migration for that version only.
3. Implement the three behaviours
Countersign stores the workflow and nothing else. Everything about your app comes through three modules you write:
| behaviour | guide | what it answers |
|---|---|---|
Countersign.Host | The Host behaviour | switches, durations, rate limits, audit, notifications, PDF rendering, the clock, the subject lock |
Countersign.Signable | The Signable behaviour | the thing being signed: fetch, content hash, render, record the signature |
Countersign.Delivery | The Delivery behaviour | carry the link and the code to the signer |
A scope is your tenant (an account, an organisation), or nil in a
single-tenant app. Every request carries it as scope_id, and every
callback that can differ per tenant receives it.
4. Build the config
Countersign reads no application env, no OS env and no :persistent_term.
Build a Countersign.Config in one function and pass it to every call:
defmodule MyApp.Signing do
def config do
Countersign.Config.new!(
repo: MyApp.Repo,
host: MyApp.Signing.Host,
signable: MyApp.Signing.Signable,
delivery: MyApp.Signing.Delivery,
base_url: MyAppWeb.Endpoint.url(),
secret_key_base: MyAppWeb.Endpoint.config(:secret_key_base),
disclosure: %{version: 1, text: "You agree to sign electronically. ..."},
certificate_worker: MyApp.Signing.CertificateWorker
)
end
endCountersign.Config.new!/1 raises when a key is missing or a module does not export
every callback of its behaviour, so a wiring mistake fails at boot. The
full list of keys, with their defaults, is in Countersign.Config.
5. Add the workers
defmodule MyApp.Signing.DeliveryWorker do
use Countersign.Workers.Delivery, queue: :notifications, max_attempts: 5
@impl true
def config, do: MyApp.Signing.config()
end
defmodule MyApp.Signing.DailyWorker do
use Countersign.Workers.Daily, delivery: MyApp.Signing.DeliveryWorker
@impl true
def config, do: MyApp.Signing.config()
end
defmodule MyApp.Signing.CertificateWorker do
use Countersign.Workers.Certificate, queue: :default
@impl true
def config, do: MyApp.Signing.config()
endSchedule MyApp.Signing.DailyWorker with Oban's cron plugin. It sends the
one reminder per request and expires links past their date. The
certificate worker renders the signing certificate after each signature;
leave :certificate_worker out and no certificate is made.
6. Mount the signer's pages
import Countersign.Router
pipeline :signing do
plug :accepts, ["html"]
plug :fetch_session
plug :protect_from_forgery
plug :put_secure_browser_headers, %{"referrer-policy" => "no-referrer"}
plug :put_root_layout, html: {MyAppWeb.Layouts, :signing}
end
scope "/" do
pipe_through :signing
countersign_routes "/sign", MyApp.Signing
endThe referrer-policy header matters: the signing link's path holds the
token, and a page that links out must not hand it to the next site. Keep the
/sign paths out of your request log too.
7. Send a document
Countersign.Requests.send(
MyApp.Signing.config(),
%{scope_id: account.id, subject_type: "contract", subject_id: contract.id},
[
%{
role: "client",
signer_name: "Ada Lovelace",
delivery_channel: "email",
to_address: "ada@example.com"
}
],
after_insert: fn requests ->
Enum.each(requests, &Oban.insert!(MyApp.Signing.DeliveryWorker.job(&1)))
end
)The request and its delivery job commit in one transaction. The signer
receives the link, asks for a code, enters it, accepts the disclosure and
signs. Countersign re-checks the content hash under your subject lock and
calls your Countersign.Signable.record_signature/4 in the same
transaction, and the certificate worker then renders the signing
certificate and hands it to Countersign.Signable.store_document/4.
The sender's side is in Countersign.Requests too: resend/3, revoke/3
and the list and count readers.
Next
- Security model: what each secret protects and where it lives.
Countersign.Seal: seal the PDFs you store so they prove by themselves that they were not altered.