# Sign-in methods

> How people sign in to a Postern Hub - passwords, two-step verification, passkeys and single sign-on with Authelia, Authentik or Keycloak - plus sessions, re-authentication and rate limits.

Source: https://docs.posternmail.app/admin/sign-in/

This page covers every way to sign in to a Hub, the rules behind each one, and how to connect
your own identity provider for single sign-on. It also explains how long sessions last, when the
Hub asks people to confirm it's them again, and how it slows down password guessing.

| Method | Where | Needs |
|---|---|---|
| Email and password | Every Hub | Nothing. Always available. |
| Two-step verification (authenticator app) | Added to password sign-in | Each person turns it on in **Settings → Security**. |
| Passkey | Sign-in page, **Sign in with a passkey** | HTTPS with a domain name, or `localhost`. |
| Single sign-on (OIDC) | Sign-in page, **Continue with** followed by your provider's name | `POSTERN_OIDC_*` variables. |
| Code from another device | Sign-in page, **Sign in with a code from another device** | A device that is already signed in. See [Pairing](https://docs.posternmail.app/apps/pairing/). |

## Passwords

| Rule | Value |
|---|---|
| Minimum length | 10 characters (counted as characters, so accents and emoji count once) |
| Maximum length | 1024 bytes |
| Other rules | None. No required digits or symbols, no forced expiry. A long passphrase is the best choice. |
| Storage | Argon2id hash (64 MiB memory, 3 passes, 2 threads, random salt). The password itself is never stored. |
| Email | Not case-sensitive. `Alex@Example.com` and `alex@example.com` are the same account. |

A wrong email and a wrong password give the same answer, "Incorrect email or password", and take
the same time, so the sign-in page does not reveal which accounts exist.

People change their password in **Settings → Account → Password** (current password, then new
password, then **Change password**). Their other devices are signed out ("Password changed.
Your other devices were signed out."). Forgotten passwords are reset by an admin: see
[Reset a password](https://docs.posternmail.app/admin/users/#reset-a-password).

## Two-step verification

Two-step verification adds a 6-digit code from an authenticator app (1Password, Authy, Google
Authenticator, Aegis and similar) to every **password** sign-in.

### Turn it on

1. Open **Settings → Security** and click **Set up** next to **Two-step verification**.
2. Scan the QR code with your authenticator app, or type the key shown under **Or enter this key**.
   The entry is named after your Hub and your sign-in email.
3. Enter the 6-digit code under **Code from the app** and click **Turn on**.
4. **Save your recovery codes.** The Hub shows 10 codes such as `k7qfm-2x9hc`. Each works once if
   you lose your phone, and they are not shown again. Click **Copy**, store them in a password
   manager or on paper, then click **I saved them**.

> **Check:** sign out and sign in again. After the password, the page asks for "the 6-digit code
> from your authenticator app".

> **If it did not work:** "That code is not correct" during setup almost always means the clock of
> the phone or the Hub is off. Codes change every 30 seconds and the Hub accepts the code before
> and after the current one. Turn on automatic time on the phone and NTP on the server
> (`timedatectl` on Linux shows "System clock synchronized: yes").

### How codes work

| Detail | Value |
|---|---|
| Code format | 6 digits, new every 30 seconds (TOTP, SHA-1) |
| Clock tolerance | One step either way (about 30 seconds) |
| Reuse | Each code works once. A code already used is refused even within its 30 seconds. |
| Recovery codes | 10, each usable once. Turning two-step verification off and on again creates a new set. |
| Storage | The secret is encrypted with the person's own data key. Recovery codes are stored as hashes. |

### Sign in with a recovery code

On the two-step page click **Use a recovery code instead** and enter one of the codes (dashes
and capitals do not matter). The code is then used up. When the last codes run low, turn two-step
verification off and on again to get a new set.

### Turn it off

**Settings → Security → Turn off…** asks for the password ("Your password alone will be enough to
sign in. Enter it to confirm."). Accounts without a password (created by single sign-on) confirm
with a current code instead.

If someone lost both the phone and the recovery codes, an admin turns it off on the server with
`postern-hub admin disable-2fa`. See [Users and invites](https://docs.posternmail.app/admin/users/#turn-off-two-step-verification-for-someone).

> [!NOTE]
> Two-step verification only applies to password sign-in. Passkeys already combine something
> you have with your device's lock, and with single sign-on your identity provider is in charge
> of the second factor. Neither asks for the code.

## Passkeys

A passkey signs you in with Face ID, Touch ID, Windows Hello, your phone or a security key,
without typing the email or password.

### Requirements

Passkeys (WebAuthn) only work in what browsers call a secure context, and they are tied to one
domain name. The Hub offers them only when its address meets both:

| Hub address | Passkeys |
|---|---|
| `https://mail.example.com` | Yes |
| `https://hub.tailnet-1234.ts.net` | Yes |
| `http://localhost:8080`, `http://postern.localhost:8080` | Yes (on that computer only) |
| `http://192.168.1.20:8080` | No: plain HTTP |
| `https://192.168.1.20` | No: an IP address cannot own a passkey |
| `http://127.0.0.1:8080` | No: an IP address, use `http://localhost:8080` instead |

The address is `POSTERN_PUBLIC_URL`, or the address in the browser when the variable is not set.
When passkeys are unavailable, **Settings → Security** says "Passkeys only work when your Hub is
opened over a secure (https) address." and the sign-in page has no passkey button. See
[HTTPS](https://docs.posternmail.app/reach/https/) for ways to get a certificate.

### Add and use a passkey

1. Sign in to the web app at your Hub's HTTPS address.
2. Open **Settings → Security** and click **Add a passkey**. Follow your browser's prompt.
3. The passkey appears in the list, named after the device you added it on, with **Added** and
   **last used** dates. **Remove** deletes it.

To sign in, click **Sign in with a passkey** on the sign-in page. You do not need to enter your
email: the browser offers the passkeys it has for this Hub.

The desktop and mobile apps that get mail directly from your providers do not manage passkeys
themselves. Their **Settings → Security** shows "Manage passkeys in the web app at" followed by
your Hub's address, with an **Open web app** button.

> [!WARNING]
> A passkey belongs to the exact domain it was created on. If you move the Hub to another
> address (a new domain, from Tailscale to your own domain, from the desktop app to a server),
> existing passkeys stop working. Sign in with your password at the new address and add new
> passkeys there.

## Single sign-on (OIDC)

If you run an identity provider such as Authelia, Authentik or Keycloak, people can sign in to
the Hub with it. The sign-in page then shows **Continue with** followed by the name you choose.

### Settings

Set these on the Hub, then restart it.

| Variable | Default | Meaning |
|---|---|---|
| `POSTERN_OIDC_ISSUER` | empty | The issuer URL of your provider, exactly as its discovery document states it (see below). Empty turns single sign-on off. |
| `POSTERN_OIDC_CLIENT_ID` | empty | The client ID you registered. Required together with the issuer. |
| `POSTERN_OIDC_CLIENT_SECRET` | empty | The client secret. |
| `POSTERN_OIDC_NAME` | `Single sign-on` | The button label: `Continue with Authentik`. |
| `POSTERN_OIDC_AUTO_REGISTER` | off | Create an account for anyone your provider signs in who has no account yet. They become a **member**, without a password. This works even when **New accounts** is set to **Closed**: your provider decides who gets in. |
| `POSTERN_OIDC_LINK_BY_EMAIL` | off | On someone's first single sign-on, attach it to the existing Hub account with the same email. See [Linking existing accounts](https://docs.posternmail.app/admin/sign-in/#linking-existing-accounts). |

What to register with your provider:

| Setting | Value |
|---|---|
| Redirect URI | `{POSTERN_PUBLIC_URL}/api/v1/auth/oidc/callback`, for example `https://mail.example.com/api/v1/auth/oidc/callback` |
| Client type | Confidential (with a client secret) |
| Grant type | Authorization code. The Hub always uses PKCE (S256) and a nonce. |
| Scopes the Hub requests | `openid email profile` |
| Claims the Hub reads from the ID token | `sub`, `email`, `email_verified`, `name`, `preferred_username` |
| Token endpoint authentication | `client_secret_basic` (the Hub falls back to `client_secret_post`) |

> [!IMPORTANT]
> Set `POSTERN_PUBLIC_URL`. Without it the Hub builds the redirect URI from the address each
> browser used, and your provider rejects any address you did not register.

> [!IMPORTANT]
> The Hub needs `email` and `email_verified: true` **in the ID token**. Without a verified email
> nobody can be linked or registered, and the sign-in page shows "Your sign-in provider did not
> confirm a verified email address for this account." Some providers leave these claims out of
> the ID token by default; the examples below show how to include them.

The issuer must match character for character, including a trailing slash if your provider
uses one. To see the right value, open `<issuer>/.well-known/openid-configuration` in a browser
and copy the `issuer` field.

Where to put the variables:

**Docker Compose:**
`compose.yaml` does not pass the `POSTERN_OIDC_*` variables through `.env`. Add them to
`~/postern/compose.override.yaml`:

```yaml
services:
  hub:
    environment:
      POSTERN_OIDC_ISSUER: https://auth.example.com
      POSTERN_OIDC_CLIENT_ID: postern
      POSTERN_OIDC_CLIENT_SECRET: paste-the-secret-here
      POSTERN_OIDC_NAME: Authelia
```

Then apply it:

```sh
cd ~/postern
docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Add the lines to `/etc/postern/hub.env` (one `KEY=value` per line, no inline comments):

```ini
POSTERN_OIDC_ISSUER=https://auth.example.com
POSTERN_OIDC_CLIENT_ID=postern
POSTERN_OIDC_CLIENT_SECRET=paste-the-secret-here
POSTERN_OIDC_NAME=Authelia
```

Then restart:

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
The desktop app's built-in Hub cannot be configured with environment variables, and it is only
reachable as `http://localhost` on that computer. Single sign-on needs a Hub on a server.

> **Check:** run `postern-hub doctor` (see [Doctor](https://docs.posternmail.app/admin/doctor/)). Under **Sign-in & providers** it
> shows `✓ OIDC issuer https://auth.example.com reachable ("Authelia")`. Then open the sign-in page
> in a private window: the **Continue with Authelia** button is there.

> **If it did not work:** `✗ OIDC issuer ... is not reachable` means the Hub cannot fetch the
> discovery document: check DNS and the firewall from the Hub's machine. `OIDC is partially
> configured` means the issuer or the client ID is missing. A redirect error at your provider
> means the redirect URI does not match. A message on the Hub's sign-in page is explained in
> the table below.

### Example: Authelia

Authelia's issuer is its own address, for example `https://auth.example.com`. Generate a client
secret and its hash with Authelia's tool:

```sh
authelia crypto hash generate pbkdf2 --variant sha512 --random --random.length 72 --random.charset rfc3986
```

The `Random Password` goes into `POSTERN_OIDC_CLIENT_SECRET`, the `Digest` into Authelia's
configuration:

```yaml
identity_providers:
  oidc:
    claims_policies:
      postern:
        id_token: ['email', 'email_verified', 'name', 'preferred_username']
    clients:
      - client_id: 'postern'
        client_name: 'Postern'
        client_secret: '$pbkdf2-sha512$310000$...'
        public: false
        authorization_policy: 'two_factor'
        claims_policy: 'postern'
        require_pkce: true
        pkce_challenge_method: 'S256'
        redirect_uris:
          - 'https://mail.example.com/api/v1/auth/oidc/callback'
        scopes: ['openid', 'email', 'profile']
        response_types: ['code']
        grant_types: ['authorization_code']
        token_endpoint_auth_method: 'client_secret_basic'
```

The `claims_policies` block puts the email and name into the ID token, which recent Authelia
versions no longer do by default. Authelia marks an email as verified when it comes from its
user database or LDAP. Hub settings:

```ini
POSTERN_OIDC_ISSUER=https://auth.example.com
POSTERN_OIDC_CLIENT_ID=postern
POSTERN_OIDC_CLIENT_SECRET=the-random-password
POSTERN_OIDC_NAME=Authelia
```

### Example: Authentik

1. In the Authentik admin interface, open **Applications → Providers → Create**, choose
   **OAuth2/OpenID Provider**.
2. Name it `Postern`, pick an authorization flow, set **Client type** to **Confidential**, and add
   the redirect URI `https://mail.example.com/api/v1/auth/oidc/callback` (mode strict).
3. Under advanced settings keep the scopes `openid`, `email` and `profile`, and leave **Include
   claims in id_token** on.
4. Create an application (**Applications → Create**) with the slug `postern` and select the
   provider. Copy the **Client ID** and **Client Secret** from the provider.

Authentik's issuer contains the application slug and ends with a slash:

```ini
POSTERN_OIDC_ISSUER=https://auth.example.com/application/o/postern/
POSTERN_OIDC_CLIENT_ID=the-client-id
POSTERN_OIDC_CLIENT_SECRET=the-client-secret
POSTERN_OIDC_NAME=Authentik
```

Check the `email_verified` value Authentik sends for your users (the provider's **Preview**
tab shows the ID token). If it is `false`, add a scope mapping for the `email` scope that returns
`email_verified: True` for the users whose addresses you manage, and select it in the provider
instead of the default one.

### Example: Keycloak

1. In your realm, open **Clients → Create client**. Client type **OpenID Connect**, client ID
   `postern`.
2. Turn **Client authentication** on and keep **Standard flow** checked.
3. Set **Valid redirect URIs** to `https://mail.example.com/api/v1/auth/oidc/callback` and
   **Web origins** to `https://mail.example.com`.
4. Optionally set **Advanced → Proof Key for Code Exchange Code Challenge Method** to `S256`.
5. Copy the secret from the **Credentials** tab.

Keycloak 17 and later use `/realms/<realm>` (older versions `/auth/realms/<realm>`):

```ini
POSTERN_OIDC_ISSUER=https://auth.example.com/realms/home
POSTERN_OIDC_CLIENT_ID=postern
POSTERN_OIDC_CLIENT_SECRET=the-client-secret
POSTERN_OIDC_NAME=Keycloak
```

Keycloak sends `email_verified` from each user's **Email verified** switch. Turn it on for the
people who should be able to use the Hub.

### How the Hub finds the account

On every single sign-on the Hub looks for the account in this order:

1. **An account already linked to this identity.** The link is the issuer plus the provider's
   user ID (`sub`). Changing `POSTERN_OIDC_ISSUER` later breaks existing links.
2. **An account with the same verified email**, only if `POSTERN_OIDC_LINK_BY_EMAIL` is on. The
   identity is then linked for good.
3. **A new member account**, only if `POSTERN_OIDC_AUTO_REGISTER` is on. Its name comes from
   `name`, then `preferred_username`, then the part of the email before the `@`. It has no
   password, so it signs in with single sign-on (or a passkey added later).

Accounts created with an invite or `create-user` can be linked by step 2. The first account
of a Hub cannot be created by single sign-on: run the setup wizard first.

### Linking existing accounts

With `POSTERN_OIDC_LINK_BY_EMAIL` off (the default), people who already have a password account
cannot use single sign-on for it. Turn it on only if your provider verifies email addresses and
nobody else can register one of your users' addresses there: whoever controls that address at
the provider gets into the Hub account.

One extra guard applies even when it is on: if an account's sign-in email was changed in the
last 7 days, it is not linked. This stops someone who just changed an account's email to their
own address from taking it over through the provider.

### Messages on the sign-in page

| Message | What to do |
|---|---|
| "Your sign-in provider did not confirm a verified email address for this account." | Send `email` and `email_verified: true` in the ID token (see the examples). |
| "An account for ... already exists on this Hub but is not linked to single sign-on. ..." | Sign in with the password, or turn on `POSTERN_OIDC_LINK_BY_EMAIL`. |
| "The sign-in email of this account was changed recently, so it can't be linked to single sign-on yet. ..." | Wait until 7 days after the change, or sign in with the password. |
| "This account is linked to a different single sign-on identity." | The account is already linked to another user at the provider. |
| "There is no account for ... on this Hub. Ask the administrator for an invite." | Invite the person, or turn on `POSTERN_OIDC_AUTO_REGISTER`. |
| "This account has been disabled." | An admin disabled the account. |
| "This sign-in link is not valid in this browser. Please try again." | The sign-in was started in another browser, or cookies are blocked. |
| "This sign-in attempt has expired. Please try again." | More than 10 minutes passed at the provider. |
| "Sign-in failed: the identity token could not be verified." | Wrong client ID, an issuer that does not match, or a clock that is off. The Hub log has the reason (`oidc id token rejected`). |
| "The sign-in provider is unreachable." | The Hub cannot reach the issuer. Run `doctor`. |
| "Sign-in was cancelled: ..." | The provider returned an error; its text follows. |

## Sessions and signing out devices

Each sign-in creates a device with its own session. **Settings → Devices** lists them ("Everywhere
you're signed in."), with **This device** marked.

| Client | Session lifetime |
|---|---|
| Web app and desktop app | 30 days, extended every time the device is used |
| iOS and Android | 90 days, extended every time the device is used |

A device you keep using stays signed in. A device unused for longer than its lifetime is signed
out and removed from the list.

Ways a session ends:

| Action | Effect |
|---|---|
| **Settings → Devices → Sign out…** on a device | That device is signed out now. Its saved mail is removed the next time it connects. |
| **Sign out** in **Settings → Account** | This device only. |
| Changing your password | Every other device is signed out. |
| An admin resets your password, disables or deletes the account | Every device is signed out. |

Lost a phone or a laptop? Sign it out under **Settings → Devices** from any other device, then
change your password if it was not locked.

The web app and the desktop app keep the session in an HttpOnly cookie named `mc_session`; the
mobile apps use a bearer token.

## Confirming it's you again

Some actions need proof that the person at the keyboard is the account owner, not just someone
with a signed-in browser. The Hub counts a password, a two-step code, a passkey or a single
sign-on on this device within the **last 10 minutes** as proof.

| Action | What the Hub asks for |
|---|---|
| Change the password | The current password, always |
| Turn off two-step verification | The password, always |
| Delete the account | The password, always |
| Change the sign-in email | The password, unless you signed in on this device within 10 minutes |
| Allow a desktop or mobile app to sign in through the browser ("Sign in on ...?") | The password, unless you signed in within 10 minutes |
| Approve a desktop or mobile app from **Connect a device** in the web app | The password, unless you signed in within 10 minutes |

Accounts without a password (created by single sign-on) confirm with a two-step code if they
have one. Otherwise they have to sign out and sign in again: the Hub says "For your security,
please confirm it's you: sign out, sign in again and retry within 10 minutes."

A wrong password during a confirmation counts as a failed sign-in for the lockout below.

Desktop and mobile apps that fetch your mailbox sign-ins to get mail directly need a session
that was created with a password, passkey or single sign-on (or confirmed that way when it was
approved). Otherwise the app shows "Sign in on this device with your password, passkey or single
sign-on to use your mailbox credentials here."

## Rate limits

The Hub slows down guessing with limits kept in memory (a restart resets them). When a limit is
hit, the request fails with "Too many attempts. Please wait a moment and try again." and a
`Retry-After` header.

| What | Per | Allowed |
|---|---|---|
| Sign-in attempts (password and passkey) | IP address | 20 at once, then 1 every 15 seconds |
| Failed passwords and codes | Account (email) | 5, then 1 per minute; a successful sign-in resets it |
| Setup wizard and invite registrations | IP address | 5 at once, then 1 per minute |
| Pairing codes entered | IP address | 10 at once, then 1 every 30 seconds |
| Other public sign-in requests (invite lookups, starting a passkey or single sign-on) | IP address | 30 at once, then 1 every 5 seconds |
| Turning on two-step verification | Account | 5 tries, then 1 per minute |

The account limit applies to addresses that do not exist too, so it does not reveal which
accounts are real. Behind a reverse proxy the IP is only right if `POSTERN_TRUST_PROXY` and
`POSTERN_TRUSTED_PROXIES` are set correctly (see [Configuration](https://docs.posternmail.app/configuration/)); otherwise
every visitor shares the proxy's address and its limit.

Failed sign-ins are not written to the Hub's log. Each person sees them in **Settings → Security →
Recent security activity** as "Failed sign-in attempt", with the device name and IP address.
