nom-nom-nix-gc/src/templates/mod.rs

76 lines
2.7 KiB
Rust

use handlebars::RenderError;
use rust_embed::RustEmbed;
use serde_json::json;
use handlebars::Handlebars;
use std::{sync::Arc, str::from_utf8};
use crate::models::RegistrationUuid;
#[derive(RustEmbed)]
#[folder = "src/templates"]
struct Templates;
pub fn new<'a>() -> Result<Handlebars<'a>, RenderError> {
let landing_path = Templates::get("landing.hbs").unwrap();
let template_path = Templates::get("template.hbs").unwrap();
let register_user = Templates::get("register-user.hbs").unwrap();
let mut hbs = handlebars::Handlebars::new();
let css = Templates::get("main.css").unwrap();
let webauthn_register_js =Templates::get("webauthn-register.js").unwrap();
let webauthn_login_js = Templates::get("webauthn-login.js").unwrap();
let login = Templates::get("login.hbs").unwrap();
let new_binary_cache_form = Templates::get("new-binary-cache.hbs").unwrap();
hbs.register_template_string("landing", from_utf8(&landing_path.data).unwrap())?;
hbs.register_template_string("template", from_utf8(&template_path.data).unwrap())?;
hbs.register_template_string("css", from_utf8(&css.data).unwrap())?;
hbs.register_template_string("webauthn-register-js", from_utf8(&webauthn_register_js.data).unwrap())?;
hbs.register_template_string("webauthn-login-js", from_utf8(&webauthn_login_js.data).unwrap())?;
hbs.register_template_string("register-user", from_utf8(&register_user.data).unwrap())?;
hbs.register_template_string("login", from_utf8(&login.data).unwrap())?;
hbs.register_template_string("new-binary-cache-form", from_utf8(&new_binary_cache_form.data).unwrap())?;
Ok(hbs)
}
pub fn landing_page(hb: Arc<Handlebars<'_>>, _logged: bool) -> Result<String, RenderError> {
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<Handlebars<'_>>, registration_uuid: RegistrationUuid, username: String, keyids: Vec<String>) -> Result<String, RenderError> {
let js_data = json!({
"registration-uuid": &registration_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<Handlebars<'_>>) -> Result<String, RenderError> {
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<Handlebars<'_>>) -> Result<String, RenderError> {
let data = json!({
});
hb.render("new-binary-cache-form", &data)
}