summaryrefslogtreecommitdiff
path: root/src/token/repo/auth.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-02 01:02:58 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-02 01:02:58 -0400
commit5d3392799f88c5a3d3f9c656c73d6e8ac5c4d793 (patch)
tree426c568d82b67a98095d25952d2b5b2345a6545b /src/token/repo/auth.rs
parent357116366c1307bedaac6a3dfe9c5ed8e0e0c210 (diff)
Split login and token handling.
Diffstat (limited to 'src/token/repo/auth.rs')
-rw-r--r--src/token/repo/auth.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/token/repo/auth.rs b/src/token/repo/auth.rs
new file mode 100644
index 0000000..b299697
--- /dev/null
+++ b/src/token/repo/auth.rs
@@ -0,0 +1,50 @@
+use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
+
+use crate::login::{self, password::StoredHash, Login};
+
+pub trait Provider {
+ fn auth(&mut self) -> Auth;
+}
+
+impl<'c> Provider for Transaction<'c, Sqlite> {
+ fn auth(&mut self) -> Auth {
+ Auth(self)
+ }
+}
+
+pub struct Auth<'t>(&'t mut SqliteConnection);
+
+impl<'t> Auth<'t> {
+ // Retrieves a login by name, plus its stored password hash for
+ // verification. If there's no login with the requested name, this will
+ // return [None].
+ pub async fn for_name(
+ &mut self,
+ name: &str,
+ ) -> Result<Option<(Login, StoredHash)>, sqlx::Error> {
+ let found = sqlx::query!(
+ r#"
+ select
+ id as "id: login::Id",
+ name,
+ password_hash as "password_hash: StoredHash"
+ from login
+ where name = $1
+ "#,
+ name,
+ )
+ .map(|rec| {
+ (
+ Login {
+ id: rec.id,
+ name: rec.name,
+ },
+ rec.password_hash,
+ )
+ })
+ .fetch_optional(&mut *self.0)
+ .await?;
+
+ Ok(found)
+ }
+}