caden 0.7.0

Server-side WebAuthn/Passkey library implementing the W3C WebAuthn Level 2 specification
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
//! Minimal Axum HTTP server demonstrating real-world WebAuthn integration.
//!
//! Run with:
//! ```bash
//! cargo run --example server
//! ```
//!
//! The server starts on `http://localhost:3000`. All state is in-memory — restart
//! clears all registered credentials. This is a demo; a production server would
//! persist credentials to a database.

use std::collections::HashMap;
use std::sync::Arc;

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{routing::get, routing::post, Json, Router};
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;

use webauthn::{
    AuthenticatorAssertionResponse, AuthenticatorAttestationResponse, Challenge, Credential,
    RelyingParty,
};

// ─── App state ────────────────────────────────────────────────────────────────

struct AppState {
    /// pending challenges: session_id → Challenge
    pending_challenges: Mutex<HashMap<String, Challenge>>,
    /// stored credentials: credential_id (hex) → Credential
    credentials: Mutex<HashMap<String, Credential>>,
    relying_party: RelyingParty,
}

type SharedState = Arc<AppState>;

// ─── Request / Response types ─────────────────────────────────────────────────

#[derive(Deserialize)]
struct RegisterBeginRequest {
    user_id: String,
    username: String,
}

#[derive(Serialize)]
struct PubKeyCredParam {
    r#type: &'static str,
    alg: i64,
}

#[derive(Serialize)]
struct RpInfo {
    id: &'static str,
    name: &'static str,
}

#[derive(Serialize)]
struct UserInfo {
    id: String,
    name: String,
}

#[derive(Serialize)]
struct RegisterBeginResponse {
    session_id: String,
    challenge: String,
    rp: RpInfo,
    user: UserInfo,
    #[serde(rename = "pubKeyCredParams")]
    pub_key_cred_params: Vec<PubKeyCredParam>,
}

#[derive(Deserialize)]
struct RegisterCompleteRequest {
    session_id: String,
    client_data_json: String,
    attestation_object: String,
}

#[derive(Serialize)]
struct RegisterCompleteResponse {
    credential_id: String,
    status: &'static str,
}

#[derive(Deserialize)]
struct AuthBeginRequest {
    credential_id: String,
}

#[derive(Serialize)]
struct AllowCredential {
    r#type: &'static str,
    id: String,
}

#[derive(Serialize)]
struct AuthBeginResponse {
    session_id: String,
    challenge: String,
    #[serde(rename = "allowCredentials")]
    allow_credentials: Vec<AllowCredential>,
}

#[derive(Deserialize)]
struct AuthCompleteRequest {
    session_id: String,
    credential_id: String,
    client_data_json: String,
    authenticator_data: String,
    signature: String,
}

#[derive(Serialize)]
struct AuthCompleteResponse {
    status: &'static str,
    new_sign_count: u32,
}

#[derive(Serialize)]
struct ErrorResponse {
    error: String,
    code: &'static str,
}

// ─── Error helpers ────────────────────────────────────────────────────────────

fn client_error(message: impl Into<String>, code: &'static str) -> impl IntoResponse {
    (
        StatusCode::BAD_REQUEST,
        Json(ErrorResponse {
            error: message.into(),
            code,
        }),
    )
}

fn server_error(message: impl Into<String>) -> impl IntoResponse {
    (
        StatusCode::INTERNAL_SERVER_ERROR,
        Json(ErrorResponse {
            error: message.into(),
            code: "INTERNAL_ERROR",
        }),
    )
}

fn decode_b64url(
    value: &str,
    field: &'static str,
) -> Result<Vec<u8>, (StatusCode, Json<ErrorResponse>)> {
    URL_SAFE_NO_PAD.decode(value).map_err(|e| {
        (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("{field}: {e}"),
                code: "BASE64_DECODE_ERROR",
            }),
        )
    })
}

fn to_hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// Generate a random session ID as 16 random bytes encoded as 32 hex chars.
fn new_session_id() -> String {
    let bytes = webauthn::random_bytes(16).expect("RNG failure");
    to_hex(&bytes)
}

// ─── Endpoints ────────────────────────────────────────────────────────────────

/// GET /health — liveness check.
async fn health() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "status": "ok",
        "version": env!("CARGO_PKG_VERSION")
    }))
}

/// POST /register/begin — issue a registration challenge.
async fn register_begin(
    State(state): State<SharedState>,
    Json(req): Json<RegisterBeginRequest>,
) -> impl IntoResponse {
    let challenge = match Challenge::new() {
        Ok(c) => c,
        Err(e) => return server_error(format!("challenge generation failed: {e}")).into_response(),
    };

    let session_id = new_session_id();
    let challenge_b64 = URL_SAFE_NO_PAD.encode(&challenge.bytes);

    state
        .pending_challenges
        .lock()
        .await
        .insert(session_id.clone(), challenge);

    let response = RegisterBeginResponse {
        session_id,
        challenge: challenge_b64,
        rp: RpInfo {
            id: "localhost",
            name: "Caden Demo",
        },
        user: UserInfo {
            id: req.user_id,
            name: req.username,
        },
        pub_key_cred_params: vec![
            PubKeyCredParam {
                r#type: "public-key",
                alg: -7,
            },
            PubKeyCredParam {
                r#type: "public-key",
                alg: -8,
            },
            PubKeyCredParam {
                r#type: "public-key",
                alg: -35,
            },
            PubKeyCredParam {
                r#type: "public-key",
                alg: -257,
            },
        ],
    };

    (StatusCode::OK, Json(response)).into_response()
}

/// POST /register/complete — verify registration and store the credential.
async fn register_complete(
    State(state): State<SharedState>,
    Json(req): Json<RegisterCompleteRequest>,
) -> impl IntoResponse {
    let challenge = {
        let mut map = state.pending_challenges.lock().await;
        match map.remove(&req.session_id) {
            Some(c) => c,
            None => {
                return client_error("session not found or expired", "SESSION_NOT_FOUND")
                    .into_response()
            }
        }
    };

    let client_data_json = match decode_b64url(&req.client_data_json, "client_data_json") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };
    let attestation_object = match decode_b64url(&req.attestation_object, "attestation_object") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };

    let response = AuthenticatorAttestationResponse {
        client_data_json,
        attestation_object,
    };

    // Use the session_id bytes as user_id for this demo.
    let user_id = req.session_id.as_bytes().to_vec();

    let result = match state
        .relying_party
        .verify_registration(&challenge, &response, &user_id)
    {
        Ok(r) => r,
        Err(e) => {
            return client_error(format!("registration failed: {e}"), "VERIFICATION_FAILED")
                .into_response()
        }
    };

    let credential_id_hex = to_hex(&result.credential.id);
    let credential_id_b64 = URL_SAFE_NO_PAD.encode(&result.credential.id);

    state
        .credentials
        .lock()
        .await
        .insert(credential_id_hex, result.credential);

    (
        StatusCode::OK,
        Json(RegisterCompleteResponse {
            credential_id: credential_id_b64,
            status: "ok",
        }),
    )
        .into_response()
}

/// POST /authenticate/begin — issue an authentication challenge.
async fn authenticate_begin(
    State(state): State<SharedState>,
    Json(req): Json<AuthBeginRequest>,
) -> impl IntoResponse {
    let cred_id_bytes = match decode_b64url(&req.credential_id, "credential_id") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };
    let cred_id_hex = to_hex(&cred_id_bytes);

    {
        let creds = state.credentials.lock().await;
        if !creds.contains_key(&cred_id_hex) {
            return client_error("credential not found", "CREDENTIAL_NOT_FOUND").into_response();
        }
    }

    let challenge = match Challenge::new() {
        Ok(c) => c,
        Err(e) => return server_error(format!("challenge generation failed: {e}")).into_response(),
    };

    let session_id = new_session_id();
    let challenge_b64 = URL_SAFE_NO_PAD.encode(&challenge.bytes);

    state
        .pending_challenges
        .lock()
        .await
        .insert(session_id.clone(), challenge);

    let response = AuthBeginResponse {
        session_id,
        challenge: challenge_b64,
        allow_credentials: vec![AllowCredential {
            r#type: "public-key",
            id: req.credential_id,
        }],
    };

    (StatusCode::OK, Json(response)).into_response()
}

/// POST /authenticate/complete — verify authentication and update sign count.
async fn authenticate_complete(
    State(state): State<SharedState>,
    Json(req): Json<AuthCompleteRequest>,
) -> impl IntoResponse {
    let challenge = {
        let mut map = state.pending_challenges.lock().await;
        match map.remove(&req.session_id) {
            Some(c) => c,
            None => {
                return client_error("session not found or expired", "SESSION_NOT_FOUND")
                    .into_response()
            }
        }
    };

    let cred_id_bytes = match decode_b64url(&req.credential_id, "credential_id") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };
    let cred_id_hex = to_hex(&cred_id_bytes);

    let stored_credential = {
        let creds = state.credentials.lock().await;
        match creds.get(&cred_id_hex).cloned() {
            Some(c) => c,
            None => {
                return client_error("credential not found", "CREDENTIAL_NOT_FOUND").into_response()
            }
        }
    };

    let client_data_json = match decode_b64url(&req.client_data_json, "client_data_json") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };
    let authenticator_data = match decode_b64url(&req.authenticator_data, "authenticator_data") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };
    let signature = match decode_b64url(&req.signature, "signature") {
        Ok(b) => b,
        Err(e) => return e.into_response(),
    };

    let assertion = AuthenticatorAssertionResponse {
        client_data_json,
        authenticator_data,
        signature,
        user_handle: None,
    };

    let result =
        match state
            .relying_party
            .verify_authentication(&stored_credential, &challenge, &assertion)
        {
            Ok(r) => r,
            Err(e) => {
                return client_error(format!("authentication failed: {e}"), "VERIFICATION_FAILED")
                    .into_response()
            }
        };

    // Update the stored sign count.
    {
        let mut creds = state.credentials.lock().await;
        if let Some(cred) = creds.get_mut(&cred_id_hex) {
            cred.sign_count = result.new_sign_count;
        }
    }

    (
        StatusCode::OK,
        Json(AuthCompleteResponse {
            status: "ok",
            new_sign_count: result.new_sign_count,
        }),
    )
        .into_response()
}

// ─── Main ─────────────────────────────────────────────────────────────────────

#[tokio::main]
async fn main() {
    let state = Arc::new(AppState {
        pending_challenges: Mutex::new(HashMap::new()),
        credentials: Mutex::new(HashMap::new()),
        relying_party: RelyingParty::new("localhost", "http://localhost:3000", "Caden Demo"),
    });

    let app = Router::new()
        .route("/health", get(health))
        .route("/register/begin", post(register_begin))
        .route("/register/complete", post(register_complete))
        .route("/authenticate/begin", post(authenticate_begin))
        .route("/authenticate/complete", post(authenticate_complete))
        .with_state(state);

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
        .await
        .expect("failed to bind to port 3000");

    println!("Caden demo server running on http://localhost:3000");
    println!();
    println!("Endpoints:");
    println!("  GET  /health");
    println!("  POST /register/begin");
    println!("  POST /register/complete");
    println!("  POST /authenticate/begin");
    println!("  POST /authenticate/complete");

    axum::serve(listener, app).await.expect("server failed");
}