summaryrefslogtreecommitdiff
path: root/src/login/repo/tokens.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-09-04 00:28:35 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-09-04 00:28:35 -0400
commit2965a788cfcf4a0386cb8832e0d96491bf54c1d3 (patch)
tree096b00f64b092396e99d04ebe124fa15d734f6c1 /src/login/repo/tokens.rs
parent289e99ba977ebe6c4599141bc368c17f9905ffcc (diff)
Display a different / page depending on whether the current identity is valid or not.
This is mostly a proof of concept for the implementation of form login implemented in previous commits, but it _is_ useful as it controls whether the / page shows login, or shows logout. From here, chat is next!
Diffstat (limited to 'src/login/repo/tokens.rs')
-rw-r--r--src/login/repo/tokens.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/login/repo/tokens.rs b/src/login/repo/tokens.rs
index e31a301..584f6dc 100644
--- a/src/login/repo/tokens.rs
+++ b/src/login/repo/tokens.rs
@@ -1,7 +1,7 @@
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use uuid::Uuid;
-use super::logins::Id as LoginId;
+use super::logins::{Id as LoginId, Login};
use crate::error::BoxedError;
type DateTime = chrono::DateTime<chrono::Utc>;
@@ -45,18 +45,41 @@ impl<'c> Tokens<'c> {
Ok(secret)
}
- pub async fn revoke(&mut self, token: &str) -> Result<(), BoxedError> {
+ /// Revoke a token by its secret. If there is no such token with that
+ /// secret, this will succeed by doing nothing.
+ pub async fn revoke(&mut self, secret: &str) -> Result<(), BoxedError> {
sqlx::query!(
r#"
delete
from token
where secret = $1
"#,
- token,
+ secret,
)
.execute(&mut *self.0)
.await?;
Ok(())
}
+
+ /// Validate a token by its secret, retrieving the associated Login record.
+ /// Will return [None] if the token is not valid.
+ pub async fn validate(&mut self, secret: &str) -> Result<Option<Login>, BoxedError> {
+ let login = sqlx::query_as!(
+ Login,
+ r#"
+ select
+ login.id as "id: LoginId",
+ name
+ from login
+ join token on login.id = token.login
+ where token.secret = $1
+ "#,
+ secret,
+ )
+ .fetch_optional(&mut *self.0)
+ .await?;
+
+ Ok(login)
+ }
}