Moved user structs out of db module
This commit is contained in:
parent
d70abba47e
commit
ac93ec5b02
4 changed files with 70 additions and 64 deletions
|
@ -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<bool> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<u8>,
|
||||
pub password_hash: Vec<u8>,
|
||||
}
|
||||
|
||||
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<u8>,
|
||||
pub password_hash: Vec<u8>,
|
||||
}
|
||||
|
||||
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<u8>, password: &str) -> Vec<u8> {
|
||||
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 {
|
||||
|
|
|
@ -65,6 +65,7 @@ mod ddns;
|
|||
mod errors;
|
||||
mod metadata;
|
||||
mod ui;
|
||||
mod user;
|
||||
mod utils;
|
||||
mod thumbnails;
|
||||
mod vfs;
|
||||
|
|
67
src/user.rs
Normal file
67
src/user.rs
Normal file
|
@ -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<u8>,
|
||||
pub password_hash: Vec<u8>,
|
||||
}
|
||||
|
||||
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<u8>,
|
||||
pub password_hash: Vec<u8>,
|
||||
}
|
||||
|
||||
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<u8>, password: &str) -> Vec<u8> {
|
||||
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<bool> {
|
||||
use db::users::dsl::*;
|
||||
let user: User = users.filter(name.eq(username)).get_result(connection)?;
|
||||
Ok(user.verify_password(password))
|
||||
}
|
Loading…
Add table
Reference in a new issue