Moved auth() out of DB struct

This commit is contained in:
Antoine Gersant 2017-07-01 12:42:49 -07:00
parent c314f7a452
commit d9532dd22c
3 changed files with 9 additions and 10 deletions

View file

@ -18,6 +18,7 @@ use url::percent_encoding::percent_decode;
use db::DB;
use errors::*;
use thumbnails::*;
use user;
use utils::*;
const CURRENT_MAJOR_VERSION: i32 = 2;
@ -149,7 +150,7 @@ impl Handler for AuthHandler {
// Auth via Authorization header
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
if let Some(ref password) = auth.password {
auth_success = self.db.auth(auth.username.as_str(), password.as_str())?;
auth_success = user::auth(self.db.deref(), auth.username.as_str(), password.as_str())?;
req.extensions
.insert::<SessionKey>(Session { username: auth.username.clone() });
}
@ -193,7 +194,7 @@ fn auth(request: &mut Request, db: &DB) -> IronResult<Response> {
_ => return Err(Error::from(ErrorKind::MissingPassword).into()),
};
}
if db.auth(username.as_str(), password.as_str())? {
if user::auth(db, username.as_str(), password.as_str())? {
request
.extensions
.insert::<SessionKey>(Session { username: username.clone() });

View file

@ -245,12 +245,6 @@ impl DB {
.filter_map(|s| self.virtualize_directory(&vfs, s));
Ok(virtual_directories.collect::<Vec<_>>())
}
pub fn auth(&self, username: &str, password: &str) -> Result<bool> {
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
auth(connection, username, password)
}
}
impl ConnectionSource for DB {

View file

@ -1,8 +1,9 @@
use core::ops::Deref;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use rand;
use ring::{digest, pbkdf2};
use db::ConnectionSource;
use db::users;
use errors::*;
@ -60,8 +61,11 @@ impl NewUser {
}
}
pub fn auth(connection: &SqliteConnection, username: &str, password: &str) -> Result<bool> {
pub fn auth<T>(db: &T, username: &str, password: &str) -> Result<bool> where T: ConnectionSource {
use db::users::dsl::*;
let connection = db.get_connection();
let connection = connection.lock().unwrap();
let connection = connection.deref();
let user: User = users.filter(name.eq(username)).get_result(connection)?;
Ok(user.verify_password(password))
}