# Email, passwords, and presets

> Email and password sign-in held to OWASP ASVS 5.0 Level 2, and the four versioned presets that configure it in one line.

From `docs/guide.md`, `README.md`, `docs/design/presets.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/guides/email-and-passwords.

The password package needs an audit key on the service: create it with `auth.WithAuditKey(key)`, where `key` is at least 32 random bytes from your secret store, for example from `openssl rand -hex 32`. The key turns each address into a label in the audit log and a rate-limit key, so neither reveals the address, even to someone who has the log and a list of candidate addresses. Keep the key for the life of the audit log: a new key gives every address a new label.

```go
pw, err := password.New(svc, store, password.Config{
	Mailer: password.MailerFunc(func(ctx context.Context, m password.Message) error {
		// Return at once; don't wait for delivery. Show the typed code in
		// large type, how long it works, and then the link.
		return queue.Enqueue(ctx, m.Kind, m.To, m.Code.Reveal(), m.ValidFor(), m.Link.Reveal())
	}),
	Links: password.Links{ // pages that post the code on a click; authroutes serves them
		Confirm: "https://app.example.com/auth/password/confirm",
		Reset:   "https://app.example.com/auth/password/reset",
	},
	NewUser: func(ctx context.Context, n password.NewUser) (auth.UserID, error) {
		// A new account, never an existing one. Store the returned ID in
		// the row: sessions name the user by it.
		return accounts.Create(ctx, n.Address)
	},
	ContextWords: []string{"tensile"},
})
```

`New` refuses a service without a limiter. The defaults follow OWASP ASVS 5.0 and NIST SP 800-63B-4: Argon2id at the OWASP minimum, a 15-character minimum and a 256-character maximum, no composition rules, no expiry, 10,000 common passwords refused, and the address and `ContextWords` don't count toward the minimum. To also refuse breached passwords, set `Breached` to a `pwned.New(pwned.Options{})` checker; it sends 5 characters of a SHA-1 to the Have I Been Pwned range API.

- **Sign-up.** `SignUp` answers nil for every address and sends one message: with `VerifyFirst`, the default, a link that creates the account when it is opened in the same browser or with the chosen password. Build links with the code in the URL fragment, for example `https://tensile.example/confirm#code=…`, and redeem them with `Confirm`, or the client's `confirmEmail()`, which reads the code from the fragment. `confirmEmail` takes an object: to pass the code yourself, call `confirmEmail({code})`, not `confirmEmail(code)`.
- **Sign-in.** `SignIn` returns a proof for `authhttp.SignIn`, which asks for TOTP when the account has it. Every failure is the same `factor_rejected`: show one message, such as "That email address or password isn't right." Store `SignedIn.Device` in the `__Host-known` cookie and pass it back, so an attacker who floods one address can't slow its owner down.
- **Strength.** A password alone is single-factor. On an account with a passkey or confirmed TOTP, a session that signed in with only a password is never fresh, and `Verify` can't step it up, so a phished password can't change factors, create tokens, or end other sessions.
- **Reset.** `Forgot` answers nil for every address and sends a code only to a verified address. `Reset` ends every session, starts none, and keeps every other factor; `ResetOptions.KeepTokens` keeps personal access tokens and OAuth grants. `StartReset` is for an administrator, who never sees the code.
- **Settings.** `Change`, `Add`, `Remove`, and `ChangeEmail` need a session that authenticated within 10 minutes; `Change` also needs the current password. An address changes only when the new one is confirmed, and the old one is told.
- **Pages.** Use `<input type="password">` and allow paste, so password managers work. The library sends no mail: your mailer does, and it never sees a password.

An account whose address isn't confirmed can't create, claim, or delegate to agents (`email_unverified`). An account without an address, such as a passkey-only account, is unaffected.

## Presets

```bash
go get tensile.tools/auth@latest
```

The service, every sign-in route, and one element make a complete passkey sign-in. Error handling is left out here:

```go
store := memstore.New()
limiter, _ := ratelimit.New(ratelimit.NewMemory(), ratelimit.DefaultRules())
svc, _ := auth.New(store, auth.WithLimiter(limiter))
go svc.Run(ctx) // seals the audit log
pk, _ := passkey.New(svc, store, passkey.Config{RPID: "localhost", Origins: []string{"http://localhost:8080"}})
mux := authhttp.NewMux(svc, authhttp.WithClientIP(), authhttp.WithOrigins("http://localhost:8080"))
authroutes.Mount(mux, authroutes.Config{Passkeys: pk, PasskeyOnly: true})
```

```html
<tensile-sign-in redirect="/"></tensile-sign-in>
<script type="module" src="/auth/client/elements/index.js"></script>
```

To see it working, run `go run ./examples/quickstart` and open `http://localhost:8080`. `examples/quickstart` is the same code with its errors handled. For a complete app with agents, approvals, a CLI, and an MCP server, run `go run ./examples/agents` and follow its tour.

## Compare the presets

| Setting | `consumer@1` | `standard@1` (default) | `strict@1` | `passkey-only@1` |
|---|---|---|---|---|
| Passwords | Yes | Yes | Yes | No |
| Verification | `VerifyLater` | `VerifyFirst` | `VerifyFirst` | Not applicable |
| Minimum length | 8 | 15 | 15 | Not applicable |
| Breached-password check | Strongly recommended | Recommended | Required: `PasswordConfig` refuses a configuration without `Breached` | Not applicable |
| Second factor | Optional | Offered | Required before a full session | The passkey |
| Recovery gate | No | No | A second factor and a confirmed recovery path before a full session | Confirmed recovery codes before a full session |
| Nudge | A passkey, after sign-in, at most once every 7 days | A passkey, right after the first sign-in, then at most once every 3 days, always with "Skip for now" | Recovery codes, then a second factor, until done; not skippable | Recovery codes until confirmed, not skippable; then a second passkey at most once every 7 days |
| Human sessions | 30 days idle, 30 days at most | 14 days idle, 14 days at most | 1 hour idle, 24 hours at most | 14 days idle, 14 days at most |
| Agent sessions | 15 minutes idle, 8 hours at most | The same | The same | The same |
| `ConfirmTTL` | 24 hours | 24 hours | 24 hours | Not applicable |
| `ResetTTL` | 10 minutes | 10 minutes | 10 minutes | Not applicable |
| A full session needs | `AssuranceSingle` | `AssuranceSingle` | `AssuranceMulti` | `AssuranceMulti` |
| ASVS | Level 1 | Level 1; Level 2 except 6.3.3 | Level 2 | Level 2 |

Agent sessions, delegation depth (4), rotation grace (10 seconds), personal access tokens (30 days by default, 366 at most, 90 idle), and OAuth grants (366 days at most, 90 idle) are the library defaults in every preset, written out explicitly.

Choices where a looser option was possible, and the safer option taken:

- **`strict@1` idles out after 1 hour.** Its sessions last at most 24 hours. ASVS 7.3.1 asks for an inactivity timeout that the application documents; NIST SP 800-63B-4 sets AAL2 reauthentication at 24 hours and 1 hour of inactivity, so `strict@1` uses both.
- **`strict@1` and `consumer@1` require the breached-password check.** ASVS 6.2.12 is a Level 2 requirement, and the check lives in `password/pwned` because it makes network calls. A preset can't import it without making every application call the network, so `PasswordConfig` refuses a `strict@1` or `consumer@1` configuration whose `Breached` is nil. `consumer@1` requires it because its 8-character minimum allows the short passwords that breach lists hold most. `standard@1`, with a 15-character minimum, records 6.2.12 as a requirement it misses until the application sets `Breached`. The `pwned` checker sends only the first 5 characters of the password's SHA-1 to the Have I Been Pwned range API, never the password.
- **`standard@1` nudges at most once every 3 days after the first offer.** The first offer comes right after the first successful sign-in, no offer has a permanent opt-out, and the cap keeps the prompt from repeating on every sign-in.

### Turning on the breached-password check

```go
checker, err := pwned.New(pwned.Options{})
cfg := p.PasswordConfig(password.Config{Mailer: mailer, NewUser: newUser, Breached: checker})
```

`pwned` sends 5 hexadecimal characters of a SHA-1 to the Have I Been Pwned range API, with padding.
