|
|
|
@ -1,10 +1,12 @@
|
|
|
|
|
use crate::database::models::UserRecord;
|
|
|
|
|
use crate::database::tokens::SessionTokens;
|
|
|
|
|
use crate::database::user_roles::UserRoles;
|
|
|
|
|
use crate::database::{DatabaseError, DatabaseResult, RedisConnection, Table};
|
|
|
|
|
use crate::utils::create_salt;
|
|
|
|
|
use postgres::{Client, Error};
|
|
|
|
|
use crate::utils::{create_salt, get_user_id_from_token, TOKEN_LENGTH};
|
|
|
|
|
use postgres::Client;
|
|
|
|
|
use scrypt::ScryptParams;
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
use zeroize::{Zeroize, Zeroizing};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Users {
|
|
|
|
@ -13,8 +15,6 @@ pub struct Users {
|
|
|
|
|
user_roles: UserRoles,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SALT_LENGTH: usize = 16;
|
|
|
|
|
|
|
|
|
|
impl Table for Users {
|
|
|
|
|
fn new(
|
|
|
|
|
database_connection: Arc<Mutex<Client>>,
|
|
|
|
@ -55,6 +55,7 @@ impl Users {
|
|
|
|
|
password: String,
|
|
|
|
|
) -> DatabaseResult<UserRecord> {
|
|
|
|
|
let mut connection = self.database_connection.lock().unwrap();
|
|
|
|
|
let mut password = Zeroizing::new(password);
|
|
|
|
|
|
|
|
|
|
if !connection
|
|
|
|
|
.query("SELECT email FROM users WHERE email = $1", &[&email])
|
|
|
|
@ -63,19 +64,105 @@ impl Users {
|
|
|
|
|
{
|
|
|
|
|
return Err(DatabaseError::RecordExists);
|
|
|
|
|
}
|
|
|
|
|
let salt = create_salt(SALT_LENGTH);
|
|
|
|
|
let mut pw_hash = [0u8; 32];
|
|
|
|
|
let salt = Zeroizing::new(create_salt());
|
|
|
|
|
let mut pw_hash = Zeroizing::new([0u8; 32]);
|
|
|
|
|
scrypt::scrypt(
|
|
|
|
|
password.as_bytes(),
|
|
|
|
|
&salt,
|
|
|
|
|
&*salt,
|
|
|
|
|
&ScryptParams::recommended(),
|
|
|
|
|
&mut pw_hash,
|
|
|
|
|
&mut *pw_hash,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| DatabaseError::ScryptError)?;
|
|
|
|
|
password.zeroize();
|
|
|
|
|
let row = connection.query_one("
|
|
|
|
|
INSERT INTO users (name, email, password_hash, salt) VALUES ($1, $2, $3, $4) RETURNING *;
|
|
|
|
|
", &[&name, &email, &pw_hash.to_vec(), &salt.to_vec()]).map_err(|e|DatabaseError::Postgres(e))?;
|
|
|
|
|
|
|
|
|
|
Ok(UserRecord::from_ordered_row(&row))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_token(&self, email: String, password: String) -> DatabaseResult<SessionTokens> {
|
|
|
|
|
if self.validate_login(&email, password)? {
|
|
|
|
|
let mut connection = self.database_connection.lock().unwrap();
|
|
|
|
|
let row = connection
|
|
|
|
|
.query_one("SELECT id FROM users WHERE email = $1", &[&email])
|
|
|
|
|
.map_err(|e| DatabaseError::Postgres(e))?;
|
|
|
|
|
let id: i32 = row.get(0);
|
|
|
|
|
let mut redis_connection = self.redis_connection.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let tokens = SessionTokens::new(id);
|
|
|
|
|
tokens
|
|
|
|
|
.store(&mut redis_connection)
|
|
|
|
|
.map_err(|e| DatabaseError::Redis(e))?;
|
|
|
|
|
|
|
|
|
|
Ok(tokens)
|
|
|
|
|
} else {
|
|
|
|
|
Err(DatabaseError::GenericError("Invalid password".to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn validate_request_token(&self, token: &[u8; TOKEN_LENGTH]) -> DatabaseResult<bool> {
|
|
|
|
|
let id = get_user_id_from_token(token);
|
|
|
|
|
let mut redis_connection = self.redis_connection.lock().unwrap();
|
|
|
|
|
let tokens = SessionTokens::retrieve(id, &mut redis_connection)
|
|
|
|
|
.map_err(|e| DatabaseError::Redis(e))?;
|
|
|
|
|
|
|
|
|
|
Ok(tokens.request_token == *token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn validate_refresh_token(&self, token: &[u8; TOKEN_LENGTH]) -> DatabaseResult<bool> {
|
|
|
|
|
let id = get_user_id_from_token(token);
|
|
|
|
|
let mut redis_connection = self.redis_connection.lock().unwrap();
|
|
|
|
|
let tokens = SessionTokens::retrieve(id, &mut redis_connection)
|
|
|
|
|
.map_err(|e| DatabaseError::Redis(e))?;
|
|
|
|
|
|
|
|
|
|
Ok(tokens.refresh_token == *token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn refresh_tokens(
|
|
|
|
|
&self,
|
|
|
|
|
refresh_token: &[u8; TOKEN_LENGTH],
|
|
|
|
|
) -> DatabaseResult<SessionTokens> {
|
|
|
|
|
let id = get_user_id_from_token(refresh_token);
|
|
|
|
|
let mut redis_connection = self.redis_connection.lock().unwrap();
|
|
|
|
|
let mut tokens = SessionTokens::retrieve(id, &mut redis_connection)
|
|
|
|
|
.map_err(|e| DatabaseError::Redis(e))?;
|
|
|
|
|
|
|
|
|
|
if tokens.refresh_token == *refresh_token {
|
|
|
|
|
tokens.refresh();
|
|
|
|
|
tokens
|
|
|
|
|
.store(&mut redis_connection)
|
|
|
|
|
.map_err(|e| DatabaseError::Redis(e))?;
|
|
|
|
|
|
|
|
|
|
Ok(tokens)
|
|
|
|
|
} else {
|
|
|
|
|
Err(DatabaseError::GenericError(
|
|
|
|
|
"Invalid refresh token!".to_string(),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn validate_login(&self, email: &String, password: String) -> DatabaseResult<bool> {
|
|
|
|
|
let password = Zeroizing::new(password);
|
|
|
|
|
let mut connection = self.database_connection.lock().unwrap();
|
|
|
|
|
let row = connection
|
|
|
|
|
.query_one(
|
|
|
|
|
"SELECT password_hash, salt FROM users WHERE email = $1",
|
|
|
|
|
&[&email],
|
|
|
|
|
)
|
|
|
|
|
.map_err(|e| DatabaseError::Postgres(e))?;
|
|
|
|
|
let original_pw_hash: Zeroizing<Vec<u8>> = Zeroizing::new(row.get(0));
|
|
|
|
|
let salt: Zeroizing<Vec<u8>> = Zeroizing::new(row.get(1));
|
|
|
|
|
let mut pw_hash = Zeroizing::new([0u8; 32]);
|
|
|
|
|
|
|
|
|
|
scrypt::scrypt(
|
|
|
|
|
password.as_bytes(),
|
|
|
|
|
&*salt,
|
|
|
|
|
&ScryptParams::recommended(),
|
|
|
|
|
&mut *pw_hash,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| DatabaseError::ScryptError)?;
|
|
|
|
|
|
|
|
|
|
Ok(*pw_hash == *original_pw_hash.as_slice())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|