Hosting · Sign-in and MFA

Sign-in and MFA

This page covers how users sign in, what you need to set up for it, and how to help a user who is locked out.

How users sign in

  • Password. The default.
  • Email sign-in link. The sign-in page can email a one-time link instead. The link works for 15 minutes.
  • Second factor (MFA). A user who has turned on MFA must also enter a code after their password or sign-in link. The code can come from:
    • an authenticator app (TOTP),
    • an email code, valid for 10 minutes (API clients only; the sign-in pages do not offer it),
    • one of 8 single-use recovery codes, given to the user when they turn on TOTP.

A user with TOTP on must also enter an authenticator or recovery code to reset their password.

How a user turns on MFA

Each user does this for their own account, in a browser:

  1. Sign in and open Settings. The Two-Factor Authentication section is on the first page (/settings#mfa).
  2. Choose Turn on. The page shows a setup key and a link that opens an authenticator app on the same device.
  3. Add the account to the authenticator app, then enter the 6-digit code it shows.
  4. Save the 8 recovery codes. They are shown once.

The same section turns MFA off, which asks for the account password, and makes new recovery codes, which asks for a current authenticator code. New recovery codes replace the old ones.

Turning MFA on or off signs the user out everywhere else: other browsers have to sign in again. Connected apps keep their tokens and stay connected. The browser that made the change stays signed in.

A non-browser client, such as curl with the account's session, can do the same through the MFA endpoints. Web apps on other origins are blocked from these routes.

What you set up

1. Set CIVILITY_AUTH_SECRET

The server does not start without it. Generate it once and use the same value on every start.

civ api gen-secret

The secret signs sessions, reset and verification links, and MFA challenges. It also encrypts every credential you save in the admin panel (storage, email, billing, services).

If you change it, every user is signed out, pending MFA challenges fail, and the saved admin credentials can no longer be read. You have to enter them again.

2. Set up email

Email codes and sign-in links go through the provider you save in /admin/email. Until you save one:

  • new accounts count as verified,
  • reset links, sign-in links and email codes are printed to the server log, not sent.

3. Pass the right headers from your reverse proxy

Form posts from signed-in browsers must come from the server's own address, or from the address given with civ api start --site-url. Behind a proxy, the server reads its own address from X-Forwarded-Proto and X-Forwarded-Host.

Caddy and Fly set both headers. nginx needs them set by hand:

proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;

Without them, form posts on the account and admin pages fail with a 403.

4. Adjust the rate limit if needed

Sign-up, password reset, verification emails, sign-in links, email codes and app token exchange share the auth rate limit: 10 requests per minute per IP address by default. Change it in /admin/limits.

Password sign-in has its own fixed block for failed attempts, listed below.

What you cannot change

  • MFA is each user's choice. You cannot require it for everyone or for one app.
  • MFA is set up with an authenticator app, by typing in a key. The server shows no QR code. See How a user turns on MFA.
  • You cannot clear a user's MFA. See Help a locked-out user.
  • These values are fixed:
Value Setting
Failed password sign-ins before a block 10 from one IP address, then that address is blocked for 10 minutes
MFA challenge lifetime 5 minutes, single use
Failed MFA codes before a block 5, then 15 minutes. Wrong codes or passwords on the MFA settings count too.
Email code lifetime 10 minutes
Sign-in link lifetime 15 minutes
Password-reset link lifetime 1 hour
Verification link lifetime 24 hours

MFA endpoints

These are under /api/v1/auth.

Method Path Needs What it does
POST /mfa/challenge MFA challenge token Finish sign-in with a code. method is totp, email or backup.
POST /mfa/email/send MFA challenge token Email a code for the sign-in in progress.
POST /mfa/totp/setup Session Start TOTP setup. Returns an otpauth:// address.
POST /mfa/totp/confirm Session Turn TOTP on. Returns the 8 recovery codes.
DELETE /mfa/totp Session and password Turn TOTP off.
POST /mfa/recovery-codes/regenerate Session and a TOTP code Replace the recovery codes.

Recovery codes and the otpauth:// secret are returned once. They cannot be read again later.

Help a locked-out user

Situation What to do
Lost the authenticator app The user enters a recovery code. The sign-in pages do not offer an email code; it is only available through the API (POST /api/v1/auth/mfa/email/send, then /mfa/challenge with method: "email").
Lost the authenticator, the recovery codes and email access There is no way to clear MFA for a user. The account has to be deleted and created again, which loses its data on the server.
An account may be compromised Revoke its app grants on the user's page in the admin panel.
The server secret may be compromised Change CIVILITY_AUTH_SECRET. This signs everyone out. See step 1 for what else it affects.

See also