@@ -19,14 +19,10 @@ RxDBDotNet is a powerful .NET library that implements the [RxDB replication prot
19
19
- [ Policy-Based Security] ( #policy-based-security )
20
20
- [ Subscription Topics] ( #subscription-topics )
21
21
- [ Custom Error Types] ( #custom-error-types )
22
- - [ OpenID Connect (OIDC) Support for Subscription Authentication ] ( #openid-connect-oidc-support- for-subscription-authentication )
22
+ - [ Authentication Schemes for Subscriptions ] ( #authentication-schemes- for-subscriptions )
23
23
- [ Example Application] ( #example-application )
24
24
- [ Prerequisites for Running the Example Application] ( #prerequisites-for-running-the-example-application )
25
25
- [ Running the Full Stack] ( #running-the-full-stack )
26
- - [ RxDB Replication Protocol Details] ( #rxdb-replication-protocol-details )
27
- - [ Custom Implementations for RxDB Clients] ( #custom-implementations-for-rxdb-clients )
28
- - [ Security Considerations] ( #security-considerations )
29
- - [ Server-Side Timestamp Overwriting] ( #server-side-timestamp-overwriting )
30
26
- [ Contributing] ( #contributing )
31
27
- [ License] ( #license )
32
28
- [ Code of Conduct] ( #code-of-conduct )
@@ -200,31 +196,115 @@ builder.Services
200
196
});
201
197
```
202
198
203
- ### OpenID Connect (OIDC) Support for Subscription Authentication
199
+ ### Authentication Schemes for Subscriptions
204
200
205
- RxDBDotNet supports OIDC configuration for JWT validation in GraphQL subscriptions .
201
+ RxDBDotNet supports configuring multiple authentication schemes for GraphQL subscriptions over WebSocket connections. This feature provides flexibility in token validation and supports scenarios where you need to handle tokens from different authentication sources .
206
202
207
- Key Features:
203
+ #### Basic Configuration
208
204
209
- - Dynamic retrieval of OIDC configuration, including signing keys
210
- - Support for key rotation without requiring application restarts
211
- - Seamless integration with existing JWT authentication setups
205
+ By default, RxDBDotNet uses the standard JWT Bearer authentication scheme (` "Bearer" ` ). You can use this with minimal configuration:
212
206
213
- Usage:
207
+ ``` csharp
208
+ builder .Services
209
+ .AddAuthentication (JwtBearerDefaults .AuthenticationScheme )
210
+ .AddJwtBearer (options =>
211
+ {
212
+ options .TokenValidationParameters = // ... your token validation parameters
213
+ });
214
214
215
- 1 . Configure JWT Bearer authentication with OIDC support:
215
+ builder .Services
216
+ .AddGraphQLServer ()
217
+ .AddMutationConventions ()
218
+ .AddReplication () // Uses default Bearer scheme
219
+ ```
216
220
217
- ``` csharp
218
- builder .Services .AddAuthentication (JwtBearerDefaults .AuthenticationScheme )
219
- .AddJwtBearer (options =>
220
- {
221
- options .Authority = " https://your-oidc-provider.com" ;
222
- options .Audience = " your-api-audience" ;
223
- // Other JWT options...
224
- });
225
- ```
221
+ #### Custom Authentication Schemes
222
+
223
+ You can configure multiple authentication schemes to validate subscription tokens:
224
+
225
+ ``` csharp
226
+ // Configure multiple JWT Bearer schemes
227
+ builder .Services
228
+ .AddAuthentication ()
229
+ .AddJwtBearer (" Bearer" , options =>
230
+ {
231
+ options .Audience = " DefaultAudience" ;
232
+ options .TokenValidationParameters = // ... default scheme parameters
233
+ })
234
+ .AddJwtBearer (" CustomScheme" , options =>
235
+ {
236
+ options .Audience = " CustomAudience" ;
237
+ options .TokenValidationParameters = // ... custom scheme parameters
238
+ });
239
+
240
+ // Configure RxDBDotNet to use both schemes
241
+ builder .Services
242
+ .AddGraphQLServer ()
243
+ .AddMutationConventions ()
244
+ .AddReplication (options =>
245
+ {
246
+ options .Security
247
+ .TryAddSubscriptionAuthenticationScheme (" CustomScheme" );
248
+ // The default "Bearer" scheme is already included
249
+ });
250
+ ```
251
+
252
+ When a client attempts to establish a WebSocket connection, RxDBDotNet will:
253
+ 1 . Try to validate the token using each configured scheme in order
254
+ 2 . Accept the connection with the first scheme that successfully validates the token
255
+ 3 . Reject the connection if no scheme can validate the token
256
+
257
+ #### OIDC Support
258
+
259
+ The subscription authentication system fully supports OpenID Connect configuration, including dynamic key retrieval and rotation:
260
+
261
+ ``` csharp
262
+ builder .Services
263
+ .AddAuthentication ()
264
+ .AddJwtBearer (" OidcScheme" , options =>
265
+ {
266
+ options .Authority = " https://your-identity-provider.com" ;
267
+ options .Audience = " your-api" ;
268
+ options .RequireHttpsMetadata = true ;
269
+ });
270
+
271
+ builder .Services
272
+ .AddGraphQLServer ()
273
+ .AddMutationConventions ()
274
+ .AddReplication (options =>
275
+ {
276
+ options .Security
277
+ .TryAddSubscriptionAuthenticationScheme (" OidcScheme" );
278
+ });
279
+ ```
280
+
281
+ The system will automatically:
282
+ - Retrieve OIDC configuration from the authority
283
+ - Use the latest signing keys
284
+ - Handle key rotation without requiring application restarts
285
+
286
+ #### Client Usage
287
+
288
+ Clients should include the JWT token in the WebSocket connection initialization:
289
+
290
+ ``` typescript
291
+ const client = createClient ({
292
+ url: ' ws://your-api/graphql' ,
293
+ connectionParams: {
294
+ headers: {
295
+ Authorization: ` Bearer ${jwtToken } `
296
+ }
297
+ }
298
+ });
299
+ ```
300
+
301
+ #### Error Handling
302
+
303
+ The subscription authentication system provides detailed error information:
226
304
227
- 2 . The SubscriptionJwtAuthInterceptor will automatically use the OIDC configuration for subscription authentication.
305
+ - If authentication fails, the WebSocket connection is closed with code 4403 (Forbidden)
306
+ - Error details are included in the close message
307
+ - Connection attempts are logged for debugging purposes
228
308
229
309
## Example Application
230
310
0 commit comments