You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -73,7 +93,7 @@ class AdminEndpoint extends BaseApiEndpoint {
73
93
74
94
### Bearer Authentication
75
95
76
-
Validates Bearer tokens from the `Authorization` header.
96
+
Validates Bearer tokens from the `Authorization` header on **every request**. Uses `InlineAuthenticationScheme` for synchronous inline token validation.
@@ -89,12 +109,18 @@ const auth = new BearerAuthenticationScheme({
89
109
});
90
110
```
91
111
92
-
**Features:**
93
-
- Automatically validates `Authorization: Bearer <token>` header format
112
+
**How it works:**
113
+
- Middleware runs for every incoming request
114
+
- Extracts `Authorization: Bearer <token>` header
115
+
- Validates token using `checkToken()`
94
116
- Returns 401 Unauthorized if token missing or invalid
95
-
- Integrates with OpenAPI security schemes
96
117
- Sets `request.authenticated = true` on successful validation
97
118
119
+
**Architecture:**
120
+
- Extends `InlineAuthenticationScheme` for stateless credential-based auth
121
+
-`getCredentials()` - Extracts Bearer token from Authorization header on each request
122
+
-`authenticate()` - Validates extracted token using `checkToken()` on each request
123
+
98
124
## Server-Level Authentication
99
125
100
126
Apply authentication to all endpoints by default:
@@ -145,6 +171,143 @@ class AdminEndpoint extends BaseApiEndpoint {
145
171
}
146
172
```
147
173
174
+
## Inline Authentication Schemes
175
+
176
+
`InlineAuthenticationScheme` runs authentication inline **on every request**. Credentials are extracted from the request and validated immediately, without requiring a separate session object.
177
+
178
+
**How it works:**
179
+
1. Middleware runs for each incoming request
180
+
2.`getCredentials(request)` extracts credentials from the request
181
+
3.`authenticate({ credentials, request })` validates the credentials
182
+
4. If validation succeeds, request continues; otherwise an error is thrown
183
+
5. No session is created or stored
184
+
185
+
**Key Methods:**
186
+
-`getCredentials(request)` - Extract credentials from the request
187
+
-`authenticate({ credentials, request })` - Validate the extracted credentials
188
+
189
+
**Use cases:**
190
+
- API key validation (fixed key in header)
191
+
- JWT token validation (stateless bearer tokens)
192
+
- Basic authentication (username/password in header)
193
+
- Any credential that can be validated synchronously on each request
`SessionAuthenticationScheme` manages stateful authentication. It provides an `AuthFlow` to **obtain a session**, and then verifies that session **on each request**.
234
+
235
+
**How it works:**
236
+
1.`getAuthFlow()` defines the multi-step authentication process (e.g., challenge → authorization → token exchange)
237
+
2. User goes through the auth flow to obtain a session
238
+
3. Session ID is stored (typically in cookies or request state)
239
+
4. On each subsequent request, middleware calls `checkSession()` to verify the session exists and is valid
240
+
5. Only after session is verified, the request continues
241
+
242
+
**Key Methods:**
243
+
-`getAuthFlow()` - Define the authentication flow steps used to obtain a session
244
+
-`getMiddleware()` - Middleware that checks the session on each request (inherited)
245
+
246
+
**Use cases:**
247
+
- OAuth2 with authorization code flow
248
+
- SAML-based authentication
249
+
- Multi-step authentication requiring user interaction
250
+
- Session-based systems where session state must be maintained
After a session is obtained through the auth flow, each request:
302
+
1. Extracts the session ID (from cookies, headers, etc.)
303
+
2. Calls `checkSession()` to verify the session still exists
304
+
3. Validates the session is not expired
305
+
4. Sets `request.authenticated = true` and allows request to continue
306
+
307
+
## AuthFlow & AuthStep
308
+
309
+
An `AuthFlow` is a named collection of `AuthStep` objects representing the complete multi-step authentication process used to obtain a session. See [src/authentication/auth-flow.ts](../src/authentication/auth-flow.ts) and [src/authentication/auth-step.ts](../src/authentication/auth-step.ts) for type definitions.
310
+
148
311
## Public Routes
149
312
150
313
Make routes explicitly public by setting `authentication = null`:
@@ -164,63 +327,104 @@ This bypasses any parent router or server authentication.
164
327
165
328
## Custom Authentication Schemes
166
329
167
-
Create custom authentication by extending `AuthenticationScheme`:
330
+
Create custom authentication by extending `InlineAuthenticationScheme` or `SessionAuthenticationScheme` depending on your needs.
// Automatically generates OAuth2 security scheme in OpenAPI spec
215
397
```
216
398
217
399
## Best Practices
218
400
219
-
1.**Use server-level auth as default** - Apply authentication at the server level and override only where needed
220
-
2.**Make public routes explicit** - Use `authentication = null` to clearly mark public endpoints
221
-
3.**Validate tokens properly** - Always validate tokens against a secure source (database, JWT verification, etc.)
222
-
4.**Use descriptive scheme names** - Help API consumers understand authentication requirements
223
-
5.**Leverage cascading** - Set auth at the appropriate level (server/router/endpoint) based on your needs
401
+
1.**Choose the right scheme type:**
402
+
- Use `InlineAuthenticationScheme` for **stateless** auth that happens on every request (API keys, JWT tokens, bearer tokens)
403
+
- Use `SessionAuthenticationScheme` for **stateful** auth with multi-step flows that obtain a session once, then check it on each request (OAuth2, SAML, login forms)
404
+
405
+
2.**Inline scheme design:**
406
+
- Keep `getCredentials()` focused on extraction only
407
+
- Keep `authenticate()` focused on validation only
0 commit comments