mirror of
https://github.com/agersant/polaris
synced 2024-11-10 10:14:12 +00:00
Moved auth() out of DB struct
This commit is contained in:
parent
c314f7a452
commit
d9532dd22c
3 changed files with 9 additions and 10 deletions
|
@ -18,6 +18,7 @@ use url::percent_encoding::percent_decode;
|
||||||
use db::DB;
|
use db::DB;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use thumbnails::*;
|
use thumbnails::*;
|
||||||
|
use user;
|
||||||
use utils::*;
|
use utils::*;
|
||||||
|
|
||||||
const CURRENT_MAJOR_VERSION: i32 = 2;
|
const CURRENT_MAJOR_VERSION: i32 = 2;
|
||||||
|
@ -149,7 +150,7 @@ impl Handler for AuthHandler {
|
||||||
// Auth via Authorization header
|
// Auth via Authorization header
|
||||||
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
|
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
|
||||||
if let Some(ref password) = auth.password {
|
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
|
req.extensions
|
||||||
.insert::<SessionKey>(Session { username: auth.username.clone() });
|
.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()),
|
_ => 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
|
request
|
||||||
.extensions
|
.extensions
|
||||||
.insert::<SessionKey>(Session { username: username.clone() });
|
.insert::<SessionKey>(Session { username: username.clone() });
|
||||||
|
|
|
@ -245,12 +245,6 @@ impl DB {
|
||||||
.filter_map(|s| self.virtualize_directory(&vfs, s));
|
.filter_map(|s| self.virtualize_directory(&vfs, s));
|
||||||
Ok(virtual_directories.collect::<Vec<_>>())
|
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 {
|
impl ConnectionSource for DB {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
use core::ops::Deref;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::sqlite::SqliteConnection;
|
|
||||||
use rand;
|
use rand;
|
||||||
use ring::{digest, pbkdf2};
|
use ring::{digest, pbkdf2};
|
||||||
|
|
||||||
|
use db::ConnectionSource;
|
||||||
use db::users;
|
use db::users;
|
||||||
use errors::*;
|
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::*;
|
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)?;
|
let user: User = users.filter(name.eq(username)).get_result(connection)?;
|
||||||
Ok(user.verify_password(password))
|
Ok(user.verify_password(password))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue