Skip to content

exposing client builder to allow httpClient. #71

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mark-robustelli
Copy link

DefaultRestClient.cs is the change that will stick. The changes to FusionAuthClient.cs and FusionAuthSyncClient.cs will have to be added to fusionauth-client-bulder. I will add that now.

syncing with changes to fusionauth-client-builder FusionAuthSyncClient.
Making cleaner. Since the baseRequest function expected a clean httpClient every call, I just added a clear to the headers instead of all the validation logic.
@mark-robustelli mark-robustelli requested review from tonyblank and removed request for mmanes June 3, 2024 20:48
@matt-lethargic
Copy link

matt-lethargic commented Jun 13, 2024

Having built my own client to support injecting a HttpClient using IHttpClientFactory as this change supports, you should note that having a single HttpClient can mean that cookies and access tokens are shared across requests.

I have an API that sites between our client application and FusionAuth, if User A renews their token using the /api/jwt/refresh endpoint and then User B tries to do the same afterwards User B will receive User A's JWT/Access token.

This happens as the response from FusionAuth includes a Set-Cookie header (documented here) and the refresh endpoint and I asusme others, use the cookies over the json payload being sent

To get around this and still use IHttpClientFactory (as is best practice) I've had to disable cookies

services.AddHttpClient("my-fusion-client")
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { UseCookies = false })

Hope this helps someone and saves them the trouble I've had 👍

@@ -57,6 +58,10 @@ class DefaultRESTClient : IRESTClient {
httpClient = new HttpClient {BaseAddress = new Uri(host)};
Copy link

@nwithan8 nwithan8 Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
httpClient = new HttpClient {BaseAddress = new Uri(host)};
var handler = new HttpClientHandler
{
UseCookies = false,
};
httpClient = new HttpClient(handler) {BaseAddress = new Uri(host)};

In the same vein as @matt-lethargic's comment regarding preventing the accidental reuse of cookies. While there's not an easy way to override the HttpClientHandler for the custom HTTP client provided by the end-user, we can at least ensure that the default client will not re-use cookies by disabling the functionality during initial construction.


public IRESTClient build(string host)
{
if (HTTP_CLIENT.BaseAddress == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Echoing comment from FusionAuth/fusionauth-client-builder#75 (comment)

If a host string is passed in, but the BaseAddress of the HTTP_CLIENT is already set, the parameter is effectively ignored. This might be an anti-pattern.

@mmanes mmanes deleted the branch main March 6, 2025 17:41
@mmanes mmanes closed this Mar 6, 2025
@spwitt spwitt reopened this Mar 7, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants