From 285075f369690295a0ffc83fdb5b68f3460b30d5 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 8 Jun 2017 07:26:41 -0400 Subject: Testing password encryption. --- Cargo.lock | 45 +++++++++++++++++++ Cargo.toml | 2 + src/bin/crypto.rs | 18 ++++++++ src/bin/main.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++ src/main.rs | 127 ------------------------------------------------------ 6 files changed, 195 insertions(+), 127 deletions(-) create mode 100644 src/bin/crypto.rs create mode 100644 src/bin/main.rs delete mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 9abbf50..b0e15c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,8 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "sqlite 0.23.4 (registry+https://github.com/rust-lang/crates.io-index)", "staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -411,6 +413,18 @@ dependencies = [ "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-demangle" version = "0.1.4" @@ -439,6 +453,33 @@ name = "sequence_trie" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sqlite" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "sqlite3-sys 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sqlite3-src" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sqlite3-sys" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "sqlite3-src 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "staticfile" version = "0.4.0" @@ -652,11 +693,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db" "checksum route-recognizer 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3255338088df8146ba63d60a9b8e3556f1146ce2973bc05a75181a42ce2256" "checksum router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b9b1797ff166029cb632237bb5542696e54961b4cf75a324c6f05c9cf0584e4e" +"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rustc-demangle 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3058a43ada2c2d0b92b3ae38007a2d0fa5e9db971be260e0171408a4ff471c95" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084" "checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac" "checksum sequence_trie 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c915714ca833b1d4d6b8f6a9d72a3ff632fe45b40a8d184ef79c81bec6327eed" +"checksum sqlite 0.23.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3cd27f80d9229b15313403f73d9180f2368f181a870be51f07e8f7f41d7a264" +"checksum sqlite3-src 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "52f02ac7a64c3446581ee43758075d9f1cdced596d70ffe28bf02979a719dbed" +"checksum sqlite3-sys 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "71fec807a1534bd13eeaaec396175d67c79bdc68df55e18a452726ec62a8fb08" "checksum staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31493480e073d52522a94cdf56269dd8eb05f99549effd1826b0271690608878" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209" diff --git a/Cargo.toml b/Cargo.toml index 8661e7a..9e9a7a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ router = "*" horrorshow = "*" staticfile = "*" regex = "*" +sqlite = "*" +rust-crypto = "*" diff --git a/src/bin/crypto.rs b/src/bin/crypto.rs new file mode 100644 index 0000000..e059663 --- /dev/null +++ b/src/bin/crypto.rs @@ -0,0 +1,18 @@ +extern crate crypto; + +use crypto::bcrypt_pbkdf::bcrypt_pbkdf; + +pub fn encode(pw: &str) -> Vec { + let salt = "hello"; + let mut out = vec!(0; 32); + let encrypted = bcrypt_pbkdf( + pw.as_bytes(), salt.as_bytes(), + 100, &mut out); + out +} + +pub fn main() { + let pw = "123"; + let out = encode(pw); + println!("{}: {:?}", pw, out); +} diff --git a/src/bin/main.rs b/src/bin/main.rs new file mode 100644 index 0000000..36463a6 --- /dev/null +++ b/src/bin/main.rs @@ -0,0 +1,127 @@ +// CSRF protection +// https://github.com/heartsucker/iron-csrf +#[macro_use] +extern crate iron; +#[macro_use] +extern crate log; +#[macro_use] +extern crate router; +extern crate env_logger; +extern crate systemhttp; +extern crate iron_sessionstorage; +extern crate staticfile; +extern crate regex; + +use iron_sessionstorage::traits::*; +use iron_sessionstorage::SessionStorage; +use iron_sessionstorage::backends::SignedCookieBackend; +use iron::status; +use iron::modifiers::Header; +use iron::headers::ContentType; +use iron::{Iron, Request, IronResult, Response, Chain}; +use router::Router; +use systemhttp::systemd::unit; +use systemhttp::systemd::journal; +use systemhttp::render; +use staticfile::Static; +use regex::Regex; + +struct Aaa(String); + +impl iron_sessionstorage::Value for Aaa { + fn get_key() -> &'static str { + "aaa" + } + fn into_raw(self) -> String { + self.0 + } + fn from_raw(v: String) -> Option { + Some(Aaa(v)) + } +} + +fn overview(r: &mut Request) -> IronResult { + let mut _value = match try!(r.session().get::()) { + Some(aaa) => aaa, + None => Aaa("".to_owned()), + }; + + let name = r.extensions + .get::() + .unwrap() + .find("name") + .unwrap_or("World").to_owned(); + + let units = unit::get_units("*").unwrap(); + + let sections = ["service", "timer", "socket", "target", "slice", "mount", + "path"]; + let units_by_section = sections.iter().map(|&s| { + (s.to_owned(), + units.iter().filter(|&u| &u.type_ == s).collect::>()) + }).collect::>(); + + // let res = Ok(Response::with((status::Ok, + // Header(ContentType::html()), + // render_message(&format!("Hello, {} ({})", + // name, + // session_value.0), + // &units)))); + + // info!("Updating session value. Current value: {}", session_value.0); + // session_value.0.push('a'); + // try!(r.session().set(session_value)); + Ok(Response::with((status::Ok, + Header(ContentType::html()), + render::system_status(&units_by_section)))) +} + +fn journal(r: &mut Request) -> IronResult { + let unit = iexpect!(r.extensions + .get::() + .unwrap() + .find("unit"), status::BadRequest); + let re = Regex::new(r"[-_\w\d]*").unwrap(); + if !re.is_match(unit) { + return Ok(Response::with( + (status::BadRequest, + format!("Unit ({}) does not match {}", + unit, re)))); + } + Ok(Response::with((status::Ok, + itry!(journal::get_log(unit, 100))))) +} + +fn unit_status(r: &mut Request) -> IronResult { + let unit_name = iexpect!(r.extensions + .get::() + .unwrap() + .find("unit"), status::BadRequest); + let re = Regex::new(r"[-_\w\d]*").unwrap(); + if !re.is_match(unit_name) { + return Ok(Response::with( + (status::BadRequest, + format!("Unit ({}) does not match {}", + unit_name, re)))); + } + let ref unit = itry!(unit::get_units(unit_name))[0]; + let log = itry!(journal::get_log(unit_name, 15)); + Ok(Response::with((status::Ok, + Header(ContentType::html()), + render::unit_status(&unit, &log)))) +} + +fn main() { + env_logger::init().unwrap(); + let secret = b"secret2".to_vec(); + let router = router!( + root: get "/" => overview, + details: get "/status/:unit" => unit_status, + journal: get "/journal/:unit" => journal, + css: get "/static/main.css" => Static::new(""), + ); + let mut chain = Chain::new(router); + chain.link_around(SessionStorage::new(SignedCookieBackend::new(secret))); + let _server = Iron::new(chain).http(":::8080").unwrap(); + println!("On 8080"); +} diff --git a/src/lib.rs b/src/lib.rs index a5aebdf..1fef49c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ #[macro_use] extern crate horrorshow; +extern crate rust_crypto; +extern crate sqlite; pub mod systemd; pub mod render; +mod auth; #[cfg(test)] mod tests { diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 36463a6..0000000 --- a/src/main.rs +++ /dev/null @@ -1,127 +0,0 @@ -// CSRF protection -// https://github.com/heartsucker/iron-csrf -#[macro_use] -extern crate iron; -#[macro_use] -extern crate log; -#[macro_use] -extern crate router; -extern crate env_logger; -extern crate systemhttp; -extern crate iron_sessionstorage; -extern crate staticfile; -extern crate regex; - -use iron_sessionstorage::traits::*; -use iron_sessionstorage::SessionStorage; -use iron_sessionstorage::backends::SignedCookieBackend; -use iron::status; -use iron::modifiers::Header; -use iron::headers::ContentType; -use iron::{Iron, Request, IronResult, Response, Chain}; -use router::Router; -use systemhttp::systemd::unit; -use systemhttp::systemd::journal; -use systemhttp::render; -use staticfile::Static; -use regex::Regex; - -struct Aaa(String); - -impl iron_sessionstorage::Value for Aaa { - fn get_key() -> &'static str { - "aaa" - } - fn into_raw(self) -> String { - self.0 - } - fn from_raw(v: String) -> Option { - Some(Aaa(v)) - } -} - -fn overview(r: &mut Request) -> IronResult { - let mut _value = match try!(r.session().get::()) { - Some(aaa) => aaa, - None => Aaa("".to_owned()), - }; - - let name = r.extensions - .get::() - .unwrap() - .find("name") - .unwrap_or("World").to_owned(); - - let units = unit::get_units("*").unwrap(); - - let sections = ["service", "timer", "socket", "target", "slice", "mount", - "path"]; - let units_by_section = sections.iter().map(|&s| { - (s.to_owned(), - units.iter().filter(|&u| &u.type_ == s).collect::>()) - }).collect::>(); - - // let res = Ok(Response::with((status::Ok, - // Header(ContentType::html()), - // render_message(&format!("Hello, {} ({})", - // name, - // session_value.0), - // &units)))); - - // info!("Updating session value. Current value: {}", session_value.0); - // session_value.0.push('a'); - // try!(r.session().set(session_value)); - Ok(Response::with((status::Ok, - Header(ContentType::html()), - render::system_status(&units_by_section)))) -} - -fn journal(r: &mut Request) -> IronResult { - let unit = iexpect!(r.extensions - .get::() - .unwrap() - .find("unit"), status::BadRequest); - let re = Regex::new(r"[-_\w\d]*").unwrap(); - if !re.is_match(unit) { - return Ok(Response::with( - (status::BadRequest, - format!("Unit ({}) does not match {}", - unit, re)))); - } - Ok(Response::with((status::Ok, - itry!(journal::get_log(unit, 100))))) -} - -fn unit_status(r: &mut Request) -> IronResult { - let unit_name = iexpect!(r.extensions - .get::() - .unwrap() - .find("unit"), status::BadRequest); - let re = Regex::new(r"[-_\w\d]*").unwrap(); - if !re.is_match(unit_name) { - return Ok(Response::with( - (status::BadRequest, - format!("Unit ({}) does not match {}", - unit_name, re)))); - } - let ref unit = itry!(unit::get_units(unit_name))[0]; - let log = itry!(journal::get_log(unit_name, 15)); - Ok(Response::with((status::Ok, - Header(ContentType::html()), - render::unit_status(&unit, &log)))) -} - -fn main() { - env_logger::init().unwrap(); - let secret = b"secret2".to_vec(); - let router = router!( - root: get "/" => overview, - details: get "/status/:unit" => unit_status, - journal: get "/journal/:unit" => journal, - css: get "/static/main.css" => Static::new(""), - ); - let mut chain = Chain::new(router); - chain.link_around(SessionStorage::new(SignedCookieBackend::new(secret))); - let _server = Iron::new(chain).http(":::8080").unwrap(); - println!("On 8080"); -} -- cgit v1.2.3