summaryrefslogtreecommitdiff
path: root/src/login/repo/tokens.rs
diff options
context:
space:
mode:
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)
+ }
}