Move create_auth_error_handler function to http module

This commit is contained in:
silverpill 2023-02-19 17:10:29 +00:00
parent 10f2596830
commit e8ea52adba
3 changed files with 30 additions and 30 deletions

View file

@ -1,10 +1,35 @@
use actix_web::{
HttpRequest,
body::{BodySize, BoxBody, MessageBody},
dev::ServiceResponse,
error::{Error, JsonPayloadError},
http::StatusCode,
middleware::{ErrorHandlerResponse, ErrorHandlers},
HttpRequest,
};
use serde_json::json;
use crate::errors::HttpError;
/// Error handler for 401 Unauthorized
pub fn create_auth_error_handler<B: MessageBody + 'static>() -> ErrorHandlers<B> {
// Creates and returns actix middleware
ErrorHandlers::new()
.handler(StatusCode::UNAUTHORIZED, |response: ServiceResponse<B>| {
let response_new = response.map_body(|_, body| {
if let BodySize::None | BodySize::Sized(0) = body.size() {
// Insert error description if response body is empty
// https://github.com/actix/actix-extras/issues/156
let error_data = json!({
"message": "auth header is not present",
});
return BoxBody::new(error_data.to_string());
};
body.boxed()
});
Ok(ErrorHandlerResponse::Response(response_new.map_into_right_body()))
})
}
pub fn json_error_handler(
error: JsonPayloadError,
_: &HttpRequest,

View file

@ -15,7 +15,10 @@ use mitra::atom::views as atom;
use mitra::database::{get_database_client, create_pool};
use mitra::database::migrate::apply_migrations;
use mitra::ethereum::contracts::get_contracts;
use mitra::http::json_error_handler;
use mitra::http::{
create_auth_error_handler,
json_error_handler,
};
use mitra::job_queue::scheduler;
use mitra::logger::configure_logger;
use mitra::mastodon_api::accounts::views::account_api_scope;
@ -26,7 +29,6 @@ use mitra::mastodon_api::instance::views::instance_api_scope;
use mitra::mastodon_api::markers::views::marker_api_scope;
use mitra::mastodon_api::media::views::media_api_scope;
use mitra::mastodon_api::notifications::views::notification_api_scope;
use mitra::mastodon_api::oauth::auth::create_auth_error_handler;
use mitra::mastodon_api::oauth::views::oauth_api_scope;
use mitra::mastodon_api::search::views::search_api_scope;
use mitra::mastodon_api::settings::views::settings_api_scope;

View file

@ -1,11 +1,3 @@
use actix_web::{
body::{BodySize, BoxBody, MessageBody},
dev::ServiceResponse,
http::StatusCode,
middleware::{ErrorHandlerResponse, ErrorHandlers},
};
use serde_json::json;
use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::HttpError;
use crate::models::{
@ -27,22 +19,3 @@ pub async fn get_current_user(
})?;
Ok(user)
}
/// Error handler for 401 Unauthorized
pub fn create_auth_error_handler<B: MessageBody + 'static>() -> ErrorHandlers<B> {
ErrorHandlers::new()
.handler(StatusCode::UNAUTHORIZED, |response: ServiceResponse<B>| {
let response_new = response.map_body(|_, body| {
if let BodySize::None | BodySize::Sized(0) = body.size() {
// Insert error description if response body is empty
// https://github.com/actix/actix-extras/issues/156
let error_data = json!({
"message": "auth header is not present",
});
return BoxBody::new(error_data.to_string());
};
body.boxed()
});
Ok(ErrorHandlerResponse::Response(response_new.map_into_right_body()))
})
}