use passki::{
AttestationConveyancePreference, AuthenticationChallenge, AuthenticationCredential,
AuthenticationState, ClientData, Passki, RegistrationChallenge, RegistrationCredential,
RegistrationState, ResidentKeyRequirement, StoredPasskey, UserVerificationRequirement,
};
use poem::{
EndpointExt, Route, Server, get, handler,
http::StatusCode,
listener::TcpListener,
middleware::{Cors, Tracing},
post,
web::{Data, Json},
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
type AppResult<T> = poem::Result<Json<T>>;
fn err(msg: impl std::fmt::Display) -> poem::Error {
poem::Error::from_string(msg.to_string(), StatusCode::BAD_REQUEST)
}
#[derive(Clone, Default)]
struct Store {
users: Arc<Mutex<HashMap<String, User>>>,
pending_registrations: Arc<Mutex<HashMap<String, RegistrationState>>>,
pending_authentications: Arc<Mutex<HashMap<String, AuthenticationState>>>,
}
#[derive(Clone)]
#[allow(unused)]
struct User {
id: Uuid,
username: String,
display_name: String,
passkeys: Vec<StoredPasskey>,
}
#[derive(Deserialize)]
struct RegisterStartRequest {
username: String,
}
#[derive(Deserialize)]
struct RegisterFinishRequest {
credential_id: String,
public_key: String,
client_data_json: String,
}
#[derive(Deserialize, Default)]
struct AuthStartRequest {
#[serde(default)]
username: Option<String>,
}
#[derive(Deserialize)]
struct AuthFinishRequest {
credential_id: String,
authenticator_data: String,
client_data_json: String,
signature: String,
}
#[derive(Serialize)]
struct ApiResponse {
success: bool,
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
username: Option<String>,
}
#[handler]
async fn index() -> poem::Result<poem::web::Html<String>> {
let html = fs::read_to_string("examples/index.html")
.map_err(|e| poem::Error::from_string(e.to_string(), StatusCode::INTERNAL_SERVER_ERROR))?;
Ok(poem::web::Html(html))
}
#[handler]
async fn register_start(
Json(req): Json<RegisterStartRequest>,
passki: Data<&Arc<Passki>>,
store: Data<&Store>,
) -> AppResult<RegistrationChallenge> {
let user_id = Uuid::new_v4().as_bytes().to_vec();
let existing = store.users.lock().unwrap()
.get(&req.username).map(|u| u.passkeys.clone());
let (challenge, state) = passki.start_passkey_registration(
&user_id,
&req.username, &req.username, 60000, AttestationConveyancePreference::None, ResidentKeyRequirement::Preferred, UserVerificationRequirement::Preferred, existing.as_deref(), ).map_err(err)?;
store.pending_registrations.lock().unwrap().insert(challenge.challenge.clone(), state);
Ok(Json(challenge))
}
#[handler]
async fn register_finish(
Json(req): Json<RegisterFinishRequest>,
passki: Data<&Arc<Passki>>,
store: Data<&Store>,
) -> AppResult<ApiResponse> {
let client_data = ClientData::from_base64(&req.client_data_json).map_err(err)?;
let state = store.pending_registrations.lock().unwrap()
.remove(&client_data.challenge)
.ok_or_else(|| err("No pending registration"))?;
let credential = RegistrationCredential {
credential_id: req.credential_id,
public_key: req.public_key,
client_data_json: req.client_data_json,
};
let passkey = passki.finish_passkey_registration(&credential, &state).map_err(err)?;
let user_id_bytes = Passki::base64_decode(&state.user.id).map_err(err)?;
let user_id = Uuid::from_slice(&user_id_bytes).map_err(err)?;
let mut users = store.users.lock().unwrap();
users
.entry(state.user.name.clone())
.and_modify(|user| user.passkeys.push(passkey.clone()))
.or_insert(User {
id: user_id,
username: state.user.name,
display_name: state.user.display_name,
passkeys: vec![passkey],
});
Ok(Json(ApiResponse { success: true, message: "Registration successful".into(), username: None }))
}
#[handler]
async fn auth_start(
Json(req): Json<AuthStartRequest>,
passki: Data<&Arc<Passki>>,
store: Data<&Store>,
) -> AppResult<AuthenticationChallenge> {
let passkeys = if let Some(ref username) = req.username {
let users = store.users.lock().unwrap();
let user = users.get(username).ok_or_else(|| err("User not found"))?;
user.passkeys.clone()
} else {
vec![]
};
let (challenge, state) = passki.start_passkey_authentication(
&passkeys,
60000, UserVerificationRequirement::Preferred, );
store.pending_authentications.lock().unwrap().insert(challenge.challenge.clone(), state);
Ok(Json(challenge))
}
#[handler]
async fn auth_finish(
Json(req): Json<AuthFinishRequest>,
passki: Data<&Arc<Passki>>,
store: Data<&Store>,
) -> AppResult<ApiResponse> {
let client_data = ClientData::from_base64(&req.client_data_json).map_err(err)?;
let state = store.pending_authentications.lock().unwrap()
.remove(&client_data.challenge)
.ok_or_else(|| err("No pending authentication"))?;
let credential_id = Passki::base64_decode(&req.credential_id).map_err(err)?;
let mut users = store.users.lock().unwrap();
let (username, passkey) = users.iter_mut()
.find_map(|(name, user)| {
user.passkeys.iter_mut()
.find(|pk| pk.credential_id == credential_id)
.map(|pk| (name.clone(), pk))
})
.ok_or_else(|| err("Unknown credential"))?;
let credential = AuthenticationCredential {
credential_id: req.credential_id,
authenticator_data: req.authenticator_data,
client_data_json: req.client_data_json,
signature: req.signature,
};
let result = passki.finish_passkey_authentication(&credential, &state, passkey).map_err(err)?;
passkey.counter = result.counter;
Ok(Json(ApiResponse {
success: true,
message: format!("Welcome back, {}!", username),
username: Some(username),
}))
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
tracing_subscriber::fmt().with_target(false).init();
let passki = Arc::new(Passki::new(
"localhost",
"http://localhost:3000",
"Passkeys Demo",
));
let app = Route::new()
.at("/", get(index))
.at("/register/start", post(register_start))
.at("/register/finish", post(register_finish))
.at("/auth/start", post(auth_start))
.at("/auth/finish", post(auth_finish))
.with(Cors::new())
.with(Tracing)
.data(passki)
.data(Store::default());
println!("Server starting on http://localhost:3000");
Server::new(TcpListener::bind("0.0.0.0:3000"))
.run(app)
.await
}