use handlebars::RenderError; use serde_json::json; use handlebars::Handlebars; use std::{path::PathBuf, sync::Arc}; use crate::models::RegistrationUuid; pub fn new<'a>() -> Result, RenderError> { let rootpath = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let landing_path = rootpath.join("src/templates/landing.hbs"); let template_path = rootpath.join("src/templates/template.hbs"); let register_user = rootpath.join("src/templates/register-user.hbs"); let mut hbs = handlebars::Handlebars::new(); let css = rootpath.join("src/templates/main.css"); let webauthn_register_js = rootpath.join("src/templates/webauthn-register.js"); let webauthn_login_js = rootpath.join("src/templates/webauthn-login.js"); let login = rootpath.join("src/templates/login.hbs"); let new_binary_cache_form = rootpath.join("src/templates/new-binary-cache.hbs"); hbs.register_template_file("landing", landing_path.to_str().unwrap())?; hbs.register_template_file("template", template_path.to_str().unwrap())?; hbs.register_template_file("css", css.to_str().unwrap())?; hbs.register_template_file("webauthn-register-js", webauthn_register_js.to_str().unwrap())?; hbs.register_template_file("webauthn-login-js", webauthn_login_js.to_str().unwrap())?; hbs.register_template_file("register-user", register_user.to_str().unwrap())?; hbs.register_template_file("login", login.to_str().unwrap())?; hbs.register_template_file("new-binary-cache-form", new_binary_cache_form.to_str().unwrap())?; Ok(hbs) } pub fn landing_page(hb: Arc>, logged: bool) -> Result { let data = json!({ "binaryCaches": [{ }] }); hb.render("landing", &data) } /** Generates a webauthn challenge to initiate a fido key enrolling procedure. */ pub fn register_user_start(hb: Arc>, registration_uuid: RegistrationUuid, username: String, keyids: Vec) -> Result { let js_data = json!({ "registration-uuid": ®istration_uuid.0.to_string(), }); let js = hb.render("webauthn-register-js", &js_data)?; let data = json!({ "js": js, "username": &username, "keyids": keyids }); hb.render("register-user", &data) } pub fn login(hb: Arc>) -> Result { let js_data = json!({ }); let js = hb.render("webauthn-login-js", &js_data)?; let data = json!({ "js": js, }); hb.render("login", &data) } pub fn new_binary_cache(hb: Arc>) -> Result { let data = json!({ }); hb.render("new-binary-cache-form", &data) }