Skip to content

Commit

Permalink
refactor(actix-web-grants): GrantsMiddleware returns EitherBody (#64
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DDtKey authored Dec 8, 2023
1 parent 8b2250f commit 4c92341
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
22 changes: 13 additions & 9 deletions actix-web-grants/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::authorities::AttachAuthorities;
use crate::authorities::AuthoritiesExtractor;
use actix_web::body::EitherBody;
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::Error;
use std::collections::HashSet;
use std::future::{self, Future, Ready};
use std::hash::Hash;
use std::marker::PhantomData;
Expand Down Expand Up @@ -106,7 +106,7 @@ where
for<'a> E: AuthoritiesExtractor<'a, Req, Type> + 'static,
Type: Eq + Hash + 'static,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse<EitherBody<B>>;
type Error = Error;
type Transform = GrantsService<S, E, Req, Type>;
type InitError = ();
Expand Down Expand Up @@ -138,9 +138,10 @@ where
for<'a> E: AuthoritiesExtractor<'a, Req, Type>,
Type: Eq + Hash + 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Error>>>>;
type Response = ServiceResponse<EitherBody<B>>;
type Error = S::Error;
type Future =
Pin<Box<dyn Future<Output = Result<ServiceResponse<EitherBody<B>>, Self::Error>>>>;

fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
Expand All @@ -151,10 +152,13 @@ where
let extractor = Rc::clone(&self.extractor);

Box::pin(async move {
let authorities: HashSet<Type> = extractor.extract(&mut req).await?;
req.attach(authorities);

service.call(req).await
match extractor.extract(&mut req).await {
Ok(authorities) => {
req.attach(authorities);
Ok(service.call(req).await?.map_into_left_body())
}
Err(err) => Ok(req.error_response(err).map_into_right_body()),
}
})
}
}
8 changes: 6 additions & 2 deletions actix-web-grants/tests/authorities_check/guard_check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::ServiceResponse;
use actix_web::{test, web, App, HttpResponse};

Expand All @@ -23,7 +24,7 @@ async fn test_enum_guard() {
assert_eq!(StatusCode::NOT_FOUND, test_manager.status());
}

async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response(uri: &str, role: &str) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(common::extract))
Expand All @@ -42,7 +43,10 @@ async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
test::call_service(&app, req).await
}

async fn get_user_response_with_enum(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response_with_enum(
uri: &str,
role: &str,
) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::ServiceResponse;
use actix_web::{get, test, App, HttpResponse};

Expand Down Expand Up @@ -43,7 +44,7 @@ async fn test_forbidden() {
assert_eq!(StatusCode::FORBIDDEN, test_manager.status());
}

async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response(uri: &str, role: &str) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::ServiceResponse;
use actix_web::{get, test, App, HttpResponse};

Expand Down Expand Up @@ -43,7 +44,7 @@ async fn test_forbidden() {
assert_eq!(StatusCode::FORBIDDEN, test_manager.status());
}

async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response(uri: &str, role: &str) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(common::extract))
Expand Down
5 changes: 4 additions & 1 deletion actix-web-grants/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub async fn enum_extract<T: FromStr + Eq + Hash>(
.ok_or_else(|| ErrorUnauthorized("Authorization header incorrect!"))
}

pub async fn test_body(resp: ServiceResponse, expected_body: &str) {
pub async fn test_body<B: actix_web::body::MessageBody>(
resp: ServiceResponse<B>,
expected_body: &str,
) {
let body = test::read_body(resp).await;

assert_eq!(String::from_utf8(body.to_vec()).unwrap(), expected_body);
Expand Down
10 changes: 7 additions & 3 deletions actix-web-grants/tests/proc_macro/different_fn_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::common::{self, ROLE_ADMIN, ROLE_MANAGER};
use actix_web::body::BoxBody;
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::ServiceResponse;
use actix_web::error::ErrorBadRequest;
use actix_web::http::{header::AUTHORIZATION, StatusCode};
Expand Down Expand Up @@ -123,7 +123,7 @@ async fn test_access_denied_reason() {
common::test_body(test_manager, "This resource allowed only for ADMIN").await;
}

async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response(uri: &str, role: &str) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(common::extract))
Expand All @@ -142,7 +142,11 @@ async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
test::call_service(&app, req).await
}

async fn post_user_response<T: Serialize>(uri: &str, role: &str, data: &T) -> ServiceResponse {
async fn post_user_response<T: Serialize>(
uri: &str,
role: &str,
data: &T,
) -> ServiceResponse<EitherBody<BoxBody>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(common::extract))
Expand Down
6 changes: 5 additions & 1 deletion actix-web-grants/tests/proc_macro/type_feature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::common;
use crate::common::Permission;
use crate::common::Role::{self, ADMIN, MANAGER};
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::ServiceResponse;
use actix_web::http::header::AUTHORIZATION;
use actix_web::http::StatusCode;
Expand Down Expand Up @@ -76,7 +77,10 @@ async fn test_incorrect_http_response() {
assert_eq!(StatusCode::UNAUTHORIZED, test.status());
}

async fn get_user_response(uri: &str, role: &str) -> ServiceResponse {
async fn get_user_response(
uri: &str,
role: &str,
) -> ServiceResponse<EitherBody<EitherBody<BoxBody>>> {
let app = test::init_service(
App::new()
.wrap(GrantsMiddleware::with_extractor(
Expand Down

0 comments on commit 4c92341

Please sign in to comment.