Rotating a session without logging anyone out
Was this useful?
0 unique visitors
A session token is a bearer credential: whoever holds it is the user. That leaves two unpleasant settings on the same dial. Long-lived tokens are convenient and stay useful to anyone who copies one. Short-lived tokens cut that window and put a login page in front of people in the middle of their work.
Session renewal takes the third option: keep the session, change the token. Since 1.3.0, ProxyAuth replaces a session token while it is being used, and the design questions are all in what happens to the old one.
Turning it on
{
"token_expiry_seconds": 3600,
"session_refresh_interval_secs": 900,
"session_refresh_grace_secs": 30,
"session_max_lifetime_secs": 43200
}
| Setting | What it does | Default |
|---|---|---|
session_refresh_interval_secs | age at which the next request gets a fresh token | 0 — off |
session_refresh_grace_secs | how long the replaced token keeps working | 30 |
session_max_lifetime_secs | hard end of the session, counted from the login | 0 — none |
All three can be set globally, on a vhosts: group or on a single
route, so one host can renew every fifteen minutes while another leaves
it off.
Nothing happens until session_refresh_interval_secs is set. The
startup line tells you which policy is actually in force:
session renewal every 900s, previous token accepted 30s more, sessions end 43200s after login
What a renewal does
The first request that arrives after the interval, carrying a valid session cookie, gets a new token:
- new token id, a full
token_expiry_secondslifetime, and the original login time carried over — that is what makessession_max_lifetime_secsmean “since the sign-in” rather than “since the last renewal”; - delivered in a
Set-Cookiewith exactly the attributes of the login cookie —session_token,Path=/, noDomain,Secure,HttpOnly,SameSite=Strict— so the browser replaces the old cookie instead of keeping two; - on protected routes and on public pages alike. A public page is never blocked by a session problem: it is served either way, and an invalid cookie is simply cleared on the way out.
There is no timer pushing cookies at idle browsers. Renewal is something that happens on a request, which means a session nobody uses still ends when its token expires. That is the intended behaviour: renewal extends a session in use, not an abandoned one.
The interesting part: retiring the old token
Issuing a new token is easy. Deciding when the previous one stops working is where the trade-offs are.
Too early and you break a page that is still loading. A browser sends a page request and, before the response comes back, a dozen requests for stylesheets, scripts and images — all carrying the old cookie, because the new one has not arrived yet. Refuse them and the page renders broken.
Too late and the renewal buys nothing. A cookie copied from a shared machine, or lifted from an exposed log, stays valid for the whole grace period after the legitimate browser has moved on.
ProxyAuth uses both ends:
- A grace period —
session_refresh_grace_secs, 30 seconds by default. Requests that arrive with the replaced token during that window are served, and receive the same new token rather than triggering a second renewal. A page and its assets share one renewal. - An early end as soon as the new token is used. The renewed token carries the id of the token it replaces. The first request presenting it proves the browser received it, so the old token is refused two seconds later — whatever remains of the grace period. Those two seconds cover requests already on their way.
A logout ends both at once: /logout revokes the token it receives
and, when that token came from a renewal, the one it replaced.
In practice, with a grace period of 30 seconds, a copied cookie stops working about two seconds after the real browser’s next request — not thirty.
Where the refusal is recorded
Through the regular revocation store, under a key of its own
(rotated:<token id>), holding two dates: when the token starts being
refused, and when the record can be forgotten — the moment the token
would have expired anyway. Three consequences:
- every instance sharing Redis applies the same decision, within one 15-second sync tick;
- the record survives a restart, because it is in LMDB;
- it disappears on its own, because a revocation that outlives its token protects nothing. Nothing accumulates forever.
The refusal date can only ever be brought forward. A late request carrying the old token cannot push it back, which would otherwise be a neat way of keeping a stolen cookie alive by using it.
Caching, or why a renewal can appear not to happen
A page served from the browser’s cache never reaches ProxyAuth, so it
cannot carry a new cookie. With a route’s default caching — public, max-age=300 — a visitor could browse for minutes without a single
request arriving.
Two rules close that:
- a response carrying a session cookie is sent with
private, no-store: a shared cache would otherwise hand one visitor’s session to the next; - a response to a signed-in visitor is sent with
private, no-cache, so the browser revalidates before reusing it, and renewal happens on time. Anonymous visitors keep the route’s normal caching.
The hard end
session_max_lifetime_secs is the one limit renewal cannot extend. It
counts from the original sign-in, is carried from token to token, and
past it the visitor signs in again — a page goes to the login page, an
API call gets a 401. A renewed token never outlives it either: its
expiry is capped at the end of the session.
Without it, an active session lives as long as it is used. With it, you get “you may work uninterrupted for twelve hours, then prove who you are again”, which is what most internal tools actually want.
Checking it
t=0 sign in cookie A
t=16m open any page cookie B (Set-Cookie + no-store)
t=16m another tab, still cookie A served, same cookie B
t=16m the browser uses B on any page A is retired
t=16m+ a copy of A, two seconds later 302 to the login page
In the browser’s developer tools: the response carries
set-cookie: session_token=… and cache-control: private, no-store,
and the stored cookie value changes. If the startup line says
session renewal off, the setting is not reaching the running binary —
a misspelt key is reported at startup with the name it was probably
meant to be.
What it does not do
Renewal shortens the life of a leaked cookie; it does not detect the
leak. Both browsers hold a valid session as long as each keeps using it,
and whoever presents a token first gets the renewal. For browser
sessions that is the honest limit of the mechanism — HttpOnly,
Secure, SameSite=Strict, a short token_expiry_seconds and
session_max_lifetime_secs do the rest.
API sessions can go further, because a refresh token is used in one place and rarely: there, a token presented twice is evidence, and ProxyAuth revokes the session (no write).
Replace the token while the session is used, keep the old one just long enough for a page to finish loading, and end it as soon as the new one is in hand. The result changes nothing for the person using the site, and a great deal for anyone holding a copy of their cookie.