diff --git a/actix_web_httpauth/middleware/index.html b/actix_web_httpauth/middleware/index.html index aff7f3a83..21f2aaa7b 100644 --- a/actix_web_httpauth/middleware/index.html +++ b/actix_web_httpauth/middleware/index.html @@ -5,7 +5,7 @@

Module middleware

Expand description

HTTP Authentication middleware.

+

Module actix_web_httpauth::middleware

source · []
Expand description

HTTP Authentication middleware.

Structs

Middleware for checking HTTP authentication.

diff --git a/actix_web_httpauth/middleware/struct.HttpAuthentication.html b/actix_web_httpauth/middleware/struct.HttpAuthentication.html index 88ec76997..889ce85bd 100644 --- a/actix_web_httpauth/middleware/struct.HttpAuthentication.html +++ b/actix_web_httpauth/middleware/struct.HttpAuthentication.html @@ -10,9 +10,9 @@ immediately, without calling the F callback.

Otherwise, it will pass both the request and the parsed credentials into it. In case of successful validation F callback is required to return the ServiceRequest back.

-

Implementations

Construct HttpAuthentication middleware with the provided auth extractor T and +

Implementations

Construct HttpAuthentication middleware with the provided auth extractor T and validation callback F.

-

Construct HttpAuthentication middleware for the HTTP “Basic” authentication scheme.

+

Construct HttpAuthentication middleware for the HTTP “Basic” authentication scheme.

Example
// In this example validator returns immediately, but since it is required to return
 // anything that implements `IntoFuture` trait, it can be extended to query database or to
@@ -20,15 +20,15 @@ validation callback F.

