aliri_reqwest 0.1.0

Background token management and renewal for reqwest based on best practices
Documentation

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;
# use aliri_clock::DurationSecs;
# use aliri_tokens::{AccessToken, TokenLifetimeConfig, TokenWithLifetime};
# use aliri_tokens::backoff::ErrorBackoffConfig;
# use aliri_tokens::jitter::NullJitter;
# use aliri_tokens::sources::AsyncTokenSource;
#
# struct ConstTokenSource;
#
# #[async_trait::async_trait]
# impl AsyncTokenSource for ConstTokenSource {
#     type Error = core::convert::Infallible;
#
#     async fn request_token(&mut self) -> std::result::Result<TokenWithLifetime, Self::Error> {
#         Ok(TokenLifetimeConfig::default().create_token(
#             AccessToken::new("token"),
#             None,
#             DurationSecs(600),
#         ))
#     }
# }
# #[tokio::main(flavor = "current_thread")] async fn main() {
# let (token_source, jitter, backoff)  = (ConstTokenSource, NullJitter, ErrorBackoffConfig::default());

let token_watcher = TokenWatcher::spawn_from_token_source(
token_source,
jitter,
backoff,
)
.await
.unwrap();

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

let req = client
.get("https://example.com");
# async move { req
.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, AccessTokenPredicate, ExactHostMatch, HttpsOnly
};
# let watcher = todo!();

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