From d1d935833543cd301a2d9ce7a15311363ac85851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac=20Jacqu=C3=A9?= Date: Sun, 8 Jan 2023 17:59:58 +0100 Subject: [PATCH] xxx 2 --- Cargo.lock | 67 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/filters/mod.rs | 19 +++++++++-- src/handlers/mod.rs | 11 +++++-- src/main.rs | 6 ++-- src/models/mod.rs | 9 ++++-- src/templates/landing.hbs | 9 ++++++ src/templates/mod.rs | 15 +++++++++ 8 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 src/templates/landing.hbs create mode 100644 src/templates/mod.rs diff --git a/Cargo.lock b/Cargo.lock index bef03ef..8a7d012 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -375,6 +375,20 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" +[[package]] +name = "handlebars" +version = "4.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "035ef95d03713f2c347a72547b7cd38cbc9af7cd51e6099fb62d586d4a6dee3a" +dependencies = [ + "log", + "pest", + "pest_derive", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -651,6 +665,9 @@ name = "nix-cache-bucket-gc" version = "0.1.0" dependencies = [ "clap", + "handlebars", + "serde", + "serde_json", "tokio", "url", "warp", @@ -796,6 +813,50 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +[[package]] +name = "pest" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f6e86fb9e7026527a0d46bc308b841d73170ef8f443e1807f6ef88526a816d4" +dependencies = [ + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96504449aa860c8dcde14f9fba5c58dc6658688ca1fe363589d6327b8662c603" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "798e0220d1111ae63d66cb66a5dcb3fc2d986d520b98e49e1852bfdb11d7c5e7" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "984298b75898e30a843e278a9f2452c31e349a073a0ce6fd950a12a74464e065" +dependencies = [ + "once_cell", + "pest", + "sha1", +] + [[package]] name = "pin-project" version = "1.0.12" @@ -1362,6 +1423,12 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "ucd-trie" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" + [[package]] name = "unicase" version = "2.6.0" diff --git a/Cargo.toml b/Cargo.toml index 24bb929..48eb079 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,6 @@ tokio = { version = "1", features = ["full"] } url = "*" warp = "0.3" webauthn-rs = "0.4.8" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +handlebars = "4.3.6" diff --git a/src/filters/mod.rs b/src/filters/mod.rs index ba03b77..ae7c1c8 100644 --- a/src/filters/mod.rs +++ b/src/filters/mod.rs @@ -1,15 +1,30 @@ use warp::Filter; use crate::handlers; +use crate::models; -pub fn start_webauthn_registration( ) -> impl Filter + Clone { + +pub fn all(state: models::AppState) -> impl Filter + Clone { + landing(state.clone()) + .or(start_webauthn_registration(state.clone())) + .or(start_webauthn_registration(state.clone())) +} + +pub fn landing(state: models::AppState) -> impl Filter + Clone { + warp::path::end() + .and(warp::any().map(move || state.clone())) + .and_then(handlers::landing_page) +} + +pub fn start_webauthn_registration(state: models::AppState) -> impl Filter + Clone { warp::path!("webauthn" / "start-registration") .and(warp::post()) .and(json_body()) + .and(warp::any().map(move || state.clone())) .and_then(handlers::start_webauthn_registration) } -fn json_body() -> impl Filter + Clone { +fn json_body() -> impl Filter + Clone { // When accepting a body, we want a JSON body // (and to reject huge payloads)... warp::body::content_length_limit(1024 * 16).and(warp::body::json()) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 952e08f..8018f2e 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,9 +1,14 @@ use std::convert::Infallible; -use webauthn_rs::prelude::*; +use warp::Reply; -use crate::models::AppState; +use crate::{models::{AppState, Db, User}, templates}; -pub async fn start_webauthn_registration(app_state: AppState) -> Result { +pub async fn landing_page (app_state: AppState) -> Result { + let content: String = templates::landing_page().unwrap(); + Ok(warp::reply::html(content)) +} + +pub async fn start_webauthn_registration(user: User, app_state: AppState) -> Result { let mut db = app_state.db.lock().await; // TODO: query the user let mut user = db.users.first_mut().unwrap(); diff --git a/src/main.rs b/src/main.rs index 6c50cac..bf5af25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use warp::Filter; mod filters; mod handlers; mod models; +mod templates; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -20,11 +21,10 @@ async fn main() { println!("Server listening to {}", &args.bind); - let routes = warp::any().map(|| "Hello, World!"); + let routes = filters::all(models::AppState::new()); warp::serve(routes).run(addr).await; - println!("Turning out server"); - println!("Adieu"); + println!("Adieu, goodbye, auf wiedersehen"); } diff --git a/src/models/mod.rs b/src/models/mod.rs index a32d0a5..799b821 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use url::Url; use std::sync::{Arc}; @@ -12,6 +13,7 @@ pub struct Database { pub users: Vec } +#[derive(Serialize, Deserialize, Clone)] pub struct User { pub uuid: Uuid, pub user_name: String, @@ -19,19 +21,20 @@ pub struct User { pub creation_challenge_response: Option, } +#[derive(Clone)] pub struct AppState { pub webauthn: Arc, pub db: Db } impl AppState { - fn new() -> Self { + pub fn new() -> Self { let rp = "localhost"; let rp_origin = Url::parse("http://localhost:8000").expect("Invalid URL"); let builder = WebauthnBuilder::new(rp, &rp_origin).expect("Invalid configuration"); let builder = builder.rp_name("LocalHost"); let webauthn = Arc::new(builder.build().expect("Invalid configuration")); - let user: User = User { + let user: User = User { uuid: Uuid::new_v4(), user_name: "felix".to_string(), display_name: "FĂ©lix".to_string(), @@ -40,7 +43,7 @@ impl AppState { let db: Db = Arc::new(Mutex::new(Database { users: Vec::from([user]) })); - + AppState { webauthn, db, diff --git a/src/templates/landing.hbs b/src/templates/landing.hbs new file mode 100644 index 0000000..f96dbfe --- /dev/null +++ b/src/templates/landing.hbs @@ -0,0 +1,9 @@ + + + + Nom Nom GC + + + Hello world + + diff --git a/src/templates/mod.rs b/src/templates/mod.rs new file mode 100644 index 0000000..6ac48f1 --- /dev/null +++ b/src/templates/mod.rs @@ -0,0 +1,15 @@ +use handlebars::RenderError; +use serde_json::json; +use std::path::PathBuf; + +pub fn landing_page() -> Result { + let mut hb = handlebars::Handlebars::new(); + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path = path.join("src/templates/landing.hbs"); + let p: &str = path.to_str().unwrap(); + println!("{}", p); + hb.register_template_file("landing", p)?; + let data = json!({ + }); + hb.render("landing", &data) +}