async fn validator( req: ServiceRequest, credentials: BasicAuth, -) -> Result<ServiceRequest, Error> { +) -> Result<ServiceRequest, (Error, ServiceRequest)> { // All users are great and more than welcome! Ok(req) } let middleware = HttpAuthentication::basic(validator);
-

Construct HttpAuthentication middleware for the HTTP “Bearer” authentication scheme.

+

Construct HttpAuthentication middleware for the HTTP “Bearer” authentication scheme.

Example
-
async fn validator(req: ServiceRequest, credentials: BearerAuth) -> Result<ServiceRequest, Error> {
+
async fn validator(req: ServiceRequest, credentials: BearerAuth) -> Result<ServiceRequest, (Error, ServiceRequest)> {
     if credentials.token() == "mF_9.B5f-4.1JqM" {
         Ok(req)
     } else {
@@ -37,7 +37,7 @@ validation callback F.

.unwrap_or_else(Default::default) .scope("urn:example:channel=HBO&urn:example:rating=G,PG-13"); - Err(AuthenticationError::from(config).into()) + Err((AuthenticationError::from(config).into(), req)) } } @@ -45,7 +45,7 @@ validation callback F.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

-

Responses produced by the service.

+

Responses produced by the service.

Errors produced by the service.

The TransformService value created by this factory

Errors produced while building a transform service.

diff --git a/implementors/actix_service/transform/trait.Transform.js b/implementors/actix_service/transform/trait.Transform.js index 4f835ba52..25e9067fa 100644 --- a/implementors/actix_service/transform/trait.Transform.js +++ b/implementors/actix_service/transform/trait.Transform.js @@ -3,5 +3,5 @@ implementors["actix_cors"] = [{"text":"impl<S, B> Transform<S, ServiceR implementors["actix_identity"] = [{"text":"impl<S, B> Transform<S, ServiceRequest> for IdentityMiddleware where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: MessageBody + 'static, 
","synthetic":false,"types":["actix_identity::middleware::IdentityMiddleware"]}]; implementors["actix_limitation"] = [{"text":"impl<S, B> Transform<S, ServiceRequest> for RateLimiter where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: 'static, 
","synthetic":false,"types":["actix_limitation::middleware::RateLimiter"]}]; implementors["actix_session"] = [{"text":"impl<S, B, Store> Transform<S, ServiceRequest> for SessionMiddleware<Store> where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: MessageBody + 'static,
    Store: SessionStore + 'static, 
","synthetic":false,"types":["actix_session::middleware::SessionMiddleware"]}]; -implementors["actix_web_httpauth"] = [{"text":"impl<S, B, T, F, O> Transform<S, ServiceRequest> for HttpAuthentication<T, F> where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    F: Fn(ServiceRequest, T) -> O + 'static,
    O: Future<Output = Result<ServiceRequest, Error>> + 'static,
    T: AuthExtractor + 'static,
    B: MessageBody + 'static, 
","synthetic":false,"types":["actix_web_httpauth::middleware::HttpAuthentication"]}]; +implementors["actix_web_httpauth"] = [{"text":"impl<S, B, T, F, O> Transform<S, ServiceRequest> for HttpAuthentication<T, F> where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    F: Fn(ServiceRequest, T) -> O + 'static,
    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>> + 'static,
    T: AuthExtractor + 'static,
    B: MessageBody + 'static, 
","synthetic":false,"types":["actix_web_httpauth::middleware::HttpAuthentication"]}]; if (window.register_implementors) {window.register_implementors(implementors);} else {window.pending_implementors = implementors;}})() \ No newline at end of file diff --git a/src/actix_web_httpauth/middleware.rs.html b/src/actix_web_httpauth/middleware.rs.html index bd3449b7c..d18923f89 100644 --- a/src/actix_web_httpauth/middleware.rs.html +++ b/src/actix_web_httpauth/middleware.rs.html @@ -417,6 +417,9 @@ 411 412 413 +414 +415 +416
//! HTTP Authentication middleware.
 
 use std::{
@@ -458,7 +461,7 @@
 where
     T: AuthExtractor,
     F: Fn(ServiceRequest, T) -> O,
-    O: Future<Output = Result<ServiceRequest, Error>>,
+    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>>,
 {
     /// Construct `HttpAuthentication` middleware with the provided auth extractor `T` and
     /// validation callback `F`.
@@ -473,7 +476,7 @@
 impl<F, O> HttpAuthentication<basic::BasicAuth, F>
 where
     F: Fn(ServiceRequest, basic::BasicAuth) -> O,
-    O: Future<Output = Result<ServiceRequest, Error>>,
+    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>>,
 {
     /// Construct `HttpAuthentication` middleware for the HTTP "Basic" authentication scheme.
     ///
@@ -489,7 +492,7 @@
     /// async fn validator(
     ///     req: ServiceRequest,
     ///     credentials: BasicAuth,
-    /// ) -> Result<ServiceRequest, Error> {
+    /// ) -> Result<ServiceRequest, (Error, ServiceRequest)> {
     ///     // All users are great and more than welcome!
     ///     Ok(req)
     /// }
@@ -504,7 +507,7 @@
 impl<F, O> HttpAuthentication<bearer::BearerAuth, F>
 where
     F: Fn(ServiceRequest, bearer::BearerAuth) -> O,
-    O: Future<Output = Result<ServiceRequest, Error>>,
+    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>>,
 {
     /// Construct `HttpAuthentication` middleware for the HTTP "Bearer" authentication scheme.
     ///
@@ -515,7 +518,7 @@
     /// # use actix_web_httpauth::middleware::HttpAuthentication;
     /// # use actix_web_httpauth::extractors::bearer::{Config, BearerAuth};
     /// # use actix_web_httpauth::extractors::{AuthenticationError, AuthExtractorConfig};
-    /// async fn validator(req: ServiceRequest, credentials: BearerAuth) -> Result<ServiceRequest, Error> {
+    /// async fn validator(req: ServiceRequest, credentials: BearerAuth) -> Result<ServiceRequest, (Error, ServiceRequest)> {
     ///     if credentials.token() == "mF_9.B5f-4.1JqM" {
     ///         Ok(req)
     ///     } else {
@@ -524,7 +527,7 @@
     ///             .unwrap_or_else(Default::default)
     ///             .scope("urn:example:channel=HBO&urn:example:rating=G,PG-13");
     ///
-    ///         Err(AuthenticationError::from(config).into())
+    ///         Err((AuthenticationError::from(config).into(), req))
     ///     }
     /// }
     ///
@@ -540,7 +543,7 @@
     S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
     S::Future: 'static,
     F: Fn(ServiceRequest, T) -> O + 'static,
-    O: Future<Output = Result<ServiceRequest, Error>> + 'static,
+    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>> + 'static,
     T: AuthExtractor + 'static,
     B: MessageBody + 'static,
 {
@@ -574,7 +577,7 @@
     S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
     S::Future: 'static,
     F: Fn(ServiceRequest, T) -> O + 'static,
-    O: Future<Output = Result<ServiceRequest, Error>> + 'static,
+    O: Future<Output = Result<ServiceRequest, (Error, ServiceRequest)>> + 'static,
     T: AuthExtractor + 'static,
     B: MessageBody + 'static,
 {
@@ -597,9 +600,12 @@
                 }
             };
 
-            // TODO: alter to remove ? operator; an error response is required for downstream
-            // middleware to do their thing (eg. cors adding headers)
-            let req = process_fn(req, credentials).await?;
+            let req = match process_fn(req, credentials).await {
+                Ok(req) => req,
+                Err((err, req)) => {
+                    return Ok(req.error_response(err).map_into_right_body());
+                }
+            };
 
             service.call(req).await.map(|res| res.map_into_left_body())
         }
@@ -781,10 +787,10 @@
     #[actix_web::test]
     async fn test_middleware_works_with_app() {
         async fn validator(
-            _req: ServiceRequest,
+            req: ServiceRequest,
             _credentials: BasicAuth,
-        ) -> Result<ServiceRequest, actix_web::Error> {
-            Err(ErrorForbidden("You are not welcome!"))
+        ) -> Result<ServiceRequest, (actix_web::Error, ServiceRequest)> {
+            Err((ErrorForbidden("You are not welcome!"), req))
         }
         let middleware = HttpAuthentication::basic(validator);
 
@@ -806,10 +812,10 @@
     #[actix_web::test]
     async fn test_middleware_works_with_scope() {
         async fn validator(
-            _req: ServiceRequest,
+            req: ServiceRequest,
             _credentials: BasicAuth,
-        ) -> Result<ServiceRequest, actix_web::Error> {
-            Err(ErrorForbidden("You are not welcome!"))
+        ) -> Result<ServiceRequest, (actix_web::Error, ServiceRequest)> {
+            Err((ErrorForbidden("You are not welcome!"), req))
         }
         let middleware = actix_web::middleware::Compat::new(HttpAuthentication::basic(validator));