Move create_auth_error_handler function to http module
This commit is contained in:
parent
10f2596830
commit
e8ea52adba
3 changed files with 30 additions and 30 deletions
27
src/http.rs
27
src/http.rs
|
@ -1,10 +1,35 @@
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
HttpRequest,
|
body::{BodySize, BoxBody, MessageBody},
|
||||||
|
dev::ServiceResponse,
|
||||||
error::{Error, JsonPayloadError},
|
error::{Error, JsonPayloadError},
|
||||||
|
http::StatusCode,
|
||||||
|
middleware::{ErrorHandlerResponse, ErrorHandlers},
|
||||||
|
HttpRequest,
|
||||||
};
|
};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::errors::HttpError;
|
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(
|
pub fn json_error_handler(
|
||||||
error: JsonPayloadError,
|
error: JsonPayloadError,
|
||||||
_: &HttpRequest,
|
_: &HttpRequest,
|
||||||
|
|
|
@ -15,7 +15,10 @@ use mitra::atom::views as atom;
|
||||||
use mitra::database::{get_database_client, create_pool};
|
use mitra::database::{get_database_client, create_pool};
|
||||||
use mitra::database::migrate::apply_migrations;
|
use mitra::database::migrate::apply_migrations;
|
||||||
use mitra::ethereum::contracts::get_contracts;
|
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::job_queue::scheduler;
|
||||||
use mitra::logger::configure_logger;
|
use mitra::logger::configure_logger;
|
||||||
use mitra::mastodon_api::accounts::views::account_api_scope;
|
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::markers::views::marker_api_scope;
|
||||||
use mitra::mastodon_api::media::views::media_api_scope;
|
use mitra::mastodon_api::media::views::media_api_scope;
|
||||||
use mitra::mastodon_api::notifications::views::notification_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::oauth::views::oauth_api_scope;
|
||||||
use mitra::mastodon_api::search::views::search_api_scope;
|
use mitra::mastodon_api::search::views::search_api_scope;
|
||||||
use mitra::mastodon_api::settings::views::settings_api_scope;
|
use mitra::mastodon_api::settings::views::settings_api_scope;
|
||||||
|
|
|
@ -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::database::{DatabaseClient, DatabaseError};
|
||||||
use crate::errors::HttpError;
|
use crate::errors::HttpError;
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
|
@ -27,22 +19,3 @@ pub async fn get_current_user(
|
||||||
})?;
|
})?;
|
||||||
Ok(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()))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue