Wiztchta (wiztchta v0.1.0)

Copy Markdown View Source

Drop-in, pluggable CAPTCHA for Phoenix forms and LiveViews.

Render a challenge inside any form with Wiztchta.Components.wiztchta/1 and check the submission with verify/2 (or Wiztchta.Plug in controllers).

Wiztchta is stateless: each challenge travels to the browser as an encrypted, expiring token that only your server can read. A pluggable Wiztchta.Store rejects tokens that are submitted more than once.

Configuration

config :wiztchta,
  # Required: either a secret or an endpoint whose secret_key_base is used.
  endpoint: MyAppWeb.Endpoint,
  # secret_key_base: "...",

  # Optional (defaults shown):
  default_challenge: Wiztchta.Challenges.ProofOfWork,
  max_age: 300,
  store: Wiztchta.Store.ETS,
  param: "wiztchta"

Every option can also be passed per call to new/1 and verify/2.

Summary

Functions

A human-readable message for a verify/2 error.

Generates a new challenge.

Returns true if verify/2 succeeds.

Verifies a submitted challenge.

Types

error()

@type error() :: :missing | :invalid_token | :expired | :already_used | :wrong_answer

Functions

error_message(arg1)

@spec error_message(error()) :: String.t()

A human-readable message for a verify/2 error.

new(opts \\ [])

@spec new(keyword()) :: Wiztchta.Challenge.t()

Generates a new challenge.

Options

valid?(params, opts \\ [])

@spec valid?(map(), keyword()) :: boolean()

Returns true if verify/2 succeeds.

verify(params, opts \\ [])

@spec verify(map(), keyword()) :: :ok | {:error, error()}

Verifies a submitted challenge.

Accepts the full form params (looking under the :param key, "wiztchta" by default) or the inner %{"token" => ..., "response" => ...} map.

A token is consumed on its first verification attempt, whether the answer is right or wrong, so render a fresh challenge after every submit.

case Wiztchta.verify(params) do
  :ok -> ...
  {:error, reason} -> ...
end