This commit is contained in:
Félix Baylac Jacqué 2023-01-08 17:59:58 +01:00
parent d44dea33c9
commit d1d9358335
8 changed files with 128 additions and 11 deletions

67
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -1,15 +1,30 @@
use warp::Filter;
use crate::handlers;
use crate::models;
pub fn start_webauthn_registration( ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
pub fn all(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
landing(state.clone())
.or(start_webauthn_registration(state.clone()))
.or(start_webauthn_registration(state.clone()))
}
pub fn landing(state: models::AppState) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + 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<Extract = impl warp::Reply, Error = warp::Rejection> + 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<Extract = (Todo,), Error = warp::Rejection> + Clone {
fn json_body() -> impl Filter<Extract = (models::User,), Error = warp::Rejection> + 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())

View File

@ -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<impl warp::Reply, Infallible> {
pub async fn landing_page (app_state: AppState) -> Result<impl Reply, Infallible> {
let content: String = templates::landing_page().unwrap();
Ok(warp::reply::html(content))
}
pub async fn start_webauthn_registration(user: User, app_state: AppState) -> Result<impl warp::Reply, Infallible> {
let mut db = app_state.db.lock().await;
// TODO: query the user
let mut user = db.users.first_mut().unwrap();

View File

@ -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");
}

View File

@ -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<User>
}
#[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<webauthn_rs::prelude::CreationChallengeResponse>,
}
#[derive(Clone)]
pub struct AppState {
pub webauthn: Arc<Webauthn>,
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,

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Nom Nom GC</title>
</head>
<body>
Hello world
</body>
</html>

15
src/templates/mod.rs Normal file
View File

@ -0,0 +1,15 @@
use handlebars::RenderError;
use serde_json::json;
use std::path::PathBuf;
pub fn landing_page() -> Result<String, RenderError> {
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)
}