Skip to main content

passkey_server/
lib.rs

1//! # Passkey
2//!
3//! A generic Rust implementation of the WebAuthn (Passkey) protocol, designed to be
4//! storage-agnostic and easy to integrate into any backend.
5//!
6//! ## Features
7//!
8//! - **Generic Storage**: Implement the [`PasskeyStore`] trait to use your own database.
9//! - **WASM Friendly**: Works well in WASM environments like Cloudflare Workers.
10//! - **Simple Protocol Handlers**: Provides high-level functions for the standard WebAuthn registration and login flows.
11//!
12//! ## Example usage
13//!
14//! ```rust,no_run
15//! use passkey_server::{PasskeyConfig, PasskeyStore, start_registration, types::{StoredPasskey, PasskeyState}, error::Result};
16//! use async_trait::async_trait;
17//!
18//! // Example usage requires implementing the PasskeyStore trait
19//! struct MyDatabase;
20//!
21//! #[async_trait(?Send)]
22//! impl PasskeyStore for MyDatabase {
23//!     async fn create_passkey(&self, _: String, _: &str, _: &str, _: &str, _: i64, _: i64) -> Result<()> { Ok(()) }
24//!     async fn get_passkey(&self, _: &str) -> Result<Option<StoredPasskey>> { Ok(None) }
25//!     async fn list_passkeys(&self, _: String) -> Result<Vec<StoredPasskey>> { Ok(vec![]) }
26//!     async fn delete_passkey(&self, _: String, _: &str) -> Result<()> { Ok(()) }
27//!     async fn update_passkey_counter(&self, _: &str, _: i64, _: i64) -> Result<()> { Ok(()) }
28//!     async fn update_passkey_name(&self, _: &str, _: &str) -> Result<()> { Ok(()) }
29//!     async fn save_state(&self, _: &str, _: &str, _: i64) -> Result<()> { Ok(()) }
30//!     async fn get_state(&self, _: &str) -> Result<Option<PasskeyState>> { Ok(None) }
31//!     async fn delete_state(&self, _: &str) -> Result<()> { Ok(()) }
32//! }
33//!
34//! #[tokio::main]
35//! async fn main() {
36//!     let config = PasskeyConfig {
37//!         rp_id: "example.com".to_string(),
38//!         rp_name: "My App".to_string(),
39//!         origin: "https://example.com".to_string(),
40//!         state_ttl: 300,
41//!     };
42//!
43//!     let store = MyDatabase;
44//!     let user_id = "123";
45//!     let now_ms = 1708358400000;
46//!
47//!     // 1. Start registration
48//!     let options = start_registration(
49//!         &store,
50//!         user_id,
51//!         "alice",
52//!         "Alice Doe",
53//!         &config,
54//!         now_ms
55//!     ).await.unwrap();
56//!
57//!     println!("Send these options to the frontend: {:?}", options);
58//! }
59//! ```
60
61pub mod error;
62pub mod protocol;
63pub mod store;
64pub mod types;
65
66pub use error::{PasskeyError, Result};
67pub use protocol::{finish_login, finish_registration, start_login, start_registration};
68pub use store::PasskeyStore;
69pub use types::PasskeyConfig;
70
71#[cfg(test)]
72mod tests;