ProxyAuth / Security

Refresh tokens: two tokens, one id, and a trap for whoever copies one

ProxyAuthSecurity / API sessionsUpdated on 2026-09-18

A browser session can be renewed quietly: the server sets a cookie, the browser replaces it, nobody notices. An API client has no such mechanism. It receives a token, stores it somewhere, and uses it until it expires.

That leaves the same dial as for browsers, without the quiet way out. Short-lived tokens mean sending the password again — from a script, a CI job, a mobile app — which usually ends with the password stored next to the token. Long-lived tokens mean a credential sitting in a configuration file for weeks.

Refresh tokens split the credential in two: something short-lived used on every request, and something long-lived used rarely, in one place, for one purpose. Since 1.3.0 ProxyAuth issues both.

Two tokens, one id

{
  "session_cookie": false,
  "token_expiry_seconds": 600,
  "refresh_token_expiry_seconds": 86400
}
curl -s -X POST https://api.example.com/auth -d 'username=ci-runner&password=...'
{
  "token": "AS1mFWRmJ3s_…",
  "expires_at": "2026-09-17 10:29:05 +02:00",
  "refresh_token": "AXtkX_71LsWz…",
  "refresh_token_expires_at": "2026-09-18 10:19:05 +02:00"
}

Two different tokens — and the same token id inside both. That is the design decision the rest follows from.

An id is what an administrator has to work with: it is what the access log records, what /adm/stats/sessions lists, what POST /adm/revoke takes. Give the two tokens separate ids and ending a session means finding and revoking both, from two different places, hoping neither was missed. Give them one id and:

curl -X POST https://gw.example.com/adm/revoke \
  -H "X-Auth-Token: $TOKEN_ADMIN" \
  -d '{"token_id": "92002bcc…"}'

ends the session — the access token, the refresh token, and every pair a later refresh issues.

Sharing an id is only safe because the two tokens are not interchangeable. Each carries its kind: a refresh token is refused on every route, an access token is refused by /refresh_token. A long-lived credential therefore never opens a route, which is the whole point of splitting them. Versions before 1.3.0 cannot even parse the mark, so they refuse refresh tokens outright.

refresh_token_expiry_seconds: 0, the default, turns all of this off: /auth answers exactly as before. It can be set per vhost, so one API uses refresh tokens while another does not. A vhost using session cookies never has them — /auth there returns a cookie, not a body.

Renewing

curl -s -X POST https://api.example.com/refresh_token \
  -H 'content-type: application/json' \
  -d "{\"refresh_token\": \"$REFRESH\"}"

The answer has the same shape as /auth: a new access token, a new refresh token, both with the session’s id. A form body works too, and every response carries Cache-Control: no-store.

Before issuing anything, ProxyAuth checks what may have changed since the login: the account still exists, its address is still allowed, the vhost’s login rules still name it, and the session is still within session_max_lifetime_secs. A refresh is a good moment to notice that someone was removed from a group three hours ago.

The endpoint is rate-limited together with /auth, and on an OIDC vhost it is proxied to the backend like the rest.

Rotation, and what a replay means

Each refresh moves the session to the next generation, recorded next to the revocation data and shared with the other instances. Tokens of an older generation — including the previous access token — are refused from that moment.

So each refresh token works exactly once. Which makes a second use interesting: the legitimate client has the new pair and has no reason to present the old token again. A replay means two parties hold it.

ProxyAuth treats it as such and revokes the session. Both the attacker and the legitimate client are out, and the client has to sign in again — which is the point: the alternative is letting a thief keep refreshing a session forever, invisibly.

POST /auth               → A0 + R0, token id X, generation 0
GET  /data  Bearer A0    → 200
POST /refresh_token R0   → A1 + R1, same id X, generation 1
GET  /data  Bearer A0    → 401  (superseded)
GET  /data  Bearer A1    → 200
POST /refresh_token R0   → 401  and the session is revoked
GET  /data  Bearer A1    → 401

The check and the increment happen in a single step, so two simultaneous refreshes with the same token cannot both succeed on one instance.

What this asks of a client

Three habits, none of them unusual for an OAuth client:

  • Serialise refreshes. One at a time, not one per worker thread.
  • Replace both tokens as soon as the response arrives, and store them together.
  • On a 401, retry once with the new access token rather than refreshing again. A network retry of a refresh that actually succeeded looks exactly like a replay — because from the server’s side it is one.

Errors are the ones OAuth clients already handle: 400 unsupported_grant_type when refresh tokens are off for that host, 400 invalid_request for a missing token, 401 invalid_grant for anything invalid, expired, revoked or replayed, and 403 access_denied when the account is no longer allowed.

Across several instances

A generation reaches the other instances on their next sync tick, up to 15 seconds later. Within that window another instance may still accept the previous access token, and two refreshes racing on two instances are not seen as a replay. Send refresh calls to one instance, or accept the window — it is the same 15 seconds that already applies to revocation.

Choosing the two lifetimes

They answer different questions. token_expiry_seconds is how long a leaked access token is useful: minutes is a reasonable answer when the client can renew on its own. refresh_token_expiry_seconds is how long a client may be offline and still come back without credentials: a day for a mobile app, longer for a job that runs nightly.

And session_max_lifetime_secs caps both: no token of the session outlives the end of the session, and a refresh past it is refused.

One credential for requests, one for renewal, one id for both. The shared id is what keeps revocation a single command; rotation is what makes a stolen refresh token useful once; and that single use is what turns the second one into evidence.