Countersign.Signable behaviour (Countersign v0.1.0)

Copy Markdown View Source

The behaviour for the thing being signed: a contract, a consent form, a plan of care. Countersign never stores the subject; it stores subject_type and subject_id and asks this module for everything else.

Summary

Types

What the signer entered on the signing page: at least :signer_name and :method (:drawn or :typed), and :image (PNG bytes) for a drawn signature.

Why a PDF is rendered: for the signer to read before signing, or as their signed copy.

Facts about the signer's HTTP request: :ip_address and :user_agent.

Whatever the host returns from fetch/3; Countersign passes it back untouched.

Callbacks

The subject's content hash: 64 lowercase hex characters. A request is sent against this hash, and a signature is accepted only while it is unchanged.

Loads the subject, scoped to scope_id. Answers {:error, :not_found} for anything outside the scope.

Writes the signature into the host's own records, inside the sign transaction.

Renders the subject as a PDF for purpose. Answers {:error, :busy} when the renderer is saturated, which the signing page reports as "try again".

The human label of role on this subject, as the signing page shows it.

Whether the subject still takes signatures (for example, not voided).

Stores a generated document (the signing certificate) against the subject. Countersign.Workers.Certificate calls it with the request row locked and pins the answer as certificate_ref. {:error, :busy} or {:error, :timeout} snoozes the job, {:cancel, reason} ends it for good (the owner is gone), and any other error retries it.

The human name of the subject's kind ("Plan of Care"), as the signing certificate shows it.

Types

input()

@type input() :: %{
  :signer_name => String.t(),
  :method => :drawn | :typed,
  optional(:image) => binary(),
  optional(atom()) => term()
}

What the signer entered on the signing page: at least :signer_name and :method (:drawn or :typed), and :image (PNG bytes) for a drawn signature.

purpose()

@type purpose() :: :review | :copy

Why a PDF is rendered: for the signer to read before signing, or as their signed copy.

request_meta()

@type request_meta() :: %{
  optional(:ip_address) => String.t(),
  optional(:user_agent) => String.t()
}

Facts about the signer's HTTP request: :ip_address and :user_agent.

subject()

@type subject() :: term()

Whatever the host returns from fetch/3; Countersign passes it back untouched.

Callbacks

content_hash(subject)

@callback content_hash(subject()) :: String.t()

The subject's content hash: 64 lowercase hex characters. A request is sent against this hash, and a signature is accepted only while it is unchanged.

fetch(scope_id, subject_type, subject_id)

@callback fetch(
  Countersign.Host.scope_id(),
  subject_type :: String.t(),
  subject_id :: String.t()
) ::
  {:ok, subject()} | {:error, :not_found}

Loads the subject, scoped to scope_id. Answers {:error, :not_found} for anything outside the scope.

record_signature(subject, t, input, request_meta)

@callback record_signature(subject(), Countersign.Request.t(), input(), request_meta()) ::
  {:ok, signature_ref :: String.t()}
  | {:error, :changed}
  | {:error, :already_signed}
  | {:error, term()}

Writes the signature into the host's own records, inside the sign transaction.

Answers {:ok, signature_ref} (stored as the request's signature_ref), {:error, :changed} when the content moved since the send, {:error, :already_signed} when the role's line is already signed, or {:error, term} for anything else, which rolls the transaction back.

render_pdf(subject, purpose)

@callback render_pdf(subject(), purpose()) :: {:ok, binary()} | {:error, :busy | term()}

Renders the subject as a PDF for purpose. Answers {:error, :busy} when the renderer is saturated, which the signing page reports as "try again".

role_label(subject, role)

@callback role_label(subject(), role :: String.t()) :: String.t()

The human label of role on this subject, as the signing page shows it.

signable?(subject)

@callback signable?(subject()) :: boolean()

Whether the subject still takes signatures (for example, not voided).

store_document(subject, atom, pdf, t)

@callback store_document(
  subject(),
  :certificate,
  pdf :: binary(),
  Countersign.Request.t()
) ::
  {:ok, document_ref :: String.t()} | {:cancel, term()} | {:error, term()}

Stores a generated document (the signing certificate) against the subject. Countersign.Workers.Certificate calls it with the request row locked and pins the answer as certificate_ref. {:error, :busy} or {:error, :timeout} snoozes the job, {:cancel, reason} ends it for good (the owner is gone), and any other error retries it.

type_label(subject)

@callback type_label(subject()) :: String.t()

The human name of the subject's kind ("Plan of Care"), as the signing certificate shows it.