From 4fd66b2719b9ce277ddb44bb2587359c71bbf90f Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sun, 29 Sep 2024 12:02:10 +0100 Subject: [PATCH] refactor(core/result): add NotAuthenticated error which is distinct from InvalidCredentials --- crates/core/database/src/models/channel_webhooks/model.rs | 2 +- crates/core/database/src/models/users/axum.rs | 2 +- crates/core/result/src/axum.rs | 1 + crates/core/result/src/lib.rs | 1 + crates/core/result/src/rocket.rs | 1 + 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/core/database/src/models/channel_webhooks/model.rs b/crates/core/database/src/models/channel_webhooks/model.rs index 568326d6f..db3a46517 100644 --- a/crates/core/database/src/models/channel_webhooks/model.rs +++ b/crates/core/database/src/models/channel_webhooks/model.rs @@ -70,7 +70,7 @@ impl Webhook { if self.token.as_deref() == Some(token) { Ok(()) } else { - Err(create_error!(InvalidCredentials)) + Err(create_error!(NotAuthenticated)) } } diff --git a/crates/core/database/src/models/users/axum.rs b/crates/core/database/src/models/users/axum.rs index da108a452..da83c5b13 100644 --- a/crates/core/database/src/models/users/axum.rs +++ b/crates/core/database/src/models/users/axum.rs @@ -18,7 +18,7 @@ impl FromRequestParts for User { let session = db.fetch_session_by_token(session_token).await?; db.fetch_user(&session.user_id).await } else { - Err(create_error!(InvalidCredentials)) + Err(create_error!(NotAuthenticated)) } } } diff --git a/crates/core/result/src/axum.rs b/crates/core/result/src/axum.rs index 29c528752..11f1fa1ce 100644 --- a/crates/core/result/src/axum.rs +++ b/crates/core/result/src/axum.rs @@ -67,6 +67,7 @@ impl IntoResponse for Error { ErrorType::InvalidCredentials => StatusCode::UNAUTHORIZED, ErrorType::InvalidProperty => StatusCode::BAD_REQUEST, ErrorType::InvalidSession => StatusCode::UNAUTHORIZED, + ErrorType::NotAuthenticated => StatusCode::UNAUTHORIZED, ErrorType::DuplicateNonce => StatusCode::CONFLICT, ErrorType::VosoUnavailable => StatusCode::BAD_REQUEST, ErrorType::NotFound => StatusCode::NOT_FOUND, diff --git a/crates/core/result/src/lib.rs b/crates/core/result/src/lib.rs index d5d093c7e..06ef7810e 100644 --- a/crates/core/result/src/lib.rs +++ b/crates/core/result/src/lib.rs @@ -147,6 +147,7 @@ pub enum ErrorType { InvalidCredentials, InvalidProperty, InvalidSession, + NotAuthenticated, DuplicateNonce, NotFound, NoEffect, diff --git a/crates/core/result/src/rocket.rs b/crates/core/result/src/rocket.rs index 2b3a3fb2f..6efcb663c 100644 --- a/crates/core/result/src/rocket.rs +++ b/crates/core/result/src/rocket.rs @@ -73,6 +73,7 @@ impl<'r> Responder<'r, 'static> for Error { ErrorType::InvalidCredentials => Status::Unauthorized, ErrorType::InvalidProperty => Status::BadRequest, ErrorType::InvalidSession => Status::Unauthorized, + ErrorType::NotAuthenticated => Status::Unauthorized, ErrorType::DuplicateNonce => Status::Conflict, ErrorType::VosoUnavailable => Status::BadRequest, ErrorType::NotFound => Status::NotFound,