-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00071dc
commit 6e32dc7
Showing
15 changed files
with
248 additions
and
14 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
.sqlx/query-93e279636ac192dab72029772a955514d52554504de5597b4073a10396d42502.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
.sqlx/query-963703da1e5f47214d66a90c092922ff76ce51505613ce11c773461794ce6c55.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
create table users ( | ||
id uuid primary key default gen_random_uuid() not null | ||
id uuid primary key default gen_random_uuid() not null, | ||
password_hash text not null, | ||
username text not null | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use anyhow::anyhow; | ||
use sqlx::prelude::FromRow; | ||
use sqlx::{query, query_as, Postgres, Transaction}; | ||
use uuid::Uuid; | ||
|
||
use crate::app_error::{AppError, Result}; | ||
use crate::schemas::users::CreateUser; | ||
|
||
#[derive(FromRow, Debug)] | ||
pub struct User { | ||
pub id: Uuid, | ||
pub username: String, | ||
pub password_hash: String, | ||
} | ||
|
||
pub async fn insert(db: &mut Transaction<'_, Postgres>, create: CreateUser) -> Result<()> { | ||
let hashed_password = hash_password(create.password)?; | ||
|
||
query!( | ||
r#" | ||
insert into users | ||
(password_hash, username) | ||
values ($1, $2)"#, | ||
hashed_password, | ||
create.username | ||
) | ||
.execute(&mut **db) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn by_username(db: &mut Transaction<'_, Postgres>, username: String) -> Result<User> { | ||
let user = query_as!( | ||
User, | ||
r#" | ||
select * from users | ||
where username = $1 | ||
"#, | ||
username | ||
) | ||
.fetch_one(&mut **db) | ||
.await?; | ||
|
||
Ok(user) | ||
} | ||
|
||
pub async fn create_user_if_not_exists( | ||
tx: &mut Transaction<'_, Postgres>, | ||
create: CreateUser, | ||
) -> Result<()> { | ||
let username = create.username.clone(); | ||
tracing::info!("Checking if admin user '{username}' exists..."); | ||
let user = by_username(tx, username).await; | ||
match user { | ||
Err(AppError::NotFound()) => { | ||
tracing::info!("Creating admin user"); | ||
insert(tx, create).await?; | ||
} | ||
Ok(_) => { | ||
tracing::info!("User already exists") | ||
} | ||
Err(other) => return Err(other), | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn hash_password(password: String) -> Result<String> { | ||
let salt = | ||
argon2::password_hash::SaltString::generate(&mut argon2::password_hash::rand_core::OsRng); | ||
|
||
let argon2 = argon2::Argon2::default(); | ||
|
||
Ok( | ||
argon2::PasswordHasher::hash_password(&argon2, password.as_bytes(), &salt) | ||
.map_err(|e| anyhow!("Failed to hash password: {e}"))? | ||
.to_string(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ mod app_error; | |
pub mod cli; | ||
mod db; | ||
mod routes; | ||
mod schemas; | ||
pub mod server; |
Oops, something went wrong.