Packages

Native Elixir authentication compatible with datadeft-auth Rust.

Current section

Files

Jump to
Raw

USAGE.md

# Using the native Elixir 0.8 package

For the published 0.8.0 package, use `{:datadeft_auth, "~> 0.8.0"}`.
To try an unpublished checkout, use the local path setup below.

The crypto, storage and framework-neutral request, confirmation and session
services described below run with injected IO. Optional Plug helpers handle the
magic-link/session HTTP flow. See [AWS-INTEGRATION.md](AWS-INTEGRATION.md) for
signed AWS HTTPS, [ADMIN-PARITY.md](ADMIN-PARITY.md) for audited admin operations,
and [IMPLEMENTATION.md](IMPLEMENTATION.md) for the acceptance checklist.

## 1. Install an unpublished checkout from a local path

Requirements: Elixir >= 1.16, OTP >= 26, and OTP `:crypto` with ChaCha20 and
ChaCha20-Poly1305. Building or running the Hex package does not require a Rust
toolchain, platform-specific artifacts or a full repository checkout. Rust is
needed only for repository checks that exercise both implementations.

In a sibling application's `mix.exs`, add a local path dependency:

```elixir
{:datadeft_auth, path: "../datadeft-auth/hex/datadeft_auth"}
```

Then use your application's normal `mix deps.get` and `mix compile` commands.
To build a local, unpublished archive and exercise a clean consumer:

```sh
mise run hex-package      # writes target/hex/datadeft_auth-<version>.tar
mise run hex-source-smoke # unpacks the archive and builds a separate Mix release
```

Run these from the repository root. The archive consumer does not need a
repository checkout, Rust, Plug, or the optional AWS signing dependency.
See [release readiness](RELEASE-READINESS.md) for revision-specific evidence.

## 2. Email identity: available now

This normalizes exactly like Rust: trim surrounding Unicode whitespace, then
ASCII-lowercase the whole address, including the local part. It does not fold
Gmail dots or strip plus tags.

```elixir
alias DatadeftAuth.MagicLink.Core.Email

{:ok, email} = Email.normalize("  Developer@Example.Test  ")
"developer@example.test" = Email.value(email)
{:error, :invalid_email} = Email.normalize("not an address")
```

Use `Email.from_normalized/1` when reading stored identities: it validates without
silently repairing them. Inspection is redacted; `Email.value/1` is explicit
sensitive access, not something to put in logs.

## 3. Branca crypto: available now, not session authentication

This is the low-level Elixir API. The following is a development
round-trip check, not a recipe for issuing login or session cookies. Run it in a
script, not an interactive shell that prints secret assignments.

```elixir
alias DatadeftAuth.Token.Branca

# Ephemeral development key. Production must load stable, purpose-separated
# secrets and use the higher-level keyring/cookie API.
key = :crypto.strong_rand_bytes(32)
now_unix = System.system_time(:second)
payload = <<1, 2, 3>>

{:ok, token} = Branca.encode(payload, key, now_unix)
{:ok, {^now_unix, nonce, ^payload}} = Branca.decode(token, key)
24 = byte_size(nonce)
{:error, :invalid_token} = Branca.decode("not-a-token", key)

:ok
```

`encode/3` draws a fresh OS CSPRNG nonce internally. You do not choose the nonce.
`decode/2` returns authenticated `{timestamp, nonce, plaintext}` and rejects
wrong keys, tampering and noncanonical encodings. The OTP-backed JOSE provider
implements the same XChaCha20-Poly1305 AEAD as Rust.

Limits: 32-byte key, at most 1024 plaintext bytes, at most 1437 token bytes, and
a timestamp in `0..4_294_967_295`. Encode returns `{:error, :invalid_input}` for
bad shape and may return `{:error, :unavailable}`. Decode returns
`{:error, :invalid_token}` or `{:error, :unavailable}` on failure.

**Successful decryption is not a login.** This primitive does not check TTL,
future skew, purpose, key windows, country, account status or server-side session
revocation. Never authorize a request from its result alone. Do not turn the
example payload into an improvised session format: Rust compatibility requires
the exact higher-level framing. `DatadeftAuth.Token.Keyring`,
`DatadeftAuth.MagicLink.Core.ConfirmCookie`, and
`DatadeftAuth.MagicLink.Service.SessionCookie` now implement that crypto framing
and freshness. For session admission use `DatadeftAuth.MagicLink.Service.Session`
with an injected live session repository, not `SessionCookie.open/4` alone.

Never print/log keys, tokens or plaintext. Provider failures have no alternate
crypto fallback. See [CRYPTO-DEPENDENCIES.md](CRYPTO-DEPENDENCIES.md).

## 4. DynamoDB storage: implemented boundary, application IO still required

The adapter is `DatadeftAuth.AWS.DynamoDB`. It currently provides:

- `new/4` (or `new/3` with the documented default cleanup grace): explicit table,
  `DatadeftAuth.AWS.StorageKey`, and `{client_module, client_state}`.
- `find_user_for_authentication/2`: keyed lookup plus strongly consistent,
  authoritative profile validation.
- `with_previous_storage_key/2` and `rekey_email_lookups/1`: rotation fallback,
  migration and dormant-account rekeying.
- `put_magic_link_if_absent/2` and `find_magic_link_for_authentication/2`: keyed
  challenge creation and side-effect-free candidate reads.
- `commit_magic_link_authentication/2`: atomic consume/account/session/index
  transaction, **for a trusted service that has already performed verification**.
- `find_session/3`, `is_session_owner_active/3`, and `revoke_session/3`: strongly
  read and revoke live sessions, including across the rotation window.
- `check_rate_limit/5` and `check_lifetime_limit/5`: shared atomic counters.

The client must implement `DatadeftAuth.AWS.DynamoDB.Client.request/3` with the
DynamoDB JSON AttributeValue protocol and scrubbed error contract. For AWS, use
the bundled `DatadeftAuth.AWS.DynamoDB.SignedClient` with the optional
`aws_signature` dependency; see [AWS-INTEGRATION.md](AWS-INTEGRATION.md).
The loopback-only DynamoDB Local client in `test/support/` is test code, not an
AWS client to deploy. Never use ephemeral development storage keys for a real
shared table: all nodes need the configured current/previous keys.

Do not directly expose the commit API as a route. It is not a verifier and does
not replace confirmation binding, consent/policy checks or cookie minting. A
transaction `:unavailable` result may have committed: only retry the identical
command with its original attempt ID; do not regenerate IDs or blindly replan.

The real mixed-language test is
[`test/compatibility/dynamodb_test.exs`](test/compatibility/dynamodb_test.exs).
It demonstrates shared account identity and transaction/counter behavior, not a
complete production login integration.

## 5. Session admission: available with injected IO

Construct `Session.new/3` with a session keyring, a repository such as
`{DatadeftAuth.AWS.DynamoDB, store}`, and a function returning
`{:ok, unix_seconds}`. Default bounds are 24 hours idle and 30 days absolute;
`Session.new/5` takes stricter ages and a country policy.

- `Session.validate/3` accepts a cookie and a **trusted-edge** country (or
  `nil`). It checks both freshness bounds, live storage and owner status.
- `Session.refresh/2` accepts only the normal validation result, in the same
  request. It returns `{:ok, nil}` until reissue is due and preserves original
  issue time, session ID and country. Send any fresh cookie with `no-store`.
- For logout, call `Session.validate_for_logout/2` and pass the returned record's
  session ID to `Session.revoke/2`. It skips only idle/country admission checks,
  and cannot feed refresh. Require a CSRF-protected same-origin POST and revoke
  server state before clearing the cookie. Plug consumers can use the `logout/2`
  helper below.

## 6. Requesting magic links with injected delivery

Use `DatadeftAuth.MagicLink.Service.Request.new/4` with a purpose-separated
lookup key, your terms/privacy version pair, and injected repository, limiter,
outbox and clock. The DynamoDB adapter implements both repository and limiter.
An optional `LocaleAllowlist` resolves request hints to configured email locales.

`Request.request/5` checks both consents, applies the four keyed limits,
persists the challenge, then passes a redacted `Mail` to the outbox. Render its
bearer token only for delivery. The host application must render the same
public response for sent and throttled outcomes and own edge/IP/global limits.

Injected callbacks should return their typed errors. One that raises, throws
or exits, such as a `GenServer.call` timeout, gets the generic `503`, never a
`500`. A raised read keeps the user's flow cookie. A raised write or limit
check may already have applied, so confirmation clears the flow; the
flow-state check, like Rust's, keeps the cookie on every `503`. Don't leave
linked work running after a callback returns: a `Task.async` task that outlives
a caught timeout can still crash the request process later. Use
`Task.Supervisor.async_nolink/3` instead.

`Request.request/5` checks its rate-limit buckets concurrently, each in a
linked task, like Rust's `join_all`. So `check_rate_limit/5` must not depend on
the calling process (its process dictionary or `self()`), and it should enforce
its own timeout: the service waits for every bucket.

## 7. Scanner-safe flow with injected storage

Your Plug/Bandit application owns routes, rendering, authorization and edge
controls. The framework-neutral `DatadeftAuth.MagicLink.Service.Flow` provides
`land/2`, `read_state/2` and `confirm/4`. Use the same lookup key, consent
versions and challenge TTL as the request service. Pair with your session
service using the same session keyring, absolute lifetime and country policy.
The framework-neutral application integration follows this scanner-safe sequence:

1. Request: validate identity/consent/locale, apply keyed limits, enqueue email,
   and return a generic public response.
2. Landing `GET`: verify candidate state, identify the account and mint bound
   temporary flow state. Never consume a link or create a session here.
3. Confirmation: same-origin, CSRF-protected `POST`; validate the bound nonce,
   account and verifier; mint the cookie before the atomic authentication commit;
   clear temporary flow state and redirect to a fixed clean URL.
4. Authenticated requests: validate cookie policy plus live server-side session
   and account state. Refresh only after successful validation.
5. Logout: CSRF-protected `POST`; revoke server-side state before clearing the
   cookie. Independent PoW middleware owns its own proof-cookie lifecycle.

## 8. Optional Plug adapter

Add `{:plug, "~> 1.20 and >= 1.20.4"}` to the consuming project to enable
`DatadeftAuth.MagicLink.Plug`. Build its configuration with `new/5`, passing the
canonical public HTTPS origin, request service, flow service, session service,
and optional fixed redirect paths. `new/5` returns `{:error, :invalid_config}`
unless the services share the lookup key, session keyring, consent versions,
TTLs and country policy. Do not construct the origin or redirect paths from a
request. For loopback development only, pass `insecure_local: true`. Country
binding is opt-in via `country_header:`, and only safe when the edge overwrites
the header and direct origin access is blocked. A flow built with
`enforce_country: true` requires `country_header:`.

| Route (example mounting) | Helper |
| --- | --- |
| `POST /auth/magic-link/request` | `request/2` — bounded JSON; sent and throttled share one response |
| `GET /auth/magic-link/open?token=…` | `open/2` — redirect to a fixed confirmation page without echoing the token |
| `GET /auth/magic-link/flow-state` | `flow_state/2` — same-origin fetch metadata required, no CORS; returns the account label and nonce |
| `POST /auth/magic-link/consume` | `confirm/2` — checks exact origin and nonce, sets session, clears flow, redirects |
| An authenticated route | `authenticate/2` — returns the live record and an unsent no-store connection, or sends `401`/`503`; the app sets its own CSP and framing headers |
| `POST /auth/logout` | `logout/2` — checks exact origin, revokes, then clears the session and redirects |

Alternatively, use `landing/3` and a server-rendered, escaped confirmation form
instead of `open/2` and `flow_state/2`. Mount flow-state and confirmation under
`/auth` so the short-lived `dd_auth_confirm` cookie is in scope. The handlers
read their own bounded bodies; don't run generic body parsers first. A
body-reading handler that sees already-populated Plug body parameters returns
the generic `503` and reports `magic_link_body_already_parsed` or
`pow_body_already_parsed` internally. It checks this only after its own method,
origin, cookie and media-type checks pass, so a request that fails one of
those still gets its usual `400`. Confirmation leaves the flow cookie intact on
this mount error. A body that stalls past the 5-second read timeout, or has
malformed chunked framing, is answered by the HTTP server, not the handler:
Bandit raises from `read_body` and replies `408` or `400` itself, as it does
for `Plug.Parsers`. Check your server's behavior if you use another adapter,
and bound slow clients at the server and edge.
Log only `conn.private[:datadeft_auth_reason]`
for these handlers, never the raw request,
full connection, URL query, body, or cookie and response headers. Apply this
logging exclusion at the app, proxy, and edge before deployment.

The [independent Bandit example](../examples/bandit_magic_link/README.md) shows
browser login, logout and shared-client PoW with local-only storage and
delivery. For app-owned admission, see [independent PoW](POW-INTEGRATION.md).
Draft PR #45 tracks live AWS acceptance and the other remaining work.

## Validation

From the repository root:

```sh
mise run verify
mise run compatibility-test
mise run dynamodb-compatibility-test  # Docker required
mise run hex-source-smoke
```

No release is published by these tasks. The clean archive, cross-language
browser/HTTP coverage and independent security review are release acceptance
gates.