Module actix_web_httpauth::middleware
source · [−]Expand description
Expand description
HTTP Authentication middleware.
Structs
Middleware for checking HTTP authentication.
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 @@
HTTP Authentication middleware.
Middleware for checking HTTP authentication.
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.
Construct HttpAuthentication
middleware with the provided auth extractor T
and
+
Construct HttpAuthentication
middleware with the provided auth extractor T
and
validation callback F
.
Construct HttpAuthentication
middleware for the HTTP “Basic” authentication scheme.
// 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.
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
.
Returns a copy of the value. Read more
Performs copy-assignment from source
. 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//! 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));