guest password protect

This commit is contained in:
Mateusz Gruszczyński
2026-08-04 16:41:45 +02:00
parent 8d58549d11
commit 6fc408ddf7
18 changed files with 527 additions and 43 deletions
+36
View File
@@ -52,6 +52,7 @@ pub struct Workspace {
pub created_at: String,
pub updated_at: String,
pub is_private: i64,
pub created_by_guest_id: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
@@ -142,6 +143,7 @@ pub async fn create_workspace(
slug: &str,
title: &str,
password: Option<&str>,
created_by_guest_id: Option<&str>,
) -> Result<Workspace, sqlx::Error> {
let password_hash = password
.filter(|value| !value.is_empty())
@@ -150,6 +152,7 @@ pub async fn create_workspace(
.bind(slug)
.bind(title)
.bind(password_hash)
.bind(created_by_guest_id)
.execute(pool.pool())
.await?;
@@ -159,6 +162,23 @@ pub async fn create_workspace(
.await
}
pub async fn set_workspace_password(
pool: &Database,
slug: &str,
password: &str,
) -> Result<(), sqlx::Error> {
let password_hash = hash_password(password);
sqlx::query(queries::get(
pool.kind(),
queries::USER_SET_WORKSPACE_PASSWORD,
))
.bind(password_hash)
.bind(slug)
.execute(pool.pool())
.await?;
Ok(())
}
pub fn verify_workspace_password(workspace: &Workspace, password: Option<&str>) -> bool {
match (
&workspace.password_hash,
@@ -496,6 +516,20 @@ pub async fn create_pad(
.await
}
pub async fn set_pad_password(
pool: &Database,
slug: &str,
password: &str,
) -> Result<(), sqlx::Error> {
let password_hash = hash_password(password);
sqlx::query(queries::get(pool.kind(), queries::USER_SET_PAD_PASSWORD))
.bind(password_hash)
.bind(slug)
.execute(pool.pool())
.await?;
Ok(())
}
pub fn verify_pad_password(pad: &Pad, password: Option<&str>) -> bool {
match (
&pad.password_hash,
@@ -631,6 +665,7 @@ impl<'r> sqlx::FromRow<'r, AnyRow> for Workspace {
created_at: crate::row_decode::text(row, "created_at")?,
updated_at: crate::row_decode::text(row, "updated_at")?,
is_private: row.try_get("is_private")?,
created_by_guest_id: crate::row_decode::optional_text(row, "created_by_guest_id")?,
})
}
}
@@ -692,6 +727,7 @@ mod password_verification_tests {
created_at: String::new(),
updated_at: String::new(),
is_private: 1,
created_by_guest_id: None,
}
}