summaryrefslogtreecommitdiff
path: root/src/token/app.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/token/app.rs')
-rw-r--r--src/token/app.rs55
1 files changed, 29 insertions, 26 deletions
diff --git a/src/token/app.rs b/src/token/app.rs
index 4a08877..5916e53 100644
--- a/src/token/app.rs
+++ b/src/token/app.rs
@@ -6,11 +6,15 @@ use futures::{
use sqlx::sqlite::SqlitePool;
use super::{
- Broadcaster, Event as TokenEvent, Secret, Token,
- extract::Identity,
- repo::{self, Provider as _},
+ Broadcaster, Event as TokenEvent, Secret, Token, extract::Identity, repo::Provider as _,
+};
+use crate::{
+ clock::DateTime,
+ db,
+ db::NotFound as _,
+ error::failed::{Failed, ResultExt as _},
+ push::repo::Provider as _,
};
-use crate::{clock::DateTime, db::NotFound as _, name, push::repo::Provider as _};
pub struct Tokens {
db: SqlitePool,
@@ -27,13 +31,15 @@ impl Tokens {
secret: &Secret,
used_at: &DateTime,
) -> Result<Identity, ValidateError> {
- let mut tx = self.db.begin().await?;
+ let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?;
let (token, login) = tx
.tokens()
.validate(secret, used_at)
.await
- .not_found(|| ValidateError::InvalidToken)?;
- tx.commit().await?;
+ .optional()
+ .fail("Failed to load token")?
+ .ok_or(ValidateError::InvalidToken)?;
+ tx.commit().await.fail(db::failed::COMMIT)?;
Ok(Identity { token, login })
}
@@ -66,12 +72,14 @@ impl Tokens {
// matter. Supervising a stream, on the other hand, will run for a
// _long_ time; if we miss the race here, we'll never actually carry out the
// supervision.
- let mut tx = self.db.begin().await?;
+ let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?;
tx.tokens()
.require(&token)
.await
- .not_found(|| ValidateError::InvalidToken)?;
- tx.commit().await?;
+ .optional()
+ .fail("Failed to load token")?
+ .ok_or(ValidateError::InvalidToken)?;
+ tx.commit().await.fail(db::failed::COMMIT)?;
// Then construct the guarded stream. First, project both streams into
// `GuardedEvent`.
@@ -111,10 +119,16 @@ impl Tokens {
}
pub async fn logout(&self, token: &Token) -> Result<(), ValidateError> {
- let mut tx = self.db.begin().await?;
- tx.push().unsubscribe_token(token).await?;
- tx.tokens().revoke(token).await?;
- tx.commit().await?;
+ let mut tx = self.db.begin().await.fail(db::failed::BEGIN)?;
+ tx.push()
+ .unsubscribe_token(token)
+ .await
+ .fail("Failed to remove push subscriptions")?;
+ tx.tokens()
+ .revoke(token)
+ .await
+ .fail("Failed to revoke token")?;
+ tx.commit().await.fail(db::failed::COMMIT)?;
self.token_events
.broadcast(TokenEvent::Revoked(token.id.clone()));
@@ -128,18 +142,7 @@ pub enum ValidateError {
#[error("invalid token")]
InvalidToken,
#[error(transparent)]
- Database(#[from] sqlx::Error),
- #[error(transparent)]
- Name(#[from] name::Error),
-}
-
-impl From<repo::LoadError> for ValidateError {
- fn from(error: repo::LoadError) -> Self {
- match error {
- repo::LoadError::Database(error) => error.into(),
- repo::LoadError::Name(error) => error.into(),
- }
- }
+ Failed(#[from] Failed),
}
#[derive(Debug)]