Skip to content

Using ClientWithMiddleware trait issue #300

@davidcorrigan714

Description

@davidcorrigan714

I'm trying to provide a reqwest client wrapped in caching middleware to handle caching of the open id config data and keys but hitting an issue with traits:

I've got something like:

        let client = {
            let builder = reqwest::Client::builder();
            let builder = builder.redirect(reqwest::redirect::Policy::none());
            builder
                .build()
                .with_context(|| "Building reqwest client for OIDC client.".to_owned())?
        };

        let middleware_client = ClientBuilder::new(client)
            .with(Cache(HttpCache {
                mode: CacheMode::Default,
                manager: MokaManager::default(),
                options: HttpCacheOptions::default(),
            }))
            .build();
...
        // Use OpenID Connect Discovery to fetch the provider metadata.
        let provider_metadata = CoreProviderMetadata::discover_async(
            IssuerUrl::new("https://accounts.example.com".to_string())?,
            &middleware_client,
        )
        .await?;

But the compiler is unhappy:

error[E0277]: expected a `Fn(http::Request<Vec<u8>>)` closure, found `ClientWithMiddleware`
   --> project\src\oidc.rs:77:10
    |
77  |         .await?;
    |          ^^^^^ expected an `Fn(http::Request<Vec<u8>>)` closure, found `ClientWithMiddleware`
    |
    = help: the trait `Fn(http::Request<Vec<u8>>)` is not implemented for `ClientWithMiddleware`, which is required by `ClientWithMiddleware: AsyncHttpClient<'_>`
    = help: the trait `AsyncHttpClient<'c>` is implemented for `reqwest::Client`
    = note: required for `ClientWithMiddleware` to implement `AsyncHttpClient<'_>`
note: required by a bound in `ProviderMetadata::<A, AD, CA, CN, CT, G, JE, JK, K, RM, RT, S>::discover_async`
   --> C:\Users\dcorriga\.cargo\registry\src\index.crates.io-6f17d22bba15001f\openidconnect-4.0.0\src\discovery\mod.rs:314:12

Seems like the easiest fix would be to add an implementation for AsyncHttpClient to the ClientWithMiddleware which would seem to support the intentions mentioned in this issue.. I don't think I can pull the inner client out of the middleware and still have the caching if it's doable at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions