# `Countersign.Host`
[🔗](https://github.com/mkreyman/countersign/blob/v0.1.0/lib/countersign/host.ex#L1)

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`.

# `event`

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

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

# `scope_id`

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

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

# `audit`

```elixir
@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`

```elixir
@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`

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

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

# `display_name`

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

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

# `enabled?`

```elixir
@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`

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

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

# `link_days`

```elixir
@callback link_days(scope_id()) :: pos_integer()
```

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

# `lock_subject`

```elixir
@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`

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

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

# `now`

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

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

# `reminder_days`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
