|
| 1 | +# 🔄 Request Lifecycle |
| 2 | +This section explains what happens when a request enters UltimateAuth. |
| 3 | + |
| 4 | +👉 From the moment an HTTP request arrives |
| 5 | +👉 Until authentication state is established or a flow is executed |
| 6 | + |
| 7 | +<br> |
| 8 | + |
| 9 | +## 🧠 Two Types of Requests |
| 10 | +UltimateAuth processes requests in two different ways: |
| 11 | + |
| 12 | +### 1. Passive Requests |
| 13 | +Regular application requests (page load, API call) |
| 14 | + |
| 15 | +### 2. Active Flow Requests |
| 16 | +Authentication flows (login, refresh, logout) |
| 17 | + |
| 18 | +👉 Both share the same foundation, but diverge at the flow level. |
| 19 | + |
| 20 | +<br> |
| 21 | + |
| 22 | +## 🧩 Middleware Pipeline |
| 23 | +Every request passes through the UltimateAuth middleware pipeline: |
| 24 | + |
| 25 | +``` |
| 26 | +Tenant → Session Resolution → (Validation) → User Resolution |
| 27 | +``` |
| 28 | + |
| 29 | +### 🏢 Tenant Resolution |
| 30 | +The system determines the tenant: |
| 31 | + |
| 32 | +- Multi-tenant → resolved via `ITenantResolver` |
| 33 | +- Single-tenant → default context applied |
| 34 | + |
| 35 | +👉 If tenant cannot be resolved → request fails early |
| 36 | + |
| 37 | +### 🔐 Session Resolution |
| 38 | +The system attempts to extract a session: |
| 39 | + |
| 40 | +- From headers, cookies, or tokens |
| 41 | +- Converted into a `SessionContext` |
| 42 | + |
| 43 | +``` |
| 44 | +SessionId → SessionContext |
| 45 | +``` |
| 46 | + |
| 47 | +👉 If no session is found → request becomes anonymous |
| 48 | + |
| 49 | +### ✔ Session Validation (Resource APIs) |
| 50 | +For API scenarios: |
| 51 | + |
| 52 | +- Session is validated immediately |
| 53 | +- Device context is considered |
| 54 | +- A validation result is attached to the request |
| 55 | + |
| 56 | +👉 This enables stateless or semi-stateful validation |
| 57 | + |
| 58 | +### 👤 User Resolution |
| 59 | +The system resolves the current user: |
| 60 | + |
| 61 | +- Based on validated session |
| 62 | +- Using `IUserAccessor` |
| 63 | + |
| 64 | +👉 This produces the final user context |
| 65 | + |
| 66 | +<br> |
| 67 | + |
| 68 | +## 🔄 Passive Request Flow |
| 69 | +For normal requests: |
| 70 | + |
| 71 | +``` |
| 72 | +Request |
| 73 | +→ Middleware pipeline |
| 74 | +→ Session resolved |
| 75 | +→ User resolved |
| 76 | +→ Application executes |
| 77 | +``` |
| 78 | + |
| 79 | +👉 No flow execution happens |
| 80 | + |
| 81 | +<br> |
| 82 | + |
| 83 | +## 🔐 Active Flow Requests |
| 84 | +For auth endpoints (login, refresh, etc.): |
| 85 | + |
| 86 | +The lifecycle continues beyond middleware. |
| 87 | + |
| 88 | +### Step 1: Flow Detection |
| 89 | +``` |
| 90 | +Endpoint → FlowType |
| 91 | +``` |
| 92 | + |
| 93 | +### Step 2: Context Creation |
| 94 | +An `AuthFlowContext` is created. |
| 95 | + |
| 96 | +It includes: |
| 97 | + |
| 98 | +- Client profile |
| 99 | +- Effective mode |
| 100 | +- Tenant |
| 101 | +- Device |
| 102 | +- Session |
| 103 | +- Response configuration |
| 104 | + |
| 105 | +👉 This defines the execution environment |
| 106 | + |
| 107 | +### Step 3: Flow Execution |
| 108 | +``` |
| 109 | +AuthFlowContext → Flow Service → Orchestrator → Authority |
| 110 | +``` |
| 111 | + |
| 112 | +### Step 4: State Mutation |
| 113 | +Depending on the flow: |
| 114 | + |
| 115 | +- Session may be created, updated, or revoked |
| 116 | +- Tokens may be issued |
| 117 | +- Security state may change |
| 118 | + |
| 119 | +### Step 5: Response Generation |
| 120 | +The system writes the response: |
| 121 | + |
| 122 | +- SessionId |
| 123 | +- Access token |
| 124 | +- Refresh token |
| 125 | +- Redirect (if needed) |
| 126 | + |
| 127 | +<br> |
| 128 | + |
| 129 | +## 🔁 Example: Login Request |
| 130 | +``` |
| 131 | +HTTP Request |
| 132 | +→ Tenant resolved |
| 133 | +→ Session resolved (anonymous) |
| 134 | +→ Flow detected (Login) |
| 135 | +→ AuthFlowContext created |
| 136 | +→ Credentials validated |
| 137 | +→ Session created |
| 138 | +→ Tokens issued |
| 139 | +→ Response returned |
| 140 | +``` |
| 141 | + |
| 142 | +<br> |
| 143 | + |
| 144 | +## 🔐 Flow Execution Boundary |
| 145 | +Authentication flows are only executed for endpoints explicitly marked with flow metadata. |
| 146 | + |
| 147 | +- Regular requests do not create an AuthFlowContext |
| 148 | +- AuthFlowContext is only created during flow execution |
| 149 | + |
| 150 | +👉 This ensures that authentication logic does not interfere with normal application behavior. |
| 151 | + |
| 152 | +<br> |
| 153 | + |
| 154 | +## 🧠 Mental Model |
| 155 | +👉 Middleware prepares the request |
| 156 | +👉 Flows change the state |
0 commit comments