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

78 lines
3.0 KiB
Rust

use handlebars::RenderError;
use serde_json::json;
use handlebars::Handlebars;
use std::{path::PathBuf, sync::Arc};
use crate::models::{RegistrationUuid, ProjectSummary};
pub fn new<'a>() -> Result<Handlebars<'a>, 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<Handlebars<'_>>, logged: bool, project_summaries: Vec<ProjectSummary>) -> Result<String, RenderError> {
let data = json!({
"binaryCaches": [{
"name": "NixOS Binary Cache",
"projects": project_summaries.into_iter().map(|p| json!({
"name": p.name,
"latestClosure": p.latest_closure,
"datetime": p.latest_closure_datetime.to_string()
}
)).collect::<Vec<_>>()
}]});
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)
}