summaryrefslogtreecommitdiff
path: root/src/login/repo.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-21 00:36:44 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-22 10:58:11 -0400
commit3f9648eed48cd8b6cd35d0ae2ee5bbe25fa735ac (patch)
tree8ecdd86cd9e09d8a3bd55ec1f72888a81498cc38 /src/login/repo.rs
parent379e97c2cb145bc3a495aa14746273d83b508214 (diff)
Canonicalize login and channel names.
Canonicalization does two things: * It prevents duplicate names that differ only by case or only by normalization/encoding sequence; and * It makes certain name-based comparisons "case-insensitive" (generalizing via Unicode's case-folding rules). This change is complicated, as it means that every name now needs to be stored in two forms. Unfortunately, this is _very likely_ a breaking schema change. The migrations in this commit perform a best-effort attempt to canonicalize existing channel or login names, but it's likely any existing channels or logins with non-ASCII characters will not be canonicalize correctly. Since clients look at all channel names and all login names on boot, and since the code in this commit verifies canonicalization when reading from the database, this will effectively make the server un-usuable until any incorrectly-canonicalized values are either manually canonicalized, or removed It might be possible to do better with [the `icu` sqlite3 extension][icu], but (a) I'm not convinced of that and (b) this commit is already huge; adding database extension support would make it far larger. [icu]: https://sqlite.org/src/dir/ext/icu For some references on why it's worth storing usernames this way, see <https://www.b-list.org/weblog/2018/nov/26/case/> and the refernced talk, as well as <https://www.b-list.org/weblog/2018/feb/11/usernames/>. Bennett's treatment of this issue is, to my eye, much more readable than the referenced Unicode technical reports, and I'm inclined to trust his opinion given that he maintains a widely-used, internet-facing user registration library for Django.
Diffstat (limited to 'src/login/repo.rs')
-rw-r--r--src/login/repo.rs98
1 files changed, 55 insertions, 43 deletions
diff --git a/src/login/repo.rs b/src/login/repo.rs
index 204329f..6021f26 100644
--- a/src/login/repo.rs
+++ b/src/login/repo.rs
@@ -1,9 +1,11 @@
+use futures::stream::{StreamExt as _, TryStreamExt as _};
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use crate::{
clock::DateTime,
event::{Instant, ResumePoint, Sequence},
- login::{password::StoredHash, History, Id, Login, Name},
+ login::{password::StoredHash, History, Id, Login},
+ name::{self, Name},
};
pub trait Provider {
@@ -26,43 +28,43 @@ impl<'c> Logins<'c> {
created: &Instant,
) -> Result<History, sqlx::Error> {
let id = Id::generate();
+ let display_name = name.display();
+ let canonical_name = name.canonical();
- let login = sqlx::query!(
+ sqlx::query!(
r#"
insert
- into login (id, name, password_hash, created_sequence, created_at)
- values ($1, $2, $3, $4, $5)
- returning
- id as "id: Id",
- name as "name: Name",
- created_sequence as "created_sequence: Sequence",
- created_at as "created_at: DateTime"
+ into login (id, display_name, canonical_name, password_hash, created_sequence, created_at)
+ values ($1, $2, $3, $4, $5, $6)
"#,
id,
- name,
+ display_name,
+ canonical_name,
password_hash,
created.sequence,
created.at,
)
- .map(|row| History {
+ .execute(&mut *self.0)
+ .await?;
+
+ let login = History {
+ created: *created,
login: Login {
- id: row.id,
- name: row.name,
+ id,
+ name: name.clone(),
},
- created: Instant::new(row.created_at, row.created_sequence),
- })
- .fetch_one(&mut *self.0)
- .await?;
+ };
Ok(login)
}
- pub async fn all(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, sqlx::Error> {
- let channels = sqlx::query!(
+ pub async fn all(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, LoadError> {
+ let logins = sqlx::query!(
r#"
select
id as "id: Id",
- name as "name: Name",
+ display_name as "display_name: String",
+ canonical_name as "canonical_name: String",
created_sequence as "created_sequence: Sequence",
created_at as "created_at: DateTime"
from login
@@ -71,24 +73,29 @@ impl<'c> Logins<'c> {
"#,
resume_at,
)
- .map(|row| History {
- login: Login {
- id: row.id,
- name: row.name,
- },
- created: Instant::new(row.created_at, row.created_sequence),
+ .map(|row| {
+ Ok::<_, LoadError>(History {
+ login: Login {
+ id: row.id,
+ name: Name::new(row.display_name, row.canonical_name)?,
+ },
+ created: Instant::new(row.created_at, row.created_sequence),
+ })
})
- .fetch_all(&mut *self.0)
+ .fetch(&mut *self.0)
+ .map(|res| res?)
+ .try_collect()
.await?;
- Ok(channels)
+ Ok(logins)
}
- pub async fn replay(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, sqlx::Error> {
- let messages = sqlx::query!(
+ pub async fn replay(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, LoadError> {
+ let logins = sqlx::query!(
r#"
select
id as "id: Id",
- name as "name: Name",
+ display_name as "display_name: String",
+ canonical_name as "canonical_name: String",
created_sequence as "created_sequence: Sequence",
created_at as "created_at: DateTime"
from login
@@ -96,22 +103,27 @@ impl<'c> Logins<'c> {
"#,
resume_at,
)
- .map(|row| History {
- login: Login {
- id: row.id,
- name: row.name,
- },
- created: Instant::new(row.created_at, row.created_sequence),
+ .map(|row| {
+ Ok::<_, name::Error>(History {
+ login: Login {
+ id: row.id,
+ name: Name::new(row.display_name, row.canonical_name)?,
+ },
+ created: Instant::new(row.created_at, row.created_sequence),
+ })
})
- .fetch_all(&mut *self.0)
+ .fetch(&mut *self.0)
+ .map(|res| Ok::<_, LoadError>(res??))
+ .try_collect()
.await?;
- Ok(messages)
+ Ok(logins)
}
}
-impl<'t> From<&'t mut SqliteConnection> for Logins<'t> {
- fn from(tx: &'t mut SqliteConnection) -> Self {
- Self(tx)
- }
+#[derive(Debug, thiserror::Error)]
+#[error(transparent)]
+pub enum LoadError {
+ Database(#[from] sqlx::Error),
+ Name(#[from] name::Error),
}