Dough Git

commit

updates

commit
107e400cd73a58115de40b14f45d2a0360cdb7f6
author
Clove Twilight <[email protected]>
date
2026-09-16 12:34

diff --git a/src/env.ts b/src/env.ts
index 35de4b7..da039b2 100644
--- a/src/env.ts
+++ b/src/env.ts
@@ -53,6 +53,8 @@ export const TTL = {
   accessToken: 60 * 60,
   idToken: 60 * 60,
   refreshToken: 60 * 60 * 24 * 30,
+  /** A used refresh token is honoured again for this long (racing app instances). */
+  refreshReuseGrace: 60,
   /** Changing sign-in methods or using /admin needs a sign-in this recent. */
   recentAuth: 60 * 30,
   /** Retired signing keys stay in the JWKS this long so issued tokens verify. */
diff --git a/src/oidc/endpoints.tsx b/src/oidc/endpoints.tsx
index 31dc057..647276b 100644
--- a/src/oidc/endpoints.tsx
+++ b/src/oidc/endpoints.tsx
@@ -359,13 +359,18 @@ oidc.post("/token", async (c) => {
     if (!row || row.client_id !== client.id || row.revoked_at || row.expires_at <= now()) return invalid();
 
     // Rotation: each refresh token works once. Seeing one again means it was
-    // copied, so the whole family is shut down.
+    // copied, so the whole family is shut down — unless it's within a short
+    // grace period of its first use. Apps running on many instances (Workers,
+    // eventually-consistent storage) can legitimately race two refreshes with
+    // the same token; each racer gets its own valid successor.
     const claimed = await c.env.DB.prepare(
       "UPDATE refresh_tokens SET used_at = ? WHERE token_hash = ? AND used_at IS NULL",
     )
       .bind(now(), hash)
       .run();
-    if (claimed.meta.changes !== 1) {
+    // If our read saw it unused but the claim lost, another request used it just now.
+    const usedAt = row.used_at ?? now();
+    if (claimed.meta.changes !== 1 && now() - usedAt > TTL.refreshReuseGrace) {
       await c.env.DB.prepare("UPDATE refresh_tokens SET revoked_at = ? WHERE family_id = ? AND revoked_at IS NULL")
         .bind(now(), row.family_id)
         .run();