|
| 1 | +# 🔄 Refresh Token Rotation |
| 2 | + |
| 3 | +Refresh tokens in UltimateAuth are not simple long-lived tokens. |
| 4 | + |
| 5 | +They are part of a **rotation-based security system** designed to: |
| 6 | + |
| 7 | +- prevent token replay |
| 8 | +- detect token theft |
| 9 | +- enforce forward-only session progression |
| 10 | + |
| 11 | +## 🧠 Why Rotation? |
| 12 | + |
| 13 | +In traditional systems: |
| 14 | + |
| 15 | +- refresh tokens are long-lived |
| 16 | +- they can be reused multiple times |
| 17 | + |
| 18 | +👉 If stolen, an attacker can: |
| 19 | + |
| 20 | +- silently keep refreshing access tokens |
| 21 | +- stay undetected |
| 22 | + |
| 23 | +UltimateAuth solves this with: |
| 24 | + |
| 25 | +👉 **single-use refresh tokens (rotation)** |
| 26 | + |
| 27 | +## 🔁 Rotation Model |
| 28 | + |
| 29 | +Each refresh token is: |
| 30 | + |
| 31 | +- used exactly once |
| 32 | +- replaced with a new token |
| 33 | +- linked to a chain |
| 34 | + |
| 35 | +```text |
| 36 | +Token A → Token B → Token C → ... |
| 37 | +``` |
| 38 | + |
| 39 | +When a refresh happens: |
| 40 | + |
| 41 | +1. Token A is validated |
| 42 | +2. Token A is **revoked** |
| 43 | +3. Token B is issued |
| 44 | +4. Token A is marked as replaced by B |
| 45 | + |
| 46 | +## 🔐 Token State |
| 47 | + |
| 48 | +A refresh token can be: |
| 49 | + |
| 50 | +- Active → valid and usable |
| 51 | +- Revoked → already used or manually revoked |
| 52 | +- Expired → lifetime exceeded |
| 53 | +- Replaced → already rotated |
| 54 | + |
| 55 | +👉 Only **active tokens** are valid |
| 56 | + |
| 57 | +## 🚨 Reuse Detection |
| 58 | + |
| 59 | +This is the most critical security feature. |
| 60 | + |
| 61 | +If a refresh token is used **after it has already been rotated**, it means: |
| 62 | + |
| 63 | +👉 The token was reused |
| 64 | +👉 Likely stolen |
| 65 | + |
| 66 | +### What happens? |
| 67 | + |
| 68 | +When reuse is detected: |
| 69 | + |
| 70 | +- the system identifies the session chain |
| 71 | +- the entire chain can be revoked |
| 72 | +- all related sessions become invalid |
| 73 | + |
| 74 | +👉 This immediately cuts off both: |
| 75 | + |
| 76 | +- attacker |
| 77 | +- legitimate user (forcing reauthentication) |
| 78 | + |
| 79 | +## 🔗 Chain Awareness |
| 80 | + |
| 81 | +Refresh tokens belong to a **session chain**. |
| 82 | + |
| 83 | +This enables: |
| 84 | + |
| 85 | +- tracking rotation history |
| 86 | +- detecting anomalies |
| 87 | +- applying revocation at the correct scope |
| 88 | + |
| 89 | +Without chains: |
| 90 | + |
| 91 | +❌ You cannot safely detect reuse |
| 92 | +❌ You cannot know which tokens belong together |
| 93 | + |
| 94 | +## 🔄 Rotation Flow |
| 95 | + |
| 96 | +```text |
| 97 | +Client → Refresh(Token A) |
| 98 | + → Validate A |
| 99 | + → Revoke A |
| 100 | + → Issue B |
| 101 | + → Return new tokens |
| 102 | +``` |
| 103 | + |
| 104 | +## ⚠️ Invalid Scenarios |
| 105 | + |
| 106 | +### 1. Expired Token |
| 107 | + |
| 108 | +```text |
| 109 | +Token expired → reject |
| 110 | +``` |
| 111 | + |
| 112 | +### 2. Revoked Token |
| 113 | + |
| 114 | +```text |
| 115 | +Token already used → reuse detected |
| 116 | +``` |
| 117 | + |
| 118 | +### 3. Session Mismatch |
| 119 | + |
| 120 | +```text |
| 121 | +Token does not belong to expected session → reject |
| 122 | +``` |
| 123 | + |
| 124 | +## 🧠 Security Guarantees |
| 125 | + |
| 126 | +Rotation ensures: |
| 127 | + |
| 128 | +- refresh tokens are forward-only |
| 129 | +- old tokens cannot be reused safely |
| 130 | +- stolen tokens are detectable |
| 131 | +- compromise triggers containment |
| 132 | + |
| 133 | +## 🔥 Why This Matters |
| 134 | + |
| 135 | +Compared to traditional refresh tokens: |
| 136 | + |
| 137 | +| Feature | Traditional | UltimateAuth | |
| 138 | +|----------------------|------------|-------------| |
| 139 | +| Reusable tokens | ✅ | ❌ | |
| 140 | +| Reuse detection | ❌ | ✅ | |
| 141 | +| Chain awareness | ❌ | ✅ | |
| 142 | +| Automatic containment| ❌ | ✅ | |
| 143 | + |
| 144 | +## ⚠️ Design Tradeoff |
| 145 | + |
| 146 | +Rotation requires: |
| 147 | + |
| 148 | +- token storage |
| 149 | +- state tracking |
| 150 | +- additional validation logic |
| 151 | + |
| 152 | +👉 UltimateAuth chooses security over simplicity. |
| 153 | + |
| 154 | +## 🧠 Mental Model |
| 155 | + |
| 156 | +If you remember one thing: |
| 157 | + |
| 158 | +👉 A refresh token is not a reusable key |
| 159 | +👉 It is a **one-time step in a chain** |
| 160 | + |
| 161 | +## ➡️ Next Step |
| 162 | + |
| 163 | +Continue to **Access Token Behavior** to understand how short-lived tokens interact with rotation. |
0 commit comments