Expand description

Middleware to automatically attach authorization to outgoing requests

When using ClientWithMiddleware, include the AccessTokenMiddleware in the middleware stack to use the current access token provided by a TokenWatcher for each outbound request.

If a request already has specified an Authorization header value by the time that the middleware executes, the existing value will be left in place, allowing overrides to be specified as required.

use aliri_reqwest::AccessTokenMiddleware;
use aliri_tokens::TokenWatcher;
use reqwest::Client;
use reqwest_middleware::ClientBuilder;

let client = ClientBuilder::new(Client::default())
    .with(AccessTokenMiddleware::new(token_watcher))
    .build();

let req = client
    .get("https://example.com");
    .send()
    .await
    .unwrap();

The middleware can also be configured to add an authorization token only conditionally. This can be useful in the event that you want to use a single common middleware stack with multiple potential backends and want to ensure that specific tokens are used for specific backends.

These predicates can be composed together to evaluate more complex requirements prior to attaching a token to a request.

use aliri_reqwest::{
    AccessTokenMiddleware, ExactHostMatch, HttpsOnly
};
use predicates::prelude::PredicateBooleanExt;

AccessTokenMiddleware::new(token_watcher)
    .with_predicate(HttpsOnly.and(ExactHostMatch::new("example.com")));

Structs

A middleware that injects an access token into outgoing requests

Only attach an access token if the request is being sent to the exact host specified

Only attach an access token if the request is being sent over HTTPS