Wraith.Tokens (Wraith v0.1.0)

View Source

Signed, expiring tokens for links we put in mail: magic sign-in, unsubscribe, password reset. Plug.Crypto.sign/4 under the endpoint's secret with a purpose salt, so a token for one purpose is useless for another.

Two kinds:

  • verify/2 — stateless, repeatable within the TTL. Right for unsubscribe and comment reply links, which must keep working.
  • consume/2 — single use. The token's hash is written to the store with a conditional put (and a TTL) the first time it is presented; a second presentation finds it there and is refused. Right for sign-in, confirmation and password-reset links, which a mail scanner or a forwarded message must not be able to replay.

Reset tokens are also bound to the password hash they were minted against (sign(:password_reset, {id, binding})), so a reset link dies the moment the password changes by any route.

Summary

Functions

Verify and burn: a valid token is accepted exactly once. The used-token record lives as long as the token could have (its TTL), then expires.

Verify only (repeatable within the TTL).

Functions

consume(purpose, token)

@spec consume(atom(), String.t()) ::
  {:ok, term()} | {:error, :invalid | :expired | :used}

Verify and burn: a valid token is accepted exactly once. The used-token record lives as long as the token could have (its TTL), then expires.

sign(purpose, data)

@spec sign(atom(), term()) :: String.t()

verify(purpose, token)

@spec verify(atom(), String.t()) :: {:ok, term()} | {:error, :invalid | :expired}

Verify only (repeatable within the TTL).