tmtd/src/database.rs

101 lines
3.5 KiB
Rust

/*
* tmtd - Suckless To Do list
* Copyright (C) 2022 C4TG1RL5
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::{task, templates};
use anyhow::anyhow;
use argon2::password_hash::{rand_core::OsRng, SaltString};
use argon2::{password_hash, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions, PgRow};
use sqlx::{ConnectOptions, PgPool};
use sqlx::{Executor, Row};
use tracing::info;
use tracing::log::LevelFilter;
pub struct Database {
pool: PgPool,
argon2: Argon2<'static>,
}
type SqlxResult<T> = Result<T, sqlx::Error>;
impl Database {
pub async fn connect(conn_string: &str) -> anyhow::Result<Self> {
let mut connect_options: PgConnectOptions = conn_string.parse()?;
connect_options.log_statements(LevelFilter::Debug);
info!("Connecting to the database");
let pool = PgPoolOptions::new().connect_with(connect_options).await?;
let mut conn = pool.acquire().await?;
let pgver = conn.server_version_num().map(|v| v.to_string());
info!(
"Database connected, PostgreSQL version '{}', migrating schema",
pgver.as_deref().unwrap_or("unknown")
);
conn.execute(include_str!("sql/schema.sql")).await?;
Ok(Self {
pool,
argon2: Argon2::default(),
})
}
pub async fn close(&self) {
self.pool.close().await;
}
pub async fn create_task(&self, task: &task::CreateTask) {
// TODO: insert the task into the database
}
pub async fn move_task(&self, task: &task::MoveTask) {
// TODO: change the category of the task inside the db
}
pub async fn register(&self, username: &str, password: &str) -> anyhow::Result<()> {
let salt = SaltString::generate(&mut OsRng);
let hash = match self.argon2.hash_password(password.as_bytes(), &salt) {
Ok(o) => o.to_string(),
Err(e) => return Err(anyhow!("Hashing error: {}", e)),
};
let mut conn = self.pool.acquire().await?;
sqlx::query("insert into users(username,hash) values($1,$2)")
.bind(username)
.bind(hash)
.execute(&mut conn)
.await?;
Ok(())
}
pub async fn check_password(&self, username: &str, password: &str) -> anyhow::Result<bool> {
let mut conn = self.pool.acquire().await?;
let phc = sqlx::query("select hash from users where username=$1")
.bind(username)
.map(move |row: PgRow| row.get::<String, _>(0))
.fetch_one(&mut conn)
.await?;
match PasswordHash::new(&phc)
.and_then(|v| self.argon2.verify_password(password.as_bytes(), &v))
{
Ok(_) => Ok(true),
Err(password_hash::Error::Password) => Ok(false),
Err(e) => Err(anyhow!("Hashing error: {}", e)),
}
}
}