61 lines
2.2 KiB
Rust
61 lines
2.2 KiB
Rust
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<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");
|
|
|
|
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())?;
|
|
|
|
Ok(hbs)
|
|
}
|
|
|
|
pub fn landing_page(hb: Arc<Handlebars<'_>>) -> Result<String, RenderError> {
|
|
let data = json!({
|
|
|
|
});
|
|
hb.render("landing", &data)
|
|
}
|
|
|
|
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": ®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<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)
|
|
}
|