Countersign.Host behaviour (Countersign v0.1.0)

Copy Markdown View Source

The behaviour an application implements once to run Countersign.

The host owns everything that is about the application rather than about signing: whether signing is switched on for a scope (a tenant, an account, or nil in a single-tenant app), how long links live, the name messages show, rate limiting, auditing, notifications, HTML-to-PDF rendering, the clock, and the per-subject lock that serialises a send or a signature with the host's own writers.

Countersign.Requests calls these callbacks; it never reads application config, so everything it needs comes from here or from Countersign.Config.

Summary

Types

An event Countersign.Requests reports to audit/3 and notify/2.

The host's tenant identifier, or nil in a single-tenant host.

Callbacks

Records an audit event for request. meta carries :ip_address and :user_agent where a signer's request caused the event, and never the delivery address.

Counts one hit against key in a window of scale_ms milliseconds.

How many signing codes the scope may send per day, across all its requests.

The name messages and the signing page show for the scope (the sender's organisation).

Whether remote signing is switched on for scope_id. A send for a disabled scope is refused.

Renders HTML (the signing certificate) to a PDF binary.

How many days a signing link stays valid after it is sent.

Takes the host's per-subject lock inside the caller's transaction and answers :ok.

Tells the host's users that request changed (signed, declined, expired, failed).

The current time. Every timestamp Countersign writes comes from here.

How many days after the last send a pending request gets one reminder; 0 sends none.

Types

event()

@type event() ::
  :send
  | :resend
  | :view
  | :consent
  | :sign
  | :decline
  | :revoke
  | :supersede
  | :expire
  | :fail

An event Countersign.Requests reports to audit/3 and notify/2.

scope_id()

@type scope_id() :: Ecto.UUID.t() | nil

The host's tenant identifier, or nil in a single-tenant host.

Callbacks

audit(event, t, meta)

@callback audit(event(), Countersign.Request.t(), meta :: map()) :: :ok

Records an audit event for request. meta carries :ip_address and :user_agent where a signer's request caused the event, and never the delivery address.

check_rate(key, scale_ms, limit)

@callback check_rate(key :: String.t(), scale_ms :: pos_integer(), limit :: pos_integer()) ::
  :ok | {:error, :rate_limited}

Counts one hit against key in a window of scale_ms milliseconds.

Answers :ok while the count is at most limit, and {:error, :rate_limited} once it is over.

codes_per_day(scope_id)

@callback codes_per_day(scope_id()) :: pos_integer()

How many signing codes the scope may send per day, across all its requests.

display_name(scope_id)

@callback display_name(scope_id()) :: String.t()

The name messages and the signing page show for the scope (the sender's organisation).

enabled?(scope_id)

@callback enabled?(scope_id()) :: boolean()

Whether remote signing is switched on for scope_id. A send for a disabled scope is refused.

html_to_pdf(html)

@callback html_to_pdf(html :: iodata()) :: {:ok, binary()} | {:error, term()}

Renders HTML (the signing certificate) to a PDF binary.

lock_subject(repo, scope_id, subject_type, subject_id)

@callback lock_subject(
  repo :: module(),
  scope_id(),
  subject_type :: String.t(),
  subject_id :: String.t()
) ::
  :ok

Takes the host's per-subject lock inside the caller's transaction and answers :ok.

Countersign.Requests calls it as the first statement of every send, resend and sign transaction, so the request-row lock always comes after the host's lock and the two can never be taken in opposite orders. A host with no writers of its own to serialise may take a transaction-scoped advisory lock or simply answer :ok.

notify(event, t)

@callback notify(event(), Countersign.Request.t()) :: :ok

Tells the host's users that request changed (signed, declined, expired, failed).

now()

@callback now() :: DateTime.t()

The current time. Every timestamp Countersign writes comes from here.

reminder_days(scope_id)

@callback reminder_days(scope_id()) :: non_neg_integer()

How many days after the last send a pending request gets one reminder; 0 sends none.