Browse Source

Hash passwords

master
Dylan Baker 7 months ago
parent
commit
c65e801587
5 changed files with 45 additions and 15 deletions
  1. 1
    2
      oslo-lib/Cargo.toml
  2. 10
    1
      oslo-lib/src/lib.rs
  3. 3
    2
      oslo-lib/src/routes.rs
  4. 23
    7
      src/main.rs
  5. 8
    3
      tests/auth.rs

+ 1
- 2
oslo-lib/Cargo.toml View File

@@ -4,9 +4,8 @@ version = "0.1.0"
4 4
 authors = ["Dylan Baker <dylan@simulacrum.party>"]
5 5
 edition = "2018"
6 6
 
7
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
-
9 7
 [dependencies]
8
+rust-argon2 = "0.4.0"
10 9
 async-std = { version = "1.6.0", features = ["attributes"] }
11 10
 chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
12 11
 dotenv = "0.15.0"

+ 10
- 1
oslo-lib/src/lib.rs View File

@@ -1,4 +1,5 @@
1
-use dotenv;
1
+use argon2::{self, Config};
2
+use dotenv::{self};
2 3
 use tide::utils::After;
3 4
 
4 5
 mod fs;
@@ -53,3 +54,11 @@ pub async fn build_app() -> Result<tide::Server<State>, tide::Error> {
53 54
 
54 55
     Ok(app)
55 56
 }
57
+
58
+pub fn hash_password(password: &str, username: &str) -> String {
59
+    let password = password.as_bytes();
60
+    let salt = format!("{}{}", &username, &username);
61
+    let salt = salt.as_bytes();
62
+    let config = Config::default();
63
+    argon2::hash_encoded(password, salt, &config).unwrap()
64
+}

+ 3
- 2
oslo-lib/src/routes.rs View File

@@ -8,7 +8,7 @@ use tide::{Redirect, Request, Response, Result, StatusCode};
8 8
 
9 9
 use std::env;
10 10
 
11
-use crate::{fs, post::Post, templates::render_template, util, State};
11
+use crate::{fs, hash_password, post::Post, templates::render_template, util, State};
12 12
 
13 13
 #[derive(Debug, Serialize, Deserialize)]
14 14
 struct User {
@@ -87,7 +87,8 @@ pub async fn login(mut req: Request<State>) -> Result {
87 87
     let username = env::var("ADMIN_USERNAME")?;
88 88
     let password = env::var("ADMIN_PASSWORD")?;
89 89
     let user: User = req.body_form().await?;
90
-    if user.username == username && user.password == password {
90
+
91
+    if user.username == username && hash_password(&user.password, &user.username) == password {
91 92
         req.session_mut().insert("logged_in", true)?;
92 93
         redirect("/")
93 94
     } else {

+ 23
- 7
src/main.rs View File

@@ -1,13 +1,29 @@
1
-use oslo_lib::build_app;
1
+use oslo_lib::{build_app, hash_password};
2 2
 use tide::Result;
3 3
 
4 4
 #[async_std::main]
5 5
 async fn main() -> Result<()> {
6
-    tide::log::start();
7
-    let hostname = std::env::var("HOSTNAME").unwrap_or(String::from("127.0.0.1"));
8
-    let port = std::env::var("PORT").unwrap_or(String::from("8080"));
9
-    let app = build_app().await?;
10
-    app.listen(format!("{}:{}", hostname, port)).await?;
6
+    let mut args = std::env::args();
7
+    if let Some(command) = &args.nth(1) {
8
+        if command == "passwordgen" {
9
+            let username = &args.next().expect("Username is required");
10
+            let password = &args.next().expect("Password is required");
11
+            let hash = hash_password(&password, &username);
12
+            println!("{}", hash);
13
+        }
11 14
 
12
-    Ok(())
15
+        Ok(())
16
+    } else {
17
+        let env = std::env::var("OSLO_ENVIRONMENT").unwrap_or(String::from("development"));
18
+        if env == "development" {
19
+            tide::log::start();
20
+        }
21
+
22
+        let hostname = std::env::var("HOSTNAME").unwrap_or(String::from("127.0.0.1"));
23
+        let port = std::env::var("PORT").unwrap_or(String::from("8080"));
24
+        let app = build_app().await?;
25
+        app.listen(format!("{}:{}", hostname, port)).await?;
26
+
27
+        Ok(())
28
+    }
13 29
 }

+ 8
- 3
tests/auth.rs View File

@@ -2,7 +2,7 @@ mod tests {
2 2
     use std::path::PathBuf;
3 3
 
4 4
     use async_std::task::block_on;
5
-    use oslo_lib::State;
5
+    use oslo_lib::{hash_password, State};
6 6
     use serde_json::json;
7 7
     use tide::{
8 8
         http::{Method, Request, Response, StatusCode, Url},
@@ -10,6 +10,11 @@ mod tests {
10 10
     };
11 11
 
12 12
     async fn build_app() -> Server<State> {
13
+        std::env::set_var("ADMIN_USERNAME", "correct_username".to_string());
14
+        std::env::set_var(
15
+            "ADMIN_PASSWORD",
16
+            hash_password("correct_password", "correct_username"),
17
+        );
13 18
         oslo_lib::build_app().await.unwrap()
14 19
     }
15 20
 
@@ -38,8 +43,8 @@ mod tests {
38 43
             let app = build_app().await;
39 44
             let mut req = Request::new(Method::Post, url("login"));
40 45
             req.set_content_type(tide::http::mime::MULTIPART_FORM);
41
-            let username = std::env::var("ADMIN_USERNAME").unwrap();
42
-            let password = std::env::var("ADMIN_PASSWORD").unwrap();
46
+            let username = String::from("correct_username");
47
+            let password = String::from("correct_password");
43 48
             req.replace_body(format!("username={}&password={}", username, password));
44 49
             let res: Response = app.respond(req).await?;
45 50
             assert_eq!(res.status(), StatusCode::Found);

Loading…
Cancel
Save