Authentication · Showcase · 21 November 2026

Refresh tokens you can take back

A signed token is easy to check and hard to take back. The fix is to give that job to a second token the server can forget.

By Shan Padayhag · 3 min read

The Showcase Authentication Service is a small Rust project I built to practise authentication properly: register, verify an email, sign in, stay signed in, sign out. It’s a workspace of small crates in a ports-and-adapters layout, with one AWS Lambda handler per endpoint and PostgreSQL underneath. This note is about one part of it, the token that keeps you signed in, as of commit ecd4d91.

The problem with a token you can’t take back

After sign-in, the service hands out an access token: a JSON Web Token signed with HS256. Any handler can check it without touching the database, which is exactly why JWTs are popular.

That same property is the problem. A signed token stays valid until it expires, and nothing on the server can cancel it early. If access tokens lived for a week, “sign out” would only mean “forget the token in this browser”, and anyone who had copied it could keep using it.

Two tokens, two jobs

So the work is split in two:

  • The access token is short-lived. In the example configuration it lasts 15 minutes. It proves who you are on each request.
  • The refresh token lasts longer, 7 days in the same configuration, and does one thing: get a new access token.

The refresh token is not a JWT. It’s 32 random bytes, encoded as a URL-safe string, with nothing inside it to decode. It only means something because the server has a row for it. It travels in a cookie marked HttpOnly, Secure and SameSite=Strict, so page scripts can’t read it and other sites can’t send it.

Keep only the hash

The server never stores the token itself. It stores the SHA-256 hash of it, in a column with a unique index, and looks tokens up by hashing what the browser sends.

The reason is a database leak. If someone copies the refresh_tokens table, they get hashes. To use one, they’d need the 32 random bytes that produced it, and those exist only in the user’s cookie. A fast hash like SHA-256 is fine here, unlike for passwords, because the input is random and long rather than something a person chose.

Signing out means something

Because the server holds a row per refresh token, it can take one back. Each row has a revoked_at column, empty while the token is active. Signing out finds the active row by its hash and sets revoked_at. Every lookup asks for a row that is not revoked and not expired, so a revoked token is simply never found again.

The integration tests run the whole flow against PostgreSQL by calling each handler directly: register, verify, sign in, fetch the current user, refresh, sign out, and then a refresh that must be refused because the token was revoked. That last step is the one this design exists for.

What the first version leaves out

Refreshing gives you a new access token and keeps the same refresh token. The code says so in a comment: “not rotated (v1)”. So if a refresh token were stolen while it’s still active, the thief could keep refreshing until the owner signs out or the token expires.

Rotation is the next step: issue a new refresh token on every use, and treat a second use of an old one as theft. I built that later in DaiBX, where each refresh replaces the token and an old token used again, outside a 30-second grace for two tabs refreshing at once, ends the whole session. Leaving it out kept the first version of this service small enough to finish and test properly, and the comment keeps the gap visible instead of pretending it isn’t there.

The code is public: the token code and the refresh-token table.