diff options
Diffstat (limited to 'src/token')
| -rw-r--r-- | src/token/app.rs | 55 | ||||
| -rw-r--r-- | src/token/extract/identity.rs | 4 | ||||
| -rw-r--r-- | src/token/repo/mod.rs | 2 |
3 files changed, 32 insertions, 29 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)] diff --git a/src/token/extract/identity.rs b/src/token/extract/identity.rs index 5c004ef..59f1dfb 100644 --- a/src/token/extract/identity.rs +++ b/src/token/extract/identity.rs @@ -40,7 +40,7 @@ where .await .map_err(|err| match err { ValidateError::InvalidToken => LoginError::Unauthorized, - other => other.into(), + ValidateError::Failed(failed) => failed.into(), }) } } @@ -59,7 +59,7 @@ where match <Self as FromRequestParts<App>>::from_request_parts(parts, state).await { Ok(identity) => Ok(Some(identity)), Err(LoginError::Unauthorized) => Ok(None), - Err(other) => Err(other), + Err(LoginError::Failure(other)) => Err(LoginError::Failure(other)), } } } diff --git a/src/token/repo/mod.rs b/src/token/repo/mod.rs index 9df5bbb..50d9147 100644 --- a/src/token/repo/mod.rs +++ b/src/token/repo/mod.rs @@ -1,3 +1,3 @@ mod token; -pub use self::token::{LoadError, Provider}; +pub use self::token::Provider; |
