tower_http::auth

Module add_authorization

Source
Expand description

Add authorization to requests using the Authorization header.

§Example

use tower_http::validate_request::{ValidateRequestHeader, ValidateRequestHeaderLayer};
use tower_http::auth::AddAuthorizationLayer;
use http::{Request, Response, StatusCode, header::AUTHORIZATION};
use tower::{Service, ServiceExt, ServiceBuilder, service_fn, BoxError};
use http_body_util::Full;
use bytes::Bytes;

let mut client = ServiceBuilder::new()
    // Use basic auth with the given username and password
    .layer(AddAuthorizationLayer::basic("username", "password"))
    .service(service_that_requires_auth);

// Make a request, we don't have to add the `Authorization` header manually
let response = client
    .ready()
    .await?
    .call(Request::new(Full::default()))
    .await?;

assert_eq!(StatusCode::OK, response.status());

Structs§