From de9a97a709dd28351da1d815378d11097e5899fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phil=20H=C3=B6fer?= <phil.hoefer@suma-ev.de> Date: Fri, 21 Jun 2024 13:27:54 +0200 Subject: [PATCH] Implement Authenthication --- src/main.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main.rs b/src/main.rs index 11958e1..20a8e46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,21 @@ fn main() -> Result<(), io::Error> { // request.url(), // request.headers() // ); + match config.get("auth") { + Some(toml::Value::String(server_auth)) => { + match get_authkey(request.url().clone()) { + Ok(client_auth) => { + if client_auth != server_auth.clone() { + println!("invalid auth:{}, server auth: {}", client_auth, server_auth); + request.respond(Response::from_string("")); + continue; + } + }, + _ => {} + } + }, + _ => {} + } let query = get_query(request.url()); //println!("got query:{}", query.clone().unwrap()); match query { @@ -95,6 +110,18 @@ fn get_query(request_url: &str) -> Result<String, url::ParseError> { } Ok(String::from_str("").unwrap()) } +fn get_authkey(request_url: &str) -> Result<String, url::ParseError> { + let parsed_url = request_url.split_once('?').map_or(request_url, |(_, after)| after); + //println!("parsed_url:{}", parsed_url); + let query_pairs = url::form_urlencoded::parse(parsed_url.as_bytes()); + for (key, value) in query_pairs { + //println!("key:{}, value: {}", key, value); + if key == "auth" { + return Ok(value.into_owned()); + } + } + Ok(String::from_str("").unwrap()) +} fn build_markov_chain(file_path: &str) -> Result<MarkovChain, io::Error> { let file = File::open(file_path)?; -- GitLab