46 lines
2.1 KiB
Rust
46 lines
2.1 KiB
Rust
use std::convert::Infallible;
|
|
use warp::Reply;
|
|
use webauthn_rs::prelude::{RegisterPublicKeyCredential, Uuid};
|
|
|
|
use crate::{models::{AppState, User}, templates};
|
|
|
|
pub async fn landing_page (app_state: AppState<'_>) -> Result<impl Reply, Infallible> {
|
|
let content: String = templates::landing_page(app_state).unwrap();
|
|
Ok(warp::reply::html(content))
|
|
}
|
|
|
|
pub async fn start_webauthn_registration(user: User, app_state: AppState<'_>) -> Result<impl warp::Reply, Infallible> {
|
|
let (creation_challenge_response, passkey_registration) = app_state.webauthn.start_passkey_registration(user.uuid, &user.user_name, &user.display_name, None).unwrap();
|
|
let uuid_str = user.uuid.to_string();
|
|
{
|
|
let mut session = app_state.session.user_registrations.write().await;
|
|
session.insert(user.clone(), passkey_registration);
|
|
}
|
|
{
|
|
let mut uuid_db = app_state.db.user_uuid_object.write().await;
|
|
uuid_db.insert(user.uuid, user);
|
|
}
|
|
let json_reply = warp::reply::json(&creation_challenge_response);
|
|
Ok(warp::reply::with_header(json_reply, "Set-Cookie", format!("uuid={};SameSite=Strict", &uuid_str)))
|
|
}
|
|
|
|
pub async fn finish_webauthn_registration(register: RegisterPublicKeyCredential, app_state: AppState<'_>, uuid: Uuid) -> Result<impl warp::Reply, Infallible> {
|
|
let registration_result = {
|
|
let users = app_state.db.user_uuid_object.read().await;
|
|
let user = users.get(&uuid).unwrap();
|
|
let session = app_state.session.user_registrations.read().await;
|
|
let passkey_registration = session.get(&user).unwrap();
|
|
app_state.webauthn.finish_passkey_registration(®ister, passkey_registration)
|
|
};
|
|
let reply = {
|
|
let mut user_keys = app_state.db.user_keys.write().await;
|
|
registration_result.map_or(
|
|
warp::reply::with_status("Challenge failed, cannot register key", warp::http::StatusCode::UNAUTHORIZED),
|
|
|passkey| {
|
|
user_keys.insert(uuid, passkey);
|
|
warp::reply::with_status("ok",warp::http::StatusCode::OK)
|
|
})
|
|
};
|
|
Ok(reply)
|
|
}
|