dioxus-clerk 0.2.0

Clerk integration for Dioxus: components, hooks, and SSR initial state for web and fullstack apps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#![cfg(feature = "server")]

//! Integration tests for Axum extraction and the `current_auth` context reader.
//!
//! The context reader reads from `FullstackContext`, not directly from the live
//! axum `Request<Body>::extensions()`. The bridge is one-way: at the start
//! of a server-function request, dioxus-server splits the request into
//! `Parts`, constructs a `FullstackContext::new(parts)`, then runs the
//! handler future inside `ctx.scope(...)` so the task-local
//! `FULLSTACK_CONTEXT` is set. `ctx.extension::<T>()` then reads from
//! `parts.extensions`, which is exactly where `ClerkAuthLayer` deposits
//! the auth.
//!
//! The plan's original test design wires a plain axum handler directly,
//! which would never see a `FullstackContext` (no scope set), so
//! `current_auth()` would return `Err(InvalidConfig(...))`. We
//! reproduce dioxus-server's bridge here with a tiny tower middleware
//! that wraps the inner handler call in `FullstackContext::new(parts)
//! .scope(...)`. This faithfully exercises the production code path
//! without pulling in the full dioxus-server runtime.

#[path = "support/auth.rs"]
mod auth;

use auth::sample_auth;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::{Router, routing::get};
use dioxus_clerk::core::{ClerkError, InvalidTokenReason};
use dioxus_clerk::server::{
    ClerkAuth, VerificationOutcome, current_auth, current_auth_opt, current_outcome,
};
use dioxus_fullstack_core::FullstackContext;
use tower::ServiceExt;

/// Mirror of what dioxus-server does for every server-function request:
/// split the incoming request into `Parts`, build a `FullstackContext`,
/// and run the rest of the call inside `ctx.scope(...)` so the
/// task-local `FULLSTACK_CONTEXT` is set for the handler body.
async fn fullstack_scope(req: Request<Body>, next: Next) -> Response {
    let (parts, body) = req.into_parts();
    let ctx = FullstackContext::new(parts.clone());
    let req = Request::from_parts(parts, body);
    ctx.scope(next.run(req)).await
}

async fn insert_valid_outcome(mut req: Request<Body>, next: Next) -> Response {
    req.extensions_mut()
        .insert(VerificationOutcome::Valid(sample_auth()));
    next.run(req).await
}

#[tokio::test]
async fn clerk_auth_extracts_valid_outcome_for_axum_handlers() {
    async fn handler(auth: ClerkAuth) -> impl IntoResponse {
        auth.user_id
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(insert_valid_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"user_2abc");
}

#[tokio::test]
async fn clerk_auth_rejects_missing_outcome_for_axum_handlers() {
    async fn handler(_auth: ClerkAuth) -> impl IntoResponse {
        "authed"
    }

    let app: Router = Router::new().route("/", get(handler));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"unauthenticated");
}

#[tokio::test]
async fn optional_clerk_auth_extracts_none_for_public_axum_handlers() {
    async fn handler(auth: Option<ClerkAuth>) -> impl IntoResponse {
        auth.map(|auth| auth.user_id)
            .unwrap_or_else(|| "none".into())
    }

    let app: Router = Router::new().route("/", get(handler));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"none");
}

#[tokio::test]
async fn optional_clerk_auth_extracts_valid_outcome_for_axum_handlers() {
    async fn handler(auth: Option<ClerkAuth>) -> impl IntoResponse {
        auth.map(|auth| auth.user_id)
            .unwrap_or_else(|| "none".into())
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(insert_valid_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"user_2abc");
}

#[tokio::test]
async fn optional_clerk_auth_extracts_none_for_invalid_outcome() {
    async fn insert_invalid_outcome(mut req: Request<Body>, next: Next) -> Response {
        req.extensions_mut()
            .insert(VerificationOutcome::Invalid(InvalidTokenReason::Other));
        next.run(req).await
    }

    async fn handler(auth: Option<ClerkAuth>) -> impl IntoResponse {
        auth.map(|auth| auth.user_id)
            .unwrap_or_else(|| "none".into())
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(insert_invalid_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"none");
}

#[tokio::test]
async fn optional_clerk_auth_rejects_unavailable_outcome() {
    async fn insert_unavailable_outcome(mut req: Request<Body>, next: Next) -> Response {
        req.extensions_mut()
            .insert(VerificationOutcome::Unavailable);
        next.run(req).await
    }

    async fn handler(_auth: Option<ClerkAuth>) -> impl IntoResponse {
        "ok"
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(insert_unavailable_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"Clerk verification unavailable");
}

#[tokio::test]
async fn current_auth_reads_valid_outcome_set_by_layer() {
    async fn handler() -> impl IntoResponse {
        let auth = current_auth().expect("auth extension should be present");
        auth.user_id
    }

    // Layers run on the request in the OPPOSITE order they're declared
    // (axum wraps services bottom-up). We want the auth-inserter layer
    // to run FIRST on the request (so the extension is present when
    // `FullstackContext::new(parts)` snapshots them), then the scope
    // layer to wrap the handler call. That means the auth-inserter is
    // declared LAST.
    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(fullstack_scope))
        .layer(axum::middleware::from_fn(insert_valid_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"user_2abc");
}

#[tokio::test]
async fn crate_root_current_auth_reads_valid_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Valid(sample_auth()));

    let auth = cx.scope(async { current_auth() }).await.unwrap();

    assert_eq!(auth.user_id, "user_2abc");
}

#[tokio::test]
async fn current_auth_returns_unauthenticated_when_no_extension() {
    async fn handler() -> impl IntoResponse {
        match current_auth() {
            Ok(_) => (StatusCode::OK, "authed"),
            Err(ClerkError::Unauthenticated) => (StatusCode::OK, "unauthenticated"),
            Err(other) => panic!("unexpected error: {other:?}"),
        }
    }

    // FullstackContext IS set up (server function was invoked), but no
    // ClerkAuth extension was inserted by any upstream layer.
    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(fullstack_scope));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"unauthenticated");
}

#[tokio::test]
async fn current_auth_opt_returns_some_when_valid_outcome_present() {
    async fn handler() -> impl IntoResponse {
        let opt = current_auth_opt().expect("FullstackContext should be present in scope");
        match opt {
            Some(auth) => auth.user_id,
            None => "none".to_string(),
        }
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(fullstack_scope))
        .layer(axum::middleware::from_fn(insert_valid_outcome));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"user_2abc");
}

#[tokio::test]
async fn context_reader_returns_current_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Valid(sample_auth()));

    let outcome = cx.scope(async { current_outcome() }).await;

    assert!(
        matches!(outcome, Some(VerificationOutcome::Valid(auth)) if auth.user_id == "user_2abc")
    );
}

#[tokio::test]
async fn context_reader_current_auth_opt_returns_none_for_missing_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Missing);

    let auth = cx.scope(async { current_auth_opt() }).await.unwrap();

    assert!(auth.is_none());
}

#[tokio::test]
async fn context_reader_current_auth_opt_errors_for_unavailable_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Unavailable);

    let err = cx.scope(async { current_auth_opt() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::JwksUnavailable(_)));
}

#[tokio::test]
async fn context_reader_current_auth_returns_jwks_unavailable_for_unavailable_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Unavailable);

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::JwksUnavailable(_)));
}

#[tokio::test]
async fn context_reader_current_auth_returns_token_expired_for_expired_token() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Invalid(InvalidTokenReason::Expired));

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::TokenExpired));
}

#[tokio::test]
async fn context_reader_current_auth_returns_unauthenticated_for_missing_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Missing);

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::Unauthenticated));
}

#[tokio::test]
async fn current_auth_opt_returns_none_when_extension_absent() {
    async fn handler() -> impl IntoResponse {
        let opt = current_auth_opt().expect("FullstackContext should be present in scope");
        match opt {
            Some(_) => "some",
            None => "none",
        }
    }

    let app: Router = Router::new()
        .route("/", get(handler))
        .layer(axum::middleware::from_fn(fullstack_scope));

    let resp = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
    assert_eq!(&body[..], b"none");
}

#[tokio::test]
async fn current_auth_errors_outside_fullstack_scope() {
    // No FullstackContext on the task-local: this is the "called outside
    // a server function" case. `current_auth` should return
    // `Err(NoServerContext)` and `current_auth_opt` likewise.
    let from_err = current_auth().expect_err("must error");
    assert!(
        matches!(from_err, ClerkError::NoServerContext),
        "expected NoServerContext, got {from_err:?}"
    );

    let opt_err = current_auth_opt().expect_err("must error");
    assert!(
        matches!(opt_err, ClerkError::NoServerContext),
        "expected NoServerContext, got {opt_err:?}"
    );
}

#[tokio::test]
async fn current_auth_opt_returns_none_for_invalid_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Invalid(InvalidTokenReason::Other));

    let auth = cx.scope(async { current_auth_opt() }).await.unwrap();

    assert!(auth.is_none());
}

#[tokio::test]
async fn current_auth_returns_unauthenticated_for_invalid_outcome() {
    let cx = server_context_with_extensions();
    cx.parts_mut()
        .extensions
        .insert(VerificationOutcome::Invalid(InvalidTokenReason::Other));

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::Unauthenticated));
}

#[tokio::test]
async fn current_auth_opt_returns_none_when_invalid_outcome_and_auth_extension_exist() {
    let cx = server_context_with_extensions();
    {
        let mut parts = cx.parts_mut();
        parts
            .extensions
            .insert(VerificationOutcome::Invalid(InvalidTokenReason::Other));
        parts.extensions.insert(sample_auth());
    }

    let auth = cx.scope(async { current_auth_opt() }).await.unwrap();

    assert!(auth.is_none());
}

#[tokio::test]
async fn current_auth_returns_unauthenticated_when_invalid_outcome_and_auth_extension_exist() {
    let cx = server_context_with_extensions();
    {
        let mut parts = cx.parts_mut();
        parts
            .extensions
            .insert(VerificationOutcome::Invalid(InvalidTokenReason::Other));
        parts.extensions.insert(sample_auth());
    }

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::Unauthenticated));
}

#[tokio::test]
async fn current_auth_opt_returns_none_when_only_stale_auth_extension_exists() {
    let cx = server_context_with_extensions();
    cx.parts_mut().extensions.insert(sample_auth());

    let auth = cx.scope(async { current_auth_opt() }).await.unwrap();

    assert!(auth.is_none());
}

#[tokio::test]
async fn current_auth_returns_unauthenticated_when_only_stale_auth_extension_exists() {
    let cx = server_context_with_extensions();
    cx.parts_mut().extensions.insert(sample_auth());

    let err = cx.scope(async { current_auth() }).await.unwrap_err();

    assert!(matches!(err, ClerkError::Unauthenticated));
}

fn server_context_with_extensions() -> FullstackContext {
    let req = Request::builder().uri("/").body(Body::empty()).unwrap();
    let (parts, _body) = req.into_parts();
    FullstackContext::new(parts)
}