1 stable release

new 1.0.0 Mar 21, 2025

#10 in #reqwest-middleware

MIT license

13KB
118 lines

reqwest-auth

A reqwest middleware preparing the authorization header.

Crates.io Docs.rs CI GitHub

Use case

This crate is for you if:

  • You are already using (or thinking about it) reqwest and/or reqwest-middleware.
  • You need to authenticate your request by providing tokens in the HTTP authorization.
  • You do not want to hard code or DIY it as it is bothersome.

How it works

The middleware relies on the token source crate common traits, more specifically the TokenSource one. If you do not know what token source offers, please have a look at its documentation.

Long story short, the TokenSource is responsible for managing your tokens and their lifetime. (for example using google-cloud-rust/auth) The middleware will use this source to obtain a token and update the AUTHORIZATION header of requests with its value.

Important: it is recommended to keep the AuthorizationHeaderMiddleware as the last one in your middleware chain if you need subsequent middlewares to benefit from fresh tokens. One typical example is when you are using the reqwest-retry middleware, the authorization one should come after.

Installation

[dependencies]
reqwest-auth = "1.0.0"

Quickstart

  let ts_provider = ...; // You should build your own or use an existing one.

  // Let say you are using the retry middleware as well
  let retry_middleware = ...;
  // Create the middleware from the token source
  let auth_middleware = AuthorizationHeaderMiddleware::from(ts_provider.token_source());

  // Create your reqwest client with the middleware
  let client = ClientBuilder::new(reqwest::Client::default())
    .with(retry_middleware)
    // Ideally, the authorization middleware should come last,
    // especially if you are using a retry middleware as well.
    // This way, your retry requests will benefit from the renewals of the token,
    // as long as your token source implementation is able to renew the token.
    .with(auth_middleware)
    .build();

Dependencies

~4–15MB
~189K SLoC