From ac93ec5b022a9bafb5fd38cc5d270abd82f1002c Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 1 Jul 2017 11:52:41 -0700 Subject: [PATCH] Moved user structs out of db module --- src/db/mod.rs | 5 ++-- src/db/models.rs | 61 ------------------------------------------- src/main.rs | 1 + src/user.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 64 deletions(-) create mode 100644 src/user.rs diff --git a/src/db/mod.rs b/src/db/mod.rs index 71b4f40..538a020 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -11,6 +11,7 @@ use std::sync::{Arc, Mutex}; use config::UserConfig; use ddns::{DDNSConfigSource, DDNSConfig}; use errors::*; +use user::*; use vfs::{MountPoint, Vfs}; mod index; @@ -261,11 +262,9 @@ impl DB { } pub fn auth(&self, username: &str, password: &str) -> Result { - use self::users::dsl::*; let connection = self.connection.lock().unwrap(); let connection = connection.deref(); - let user: User = users.filter(name.eq(username)).get_result(connection)?; - Ok(user.verify_password(password)) + auth(connection, username, password) } } diff --git a/src/db/models.rs b/src/db/models.rs index 73956a9..9d27a28 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -1,8 +1,3 @@ -use rand; -use ring::{digest, pbkdf2}; - -use db::schema::*; - // Collection content #[derive(Debug, Queryable, Serialize)] pub struct Song { @@ -41,62 +36,6 @@ pub enum CollectionFile { Song(Song), } - -// User -#[derive(Debug, Queryable)] -pub struct User { - id: i32, - pub name: String, - pub password_salt: Vec, - pub password_hash: Vec, -} - -impl User { - pub fn verify_password(&self, attempted_password: &str) -> bool { - pbkdf2::verify(DIGEST_ALG, - HASH_ITERATIONS, - &self.password_salt, - attempted_password.as_bytes(), - &self.password_hash) - .is_ok() - } -} - -#[derive(Debug, Insertable)] -#[table_name="users"] -pub struct NewUser { - pub name: String, - pub password_salt: Vec, - pub password_hash: Vec, -} - -static DIGEST_ALG: &'static pbkdf2::PRF = &pbkdf2::HMAC_SHA256; -const CREDENTIAL_LEN: usize = digest::SHA256_OUTPUT_LEN; -const HASH_ITERATIONS: u32 = 10000; -type PasswordHash = [u8; CREDENTIAL_LEN]; - -impl NewUser { - pub fn new(name: &str, password: &str) -> NewUser { - let salt = rand::random::<[u8; 16]>().to_vec(); - let hash = NewUser::hash_password(&salt, password); - NewUser { - name: name.to_owned(), - password_salt: salt, - password_hash: hash, - } - } - - pub fn hash_password(salt: &Vec, password: &str) -> Vec { - let mut hash: PasswordHash = [0; CREDENTIAL_LEN]; - pbkdf2::derive(DIGEST_ALG, - HASH_ITERATIONS, - salt, - password.as_bytes(), - &mut hash); - hash.to_vec() - } -} - // Misc Settings #[derive(Debug, Queryable)] pub struct MiscSettings { diff --git a/src/main.rs b/src/main.rs index c4e2550..145a2a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,7 @@ mod ddns; mod errors; mod metadata; mod ui; +mod user; mod utils; mod thumbnails; mod vfs; diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..fe3f0d8 --- /dev/null +++ b/src/user.rs @@ -0,0 +1,67 @@ +use diesel::prelude::*; +use diesel::sqlite::SqliteConnection; +use rand; +use ring::{digest, pbkdf2}; + +use db::users; +use errors::*; + +#[derive(Debug, Queryable)] +pub struct User { + id: i32, + pub name: String, + pub password_salt: Vec, + pub password_hash: Vec, +} + +impl User { + pub fn verify_password(&self, attempted_password: &str) -> bool { + pbkdf2::verify(DIGEST_ALG, + HASH_ITERATIONS, + &self.password_salt, + attempted_password.as_bytes(), + &self.password_hash) + .is_ok() + } +} + +#[derive(Debug, Insertable)] +#[table_name="users"] +pub struct NewUser { + pub name: String, + pub password_salt: Vec, + pub password_hash: Vec, +} + +static DIGEST_ALG: &'static pbkdf2::PRF = &pbkdf2::HMAC_SHA256; +const CREDENTIAL_LEN: usize = digest::SHA256_OUTPUT_LEN; +const HASH_ITERATIONS: u32 = 10000; +type PasswordHash = [u8; CREDENTIAL_LEN]; + +impl NewUser { + pub fn new(name: &str, password: &str) -> NewUser { + let salt = rand::random::<[u8; 16]>().to_vec(); + let hash = NewUser::hash_password(&salt, password); + NewUser { + name: name.to_owned(), + password_salt: salt, + password_hash: hash, + } + } + + pub fn hash_password(salt: &Vec, password: &str) -> Vec { + let mut hash: PasswordHash = [0; CREDENTIAL_LEN]; + pbkdf2::derive(DIGEST_ALG, + HASH_ITERATIONS, + salt, + password.as_bytes(), + &mut hash); + hash.to_vec() + } +} + +pub fn auth(connection: &SqliteConnection, username: &str, password: &str) -> Result { + use db::users::dsl::*; + let user: User = users.filter(name.eq(username)).get_result(connection)?; + Ok(user.verify_password(password)) +}