pub struct AddAuthorizationLayer { /* private fields */ }Available on crate feature
auth only.Expand description
Layer which adds authorization to all requests using the Authorization header.
§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(())
}Implementations§
Source§impl AddAuthorizationLayer
impl AddAuthorizationLayer
Sourcepub fn basic(
username: impl AsRef<str>,
password: impl AsRef<str>,
) -> Result<Self, InvalidHeaderValue>
pub fn basic( username: impl AsRef<str>, password: impl AsRef<str>, ) -> Result<Self, InvalidHeaderValue>
Authorize requests using a username and password pair.
The Authorization header will be set to Basic {credentials} where credentials is
base64_encode("{username}:{password}").
Since the username and password is sent in clear text it is recommended to use HTTPS/TLS with this method. However use of HTTPS/TLS is not enforced by this middleware.
Sourcepub fn bearer(token: impl AsRef<str>) -> Result<Self, InvalidHeaderValue>
pub fn bearer(token: impl AsRef<str>) -> Result<Self, InvalidHeaderValue>
Authorize requests using a “bearer token”. Commonly used for OAuth 2.
The Authorization header will be set to Bearer {token}.
Sourcepub fn set_sensitive(self, sensitive: bool) -> Self
pub fn set_sensitive(self, sensitive: bool) -> Self
Mark the header as sensitive.
This can for example be used to hide the header value from logs.
Trait Implementations§
Source§impl Clone for AddAuthorizationLayer
impl Clone for AddAuthorizationLayer
Source§fn clone(&self) -> AddAuthorizationLayer
fn clone(&self) -> AddAuthorizationLayer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AddAuthorizationLayer
impl Debug for AddAuthorizationLayer
Auto Trait Implementations§
impl !Freeze for AddAuthorizationLayer
impl RefUnwindSafe for AddAuthorizationLayer
impl Send for AddAuthorizationLayer
impl Sync for AddAuthorizationLayer
impl Unpin for AddAuthorizationLayer
impl UnwindSafe for AddAuthorizationLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more