# Password rules

> Refuse weak passwords whenever they are set, by whoever sets them, with rules for capitals, digits, banned characters and values, and the user's own details.

Password rules make the realm refuse a weak password at the moment it's set, whether a user chooses it, an
administrator sets it, or a script writes it through the API. You declare the rules once on the type that holds the
password, and every write of that field is held to them.

Passwords matter less in a realm where users sign in with passkeys, but every realm still lets a user sign in with a
password and an authenticator app. These rules keep that path from resting on `password1`.

**A new realm has no password rules.** It sets no minimum length and no rule, so any password is accepted until you
add them. Add at least a minimum length when you set up the realm (see [Set it up](#set-it-up)).

## Before you begin

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)).
- **Where:** today the rules are set through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`). Every password set afterwards is held to them, whoever sets it.

## How it works

Each rule is a check on the password field, run on every write before the password is hashed and stored. A write
that breaks a rule is refused with `400` and `POLICY_VIOLATION`, naming each rule it broke, and nothing is saved.

| Rule | A password must |
|---|---|
| `at-least-capitals` | Hold at least `count` capital letters (1 if absent) |
| `at-least-numbers` | Hold at least `count` digits (1 if absent) |
| `cannot-contain-characters` | Hold none of the characters listed in `chars` |
| `cannot-contain-value` | Not contain the text in `value` |
| `cannot-contain-others` | Not contain the value of any field listed in `fields`, such as the username (`uid`) |

A minimum length is the field's own `minLength` in the type's schema. The realm keeps only a hash of the password,
never the password itself.

## Set it up

Rules are declared in a type's `policies`, keyed by the field they check. For the realm's users, that's the user
type's password field, `passwordHash`, which takes the password as written and stores only its hash. A type is
replaced whole, through the admin API ([Call the API](../../../develop/api/) shows how to get a session):

1. Read the user type, and keep its `ETag`:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

2. Add the rules to its `policies`, and send the whole definition back with the `ETag` as `If-Match`:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H 'If-Match: "<rev>"' \
     -H "Content-Type: application/json" \
     -d '{ "name": "user", "…": "the rest of the definition, as you read it",
       "policies": {
         "passwordHash": [
           { "id": "at-least-capitals", "count": 1 },
           { "id": "at-least-numbers", "count": 2 },
           { "id": "cannot-contain-characters", "chars": " " },
           { "id": "cannot-contain-others", "fields": ["uid", "email"] }
         ]
       } }'
   ```

   **The definition you send replaces the type's.** Send everything you read, changed only where you mean it. A
   write with no `If-Match` is refused `428`.

3. To add a minimum length, set `minLength` on the password field in the same definition, for example
   `"passwordHash": { "…": "as you read it", "minLength": 12 }`.

## What the user sees

A password that breaks a rule is refused when it's set, with `400` and `POLICY_VIOLATION`, naming each rule it broke.
The old password stays in place. Signing in is never
refused by a rule: a password set before the rule existed still works until it's changed.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| Minimum length (`minLength`) | none | any length you choose |
| Rules on the password field | none | any of the five rules, each once or more |
| `count` on `at-least-capitals`, `at-least-numbers` | `1` | any whole number |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `428` when saving the type | No `If-Match` header | Read the type and send its `ETag` |
| `412` when saving the type | Someone changed the type since you read it | Read it again and reapply your change |
| A rule seems ignored for some users | Their passwords were set before the rule | Rules apply at the next change; ask those users to change their password |

## Audit record

Every password change is a change to the user, recorded as `UPDATED` or `PATCHED` in their
[Change history](/audit-reports/audit-trail/change-history/) (in the dashboard, **Audit → Config**), with the password
itself left out. A change to the rules is a change to the user type, recorded the same way. A password change is also
sent as a credential change to anyone following the user through [Send signals](../../signals/shared-signals/).

## Good to know

- **Every write is checked, not only sign-up.** A password set by an administrator or written through the API is
  held to the same rules.
- **Existing passwords aren't checked again.** A rule you add applies to the next time each password is set.
- **Older names still work.** `at-least-X-capitals`, `at-least-X-numbers` and `cannot-contain` are accepted, and
  `value` is read where `count` is absent.
- **`cannot-contain-others` with no `fields` checks nothing.**

## Coming next

Refusing a password used before, passwords that expire, refusing passwords found in known breaches, and a different
set of rules per group of users. See
[What's next for authentication](../whats-next/#password-reuse-expiry-and-breached-passwords).

## Related

- [Sign-in methods](../sign-in-methods/#passwords): how a password sign-in is checked.
- [Sign-in chains](../sign-in-chains/): follow a password with an authenticator app.
