barycenter/tests/helpers/mock_webauthn.rs
Till Wegmueller d7bdd51164
WIP Passkey implementation. Needs fixing storage.rs and more tests
Signed-off-by: Till Wegmueller <toasterson@gmail.com>
2025-12-07 13:18:22 +01:00

87 lines
3.3 KiB
Rust

use webauthn_rs::prelude::*;
/// Mock WebAuthn credential for testing
///
/// This creates mock WebAuthn credentials and responses for testing
/// passkey registration and authentication flows without requiring a browser.
pub struct MockWebAuthnCredential {
pub credential_id: Vec<u8>,
pub counter: u32,
pub backup_state: bool,
pub passkey: Passkey,
}
impl MockWebAuthnCredential {
/// Create a new hardware-bound passkey (e.g., YubiKey, TouchID)
pub fn new_hardware_key(user_id: Uuid, username: &str) -> Self {
// Use webauthn-rs to generate a real passkey for testing
// This will create a valid passkey structure
let credential_id = uuid::Uuid::new_v4().as_bytes().to_vec();
// Create a minimal passkey structure for testing
// Note: In real tests, you'd use webauthn-rs test utilities
// or the webauthn library's test mode
Self {
credential_id: credential_id.clone(),
counter: 0,
backup_state: false,
passkey: create_test_passkey(user_id, username, &credential_id, false),
}
}
/// Create a new cloud-synced passkey (e.g., iCloud Keychain, password manager)
pub fn new_cloud_synced(user_id: Uuid, username: &str) -> Self {
let credential_id = uuid::Uuid::new_v4().as_bytes().to_vec();
Self {
credential_id: credential_id.clone(),
counter: 0,
backup_state: true,
passkey: create_test_passkey(user_id, username, &credential_id, true),
}
}
/// Increment counter (for clone detection testing)
pub fn increment_counter(&mut self) {
self.counter += 1;
}
}
/// Helper function to create a test passkey
///
/// This creates a minimal but valid passkey structure for testing.
/// Note: For full WebAuthn testing, you should use webauthn-rs's test utilities
/// or mock the WebAuthn responses at the HTTP level.
fn create_test_passkey(
user_id: Uuid,
username: &str,
credential_id: &[u8],
backup_state: bool,
) -> Passkey {
// Create a test passkey using webauthn-rs's test utilities
// This is a simplified version - in production tests you'd want
// to use the full webauthn-rs test framework or mock HTTP responses
use webauthn_rs::prelude::*;
// For now, we'll use a placeholder
// In actual implementation, you'd use webauthn-rs test utilities
// or mock the entire flow at the HTTP level
// This is a marker to indicate where full WebAuthn mocking would go
unimplemented!("Full WebAuthn mocking should be implemented using webauthn-rs test utilities or HTTP-level mocking")
}
// Note: For comprehensive WebAuthn testing, consider these approaches:
//
// 1. HTTP-level mocking: Mock the entire WebAuthn flow by creating valid
// JSON responses that match the WebAuthn spec, and test the HTTP endpoints
//
// 2. Use webauthn-rs test mode: The webauthn-rs library has test utilities
// for creating valid attestation and assertion responses
//
// 3. Integration tests: Test the WebAuthn endpoints with pre-recorded valid
// WebAuthn responses from real authenticators
//
// For the scope of this implementation, we'll focus on approach #1 (HTTP-level)
// and approach #3 (pre-recorded responses) in the integration tests.