Packages

Native Elixir authentication compatible with datadeft-auth Rust.

Current section

Files

Jump to
datadeft_auth POW-INTEGRATION.md
Raw

POW-INTEGRATION.md

# Independent proof-of-work admission

The Elixir PoW implementation matches the Rust challenge, solve, `dd_pow`
proof-cookie and per-solve counter formats. It does not depend on magic links
or sessions. A proof only grants work-based admission, not authentication.

- Load an independent 32-byte `DatadeftAuth.PoW.Secret` and a `:pow_proof`
  `DatadeftAuth.Token.Keyring`. A proof keyring may include verify-only slots;
  it mints only with the active slot.
- Use `DatadeftAuth.PoW.Policy.new(difficulty, per_solve)` to configure how many
  requests one solve buys. The recommended production floor is five leading
  zero hex characters; tune against representative browsers. Defaults are a
  two-minute challenge, three-hour proof and 60-second future skew.
- Use one shared `DatadeftAuth.PoW.ProofBudget` store for every replica.
  `DatadeftAuth.AWS.DynamoDB` implements it over the same `pow:budget:<tid>`
  lifetime counters as Rust. The first spend fixes expiry, including the
  challenge's reissue window. A reissued proof of the same solve buys no new
  admissions. A store failure never admits a request.
- With optional Plug, construct `DatadeftAuth.PoW.Plug` with the exact public
  origin, keys, policy and `{module, state}` store. Map `create/2` and
  `validate/2` to same-origin POST routes before global body parsers. Call
  `admit/2` before handling a protected request and stop on its sent refusal.
  Configure your application or edge to enforce origin/CSRF, IP and global
  limits on protected routes as appropriate. The library does not select routes.
- Reuse `@datadeft/protect-client`, not a second solver. It retries once on
  `401` with `WWW-Authenticate: DD-PoW`. The library emits that same response
  for absent, invalid and exhausted proofs. It sends the shared no-store
  `503` for failures and leaves reason labels internal. The magic-link flow
  does not own or clear `dd_pow`.

`DatadeftAuth.PoW.Flow` exposes the same create, verify/mint, and admission steps
without Plug. A custom adapter supplies CSPRNG bytes and Unix milliseconds for
challenge mint/verify; the proof cookie and budget use Unix seconds. A custom
classifier can carry an app-defined byte in the encrypted proof. Do not treat
an unknown mint-to-verify delta as a fast solve or reject on slow timing alone.

The local [Bandit example](../examples/bandit_magic_link/README.md) shows one
composition. `mise run compatibility-test` exercises Rust↔Elixir challenge and
v1/v2 proof exchange. `mise run dynamodb-compatibility-test` races both languages
against one disposable table and confirms budget exhaustion survives reissue
and storage-key rotation. The Bandit `browser-test` runs the actual shared
worker in Chromium.