From 10d5a1c515fa929d8fde5f1df864195ddd499b90 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sat, 27 Feb 2021 20:11:47 +0100 Subject: [PATCH] feat: Add a configurable headline to the form --- src/api.rs | 12 ++++++----- src/config.rs | 19 ++++++++++++++--- src/context.rs | 25 +++++++++++++++++++++++ src/main.rs | 11 ++++++++-- src/static.rs | 6 ++++-- src/static/{index.html => index.html.hbs} | 1 + 6 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/context.rs rename src/static/{index.html => index.html.hbs} (98%) diff --git a/src/api.rs b/src/api.rs index 6479d7f..927ebe8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -22,7 +22,7 @@ use rocket_contrib::json::Json; use rocket::post; use serde_derive::{Deserialize, Serialize}; -use crate::config::Config; +use crate::context::Context; #[derive(Deserialize)] pub struct PasswordData { @@ -43,10 +43,11 @@ pub struct Response { message: Option, } -fn change_password(data: Json, config: rocket::State) -> Result<()> { +fn change_password(data: Json, + context: rocket::State) -> Result<()> { let dn = format!("uid={},ou=People,dc=fiveop,dc=de", &data.username); - let mut ldap = LdapConn::new(&config.ldap_url)?; + let mut ldap = LdapConn::new(&context.ldap_url)?; ldap .simple_bind(&dn, &data.old_password)? .success()?; @@ -63,9 +64,10 @@ fn change_password(data: Json, config: rocket::State) -> R } #[post("/update", data = "")] -pub fn update(data: Json, config: rocket::State) -> Json { +pub fn update(data: Json, + context: rocket::State) -> Json { Json( - match change_password(data, config) { + match change_password(data, context) { Ok(_) => Response{ success: true, message: None, diff --git a/src/config.rs b/src/config.rs index 91d4d2b..6e171b2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,6 +22,10 @@ use handlebars::Handlebars; use serde_derive::Deserialize; use serde_json::json; +fn default_headline() -> String { + "Change your password".to_string() +} + fn default_ldap_url() -> String { "ldap://localhost".to_string() } @@ -38,6 +42,9 @@ fn default_port() -> u16 { pub struct Config { pub dn: String, + #[serde(default = "default_headline")] + pub headline: String, + #[serde(default = "default_ldap_url")] pub ldap_url: String, @@ -50,15 +57,21 @@ pub struct Config { pub fn load_config(file_path: &str) -> Result { let file = File::open(file_path). - with_context(|| format!("Failed to open configuration file '{}'", file_path))?; + with_context(|| format!("Failed to open configuration file '{}'", + file_path))?; let config: Config = serde_json::from_reader(BufReader::new(file)) - .with_context(|| format!("Failed to parse configuration file '{}'", file_path))?; + .with_context(|| format!("Failed to parse configuration file '{}'", + file_path))?; Handlebars::new() .render_template(&config.dn, &json!({"username" : "foo"})) .map(|_| ()) - .with_context(|| format!("Failed to parse DN ({}) in configuration file '{}'", config.dn, file_path))?; + .with_context(|| format!( + "Failed to render DN ({}) from configuration file '{}'", + config.dn, + file_path + ))?; Ok(config) } diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..0c12e41 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,25 @@ +use anyhow::{Context as AnyhowContext, Result}; +use handlebars::Handlebars; +use serde_json::json; + +use crate::config::Config; + +pub struct Context { + pub ldap_url: String, + pub index_html: String, +} + +impl Context { + pub fn new(config: &Config) -> Result { + let index_html = Handlebars::new() + .render_template(include_str!("static/index.html.hbs"), + &json!({"headline" : config.headline})) + .with_context(|| format!( + "Failed to render index.html from template and configured headline '{}'", + config.headline + ))?; + + Ok(Context{ldap_url: config.ldap_url.clone(), + index_html}) + } +} diff --git a/src/main.rs b/src/main.rs index 6678314..c5826a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod api; mod config; +mod context; mod r#static; use anyhow::{Context, Result}; @@ -46,11 +47,17 @@ fn main() -> Result<()> { let config = load_config(config_file_path)?; + let context = context::Context::new(&config)?; + let rocket_config = Config::build(Environment::Production) .address(&config.host) .port(config.port) .finalize() - .with_context(|| format!("Bad host address ({}) in configuration file '{}'", config.host, config_file_path))?; + .with_context(|| format!( + "Bad host address ({}) in configuration file '{}'", + config.host, + config_file_path + ))?; rocket::custom(rocket_config) .mount("/", routes![r#static::index, @@ -62,7 +69,7 @@ fn main() -> Result<()> { r#static::logo, r#static::hourglass, api::update]) - .manage(config) + .manage(context) .launch(); Ok(()) diff --git a/src/static.rs b/src/static.rs index 5445cd0..55e5d10 100644 --- a/src/static.rs +++ b/src/static.rs @@ -20,6 +20,8 @@ use rocket::response::{content, Responder, Response}; use rocket::get; +use crate::context::Context; + pub struct Svg(pub R); impl<'r, R: Responder<'r>> Responder<'r> for Svg { @@ -29,8 +31,8 @@ impl<'r, R: Responder<'r>> Responder<'r> for Svg { } #[get("/")] -pub fn index() -> content::Html<&'static str> { - content::Html(include_str!("static/index.html")) +pub fn index(context: rocket::State) -> content::Html { + content::Html(context.index_html.clone()) } #[get("/checkmark.svg")] diff --git a/src/static/index.html b/src/static/index.html.hbs similarity index 98% rename from src/static/index.html rename to src/static/index.html.hbs index 4210c9c..0b2d6bb 100644 --- a/src/static/index.html +++ b/src/static/index.html.hbs @@ -26,6 +26,7 @@ with the WebLDAPPasswd. If not, see . +

{{headline}}