Available on crate feature
auth only.Expand description
Middleware for adding Authorization header to requests.
This module borrows heavily from the auth module in the tower-http crate.
§Example
Authorizing requests using a “bearer” token.
use http::header::AUTHORIZATION;
use pretty_assertions::assert_eq;
use tower_layer::Layer as _;
use tower_reqwest::auth::AddAuthorizationLayer;
use tower_service::Service as _;
use wiremock::{
Mock, MockServer, ResponseTemplate,
matchers::{method, path},
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Start a mock server.
let mock_server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/test"))
.respond_with(move |request: &wiremock::Request| {
eprintln!(
"Received request with authorization header: {:?}",
request.headers[AUTHORIZATION]
);
ResponseTemplate::new(200)
})
.mount(&mock_server)
.await;
// Create a new client with the `AddAuthorizationLayer` layer.
let response = AddAuthorizationLayer::bearer("abacaba")?
.layer(reqwest::Client::new())
// Send a request to the mock server.
.call(reqwest::Request::new(
reqwest::Method::GET,
format!("{}/test", mock_server.uri()).parse()?,
))
.await?;
// Check that the request was successful.
assert_eq!(response.status(), 200);
Ok(())
}Structs§
- AddAuthorization
Layer - Layer which adds authorization to all requests using the
Authorizationheader. - AddAuthorization
Service - Middleware that adds authorization all requests using the
Authorizationheader.