
# Choose and protect the admin login

Before opening the instance to people other than you, choose through which door your administrators come in, keep the emergency account closed without locking yourself out, and set the sign-in lockout behind your reverse proxy or tunnel.

## The four doors of the administration

From strongest to weakest:

1. **An OIDC identity provider** ([setup](oidc.md), [ADR 0093](repo:docs/decisions/0093-connexion-par-un-fournisseur-oidc-et-secours-ferme.md)): each organiser signs in under their own name, with the provider's second factor. An unknown or deactivated account has no role and `/api/*` answers `403`; only an `ADMIN` right in force gives the `admin` role. `/q/openapi` and `/q/swagger-ui` follow the same rule.
2. **A local account** ([local accounts](comptes-locaux.md)): an address and a password chosen by its owner alone, then a code received by mail. Open **even behind a provider**: only the fallback is closed.
3. **The configuration's `admin` account**: a shared password, with no second factor and no name in the action log (the actor there is `admin`). Without a provider it is the ordinary door; behind a provider it is the **fallback**, closed by default ([below](#le-compte-de-secours--ne-jamais-senfermer-dehors)).
4. **The `/mcp` API key**, which opens only MCP: renewable from the MCP screen (hashed, shown once, overlap chosen), or an OAuth token from the provider for an active account ([ADR 0097](repo:docs/decisions/0097-mcp-par-oauth-et-rotation-de-la-cle.md), [`mcp-connexion.md`](../comprendre/mcp-connexion.md)).

**Run the application behind https**: session cookies only make sense encrypted in transit. Behind a proxy that terminates TLS, set `OIDC_FORCE_HTTPS=true` and, if the proxy does not forward the scheme, `QUARKUS_OIDC_AUTHENTICATION_COOKIE_FORCE_SECURE=true` so the cookie never leaves in clear. Password attempts on the closed fallback are logged as failures (address only), without counting in the lockout.

**The form session.** The `admin` account and local accounts open the same cookie, `planning-session`: encrypted, valid 8 h after the person's last gesture, `SameSite=Strict` and `HttpOnly`. Only gestures (a click, a key, a touch, a scroll, a write) extend the session: the automatic polling of a screen left open (running calculation, stream, menu state) and the read of the remaining time do not. Eight hours without a gesture, the warning appears two minutes before the end and the session closes; the delay is set by `SESSION_IDLE_TIMEOUT`. A control-room screen left displayed all day will therefore be signed out: for that use, take the [wall display](../regles/suivre-le-jour-j.md), which has its own link. "Sign out" does not just erase it: the instance remembers that this session is closed, and a copy of the cookie (shared computer, stolen cookie) gets `403` until it expires, without closing the account's other devices. This memory is in the instance's memory: a restart forgets it, and a copy signed out before it becomes valid again if `SESSION_ENCRYPTION_KEY` is kept. To close all sessions of a local account, change its password or deactivate it.

### The emergency account: never lock yourself out

The `admin` account is a shared password with no second factor: leaving it open behind a provider cancels what the provider brings, closing it exposes you to locking out every administrator the day the provider goes down or its URL changes (the account is bound to the issuer + subject pair, [`oidc.md`](oidc.md#changer-lurl-du-fournisseur)).

"Closed" means `409` on `/j_security_check` (under a prefix too) and a pre-existing session stripped of its role. `ADMIN_LOCAL_SECOURS=true` reopens it; the opening at startup and each sign-in through it (`Connexion par le compte de secours`) are in the admin sign-in log (History, Sign-ins tab), and a warning is logged while it is open. `ADMIN_PASSWORD` is still checked even closed (example value refused at startup).

**Before connecting the provider** (or with the fallback open):

1. Sign in as `admin`, create your account (your address) in the accounts register and grant it the `ADMIN` right. At the next startup, a warning is logged if the fallback is closed and no active administrator exists.
2. Sign in **through the provider** with that account: this first sign-in binds it to your identity there. Check that the administration opens.
3. Leave `ADMIN_LOCAL_SECOURS` absent (or `false`).

**The day the provider stops answering, or administrator access is lost**: set `ADMIN_LOCAL_SECOURS=true`, restart, sign in as `admin` with `ADMIN_PASSWORD`, fix things (reactivate the account, give back its right), then **remove the variable and restart**, and reread the sign-in log. If the provider's URL changed, unlink the accounts too ([`oidc.md`](oidc.md#changer-lurl-du-fournisseur)). With no access to the deployment environment, the only way out is the database (`desactive_le` or an `habilitation` row): keep `ADMIN_PASSWORD` and this paragraph within the operator's reach.

## Lock the admin sign-in form

A single pair of credentials (`admin`, no second factor) opens the personal data of ~150 people, minors included. Beyond `CONNEXION_MAX_ECHECS` consecutive failures, the address gets `429` + `Retry-After` for `CONNEXION_DUREE_BLOCAGE`, **even with the right password** (otherwise waiting one's turn would be enough). A successful sign-in clears the counter, and the window runs from the last real failure.

| Variable | Default | Use |
| --- | --- | --- |
| `CONNEXION_MAX_ECHECS` | `5` | Consecutive failures tolerated per address |
| `CONNEXION_DUREE_BLOCAGE` | `PT15M` | Lockout duration, counted from the last failure |

Success and failure both answer a redirect to the **same page**: the lockout reads the failure in Quarkus's `AuthenticationFailureEvent` (which carries the HTTP context, hence the address) and the success on the request itself, the matching event having no context.

**The address kept is the rightmost entry of `X-Forwarded-For` that is not a declared proxy**, and the header is read only if the connection comes from a proxy declared in `CONNEXION_PROXYS_FIABLES`. Without that list (the default), it is ignored and the connection's address counts: safe, and right with no proxy. **Set it** to your reverse proxies' addresses as the application sees them: otherwise all visitors behind the proxy share a counter, and the first attacker locks everybody's sign-in. It accepts **literal addresses and CIDR blocks** separated by commas (`172.18.0.0/16`, or `127.0.0.1,10.0.0.0/8`); a malformed entry stops startup rather than being ignored. Declaring a block means trusting every machine in it: fine for a private network of your own services, wrong for a public range.

> **Behind a tunnel (Pangolin, Cloudflare Tunnel, `newt`...)**, the address to declare is that of the tunnel client **as your application sees it**, a container on a bridge network. That address moves at restart; the subnet is fixed at network creation, hence the block:
>
> ```bash
> docker network inspect <réseau> -f '{{(index .IPAM.Config 0).Subnet}}'
> # → 172.18.0.0/16, la valeur à passer en CONNEXION_PROXYS_FIABLES
> ```
>
> Check first that the header arrives: if your tunnel client relays over TCP with nothing having set `X-Forwarded-For` upstream, this setting brings nothing and you must first get it added on the proxy side.

This lockout does not replace the proxy's per-IP rate limiting, which covers everything else.

### The trace it leaves

Each outcome of the form is recorded in the instance's **sign-in log** (table `journal_connexion`, "Sign-ins" tab of *History*, whatever the edition; [ADR 0076](repo:docs/decisions/0076-journal-des-connexions-admin-par-instance.md)): the successful sign-in, **each failure** and **the lockout**, once, on the failure reaching `CONNEXION_MAX_ECHECS`. Each line carries the timestamp and the address the lockout counts, and **neither the password nor the identifier typed** (a wrong identifier is often a password typed in the wrong field). A `429` refusal during the lockout writes nothing, otherwise anyone hammering the form would fill the table. Lines follow `JOURNAL_RETENTION` and do not travel in the SQL export.

Since the lockout is per address, an attack from many addresses is never blocked for long and each failure would be one more line: the trace is bounded, for the whole instance.

| Cap | Value | Beyond |
| --- | --- | --- |
| Failed attempts logged | 1,000 per hour | counted, not written (the failure and its possible lockout together) |
| Writes waiting for a worker | 64 | counted, not written (successful sign-ins included) |

A successful sign-in is never held back by the first cap. A saturated window says so in **one** warning, then their count when the next one opens. Only the trace is bounded: the lockout still counts every failure. A single address stays far from the cap (about twenty failures an hour by default): reaching it signals a distributed attack.

## Behind an access proxy

An access proxy (a service that authenticates its visitors, or a shared tunnel) remains a good first barrier, but since 4.0 it **no longer signs anybody in**: the proxy-header mode is removed ([ADR 0102](repo:docs/decisions/0102-retrait-du-mode-en-tete-de-proxy.md)) and no identity header (`Remote-Email`, `Remote-User`, `X-Forwarded-User`...) is read. What protects the instance behind it:

| Protection | What it does |
| --- | --- |
| The application's sign-in | Every `/api` route outside the public exceptions requires a session opened by one of the [four doors](#les-quatre-portes-de-ladministration) |
| The staff space | Its URL token **and** the session that only the six-digit code sent by mail opens |
| `/mcp` | The key or an OAuth token; `PLANNING_MCP_REQUIRED_HEADERS` can also demand a header that only the proxy sets ([the MCP connection](../comprendre/mcp-connexion.md#passer-un-proxy-daccès)) |
| The per-address lockouts | They trust `X-Forwarded-For` only from declared proxies (`CONNEXION_PROXYS_FIABLES`) |
| The closed origin | The application port is reachable only from the proxy (private network, firewall): otherwise the proxy is bypassed |

On the proxy side, except from its authentication only the dedicated-token prefixes, `/api/abonnements/*` and `/api/mural/*` ([public tokens](securite-jetons-publics.md)). A second factor imposed by the proxy can justify `AUTH_LOCAL_SECONDE_ETAPE=false` ([local accounts](comptes-locaux.md)); it does not replace the sign-in.
