Cloud Security & Auth··2 min read

Securing B2B SaaS Multi-Tenant Authentication: Short-Lived JWTs, Refresh Token Rotation, and Reuse Detection

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Securing B2B SaaS Multi-Tenant Authentication: Short-Lived JWTs, Refresh Token Rotation, and Reuse Detection

The Revocation Paradox of Stateless JWTs

Stateless JSON Web Tokens (JWTs) are popular because API gateways can verify signatures locally without querying a database on every request. However, if you set a JWT's expiration (exp) to 7 or 30 days, you cannot revoke an individual compromised session when an employee leaves a client company or a laptop is stolen.


1. Hybrid Architecture: 10-Minute Access JWTs + Rotating Refresh Token Families

We combine the speed of stateless verification with instant revocation control:

  • Access Token (10-minute TTL): Verified in-memory at the API gateway using asymmetric Ed25519 / RS256 public keys, carrying sub, tenant_id, and role_version.
  • Refresh Token (14-day TTL in HttpOnly; Secure; SameSite=Strict cookie): Stored as a SHA-256 hash in PostgreSQL inside a token_family_id lineage.

2. Automatic Replay Theft Detection

Every time a client calls POST /auth/refresh, the server marks the used refresh token as ROTATED and issues a brand-new refresh token in the same token_family_id.

If an attacker steals an older refresh token and attempts to use it after the legitimate user has already rotated it (or vice versa), the server sees a request for an already-ROTATED token:

-- If a previously rotated token is presented again, compromise is detected:
-- Immediately revoke the entire token_family_id!
UPDATE auth_refresh_tokens
SET revoked_at = NOW(), revoke_reason = 'REPLAY_DETECTED'
WHERE token_family_id = $1 AND revoked_at IS NULL;

Both the attacker and the legitimate device are forced to re-authenticate immediately, containing token theft automatically within seconds.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles