JWT Auth Where a Stolen Refresh Token Burns Itself
The problem A refresh token that lives for days and never changes is a single point of failure. If it leaks — a log line, a proxy, a browser extension — it's valid until it expires, and there's no way to tell the diffe
The problem
A refresh token that lives for days and never changes is a single point of failure. If it leaks — a log line, a proxy, a browser extension — it's valid until it expires, and there's no way to tell the difference between the legitimate user and whoever stole it.
The fix: treat every refresh token as single-use
- Each successful
/auth/refreshcall rotates the token — the one just used is invalidated, a new one is issued. - If the same token is ever presented a second time, that's not a client retrying — a legitimate client never reuses a token it already exchanged. It means the token leaked and someone else is replaying it.
- The response to that reuse isn't "reject this one request" — it's revoking every active session for that user, treating it as a compromise signal.
Built with ASP.NET Core 8 Minimal APIs, BCrypt password hashing, short-lived HMAC-signed access tokens, and 12 xUnit tests — including a fake-clock abstraction so expiry logic is tested deterministically instead of with real Thread.Sleep calls.
Two real bugs, found by actually running the thing
Bug 1 — caught by the test suite itself: the rotation logic was treating any revoked token — including ones revoked through a normal, legitimate logout — as "reuse detected." That would have falsely nuked a user's other active sessions just because they logged out somewhere else. Fixed by separating WasRotated (genuine reuse signal) from IsRevoked (administrative revocation) as two distinct states instead of one conflated boolean.
Bug 2 — caught only by running the API manually, not by unit tests: /me, a protected endpoint, returned null for every claim even with a perfectly valid token. ASP.NET Core silently remaps short JWT claim names (sub, unique_name) to long ClaimTypes URIs on the way in by default — options.MapInboundClaims = false fixes it. Unit tests of the token service in isolation never would have caught this; it only shows up once the actual middleware pipeline runs.
Stack
C#, .NET 8, ASP.NET Core, BCrypt.Net-Next, xUnit.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.