Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

exposing client builder to allow httpClient. #71

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions fusionauth-netcore-client/src/io/fusionauth/DefaultRESTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using com.inversoft.error;
Expand All @@ -28,7 +29,7 @@
using Newtonsoft.Json.Serialization;

namespace io.fusionauth {
class DefaultRESTClient : IRESTClient {
public class DefaultRESTClient : IRESTClient {
public HttpClient httpClient;

public HttpContent content;
Expand Down Expand Up @@ -57,6 +58,10 @@ public DefaultRESTClient(string host) {
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 DefaultRESTClient(HttpClient incomingHttpClient) {
httpClient = incomingHttpClient;
}

/**
* Sets the authorization header using a key
*
Expand Down Expand Up @@ -164,6 +169,7 @@ private string getFullUri() {
}

private Task<HttpResponseMessage> baseRequest() {
httpClient.DefaultRequestHeaders.Clear();
foreach (var (key, value) in headers.Select(x => (x.Key, x.Value))) {
// .Add performs additional validation on the 'value' that may fail if an API key contains a '=' character.
// - Bypass this additional validation for the Authorization header. If we find other edge cases, perhaps
Expand Down Expand Up @@ -228,4 +234,4 @@ public override Task<ClientResponse<T>> goAsync<T>() {
});
}
}
}
}
20 changes: 20 additions & 0 deletions fusionauth-netcore-client/src/io/fusionauth/FusionAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3300,4 +3300,24 @@ public IRESTClient build(string host) {
return new DefaultRESTClient(host);
}
}

public class HttpClientBuilder : IRESTClientBuilder
{
public HttpClient HTTP_CLIENT;

public HttpClientBuilder(HttpClient httpClient)
{
HTTP_CLIENT = httpClient;
}

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.

{
HTTP_CLIENT.BaseAddress = new Uri(host);
}
return new DefaultRESTClient(HTTP_CLIENT);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace io.fusionauth {
public class FusionAuthSyncClient : IFusionAuthSyncClient {
public readonly FusionAuthClient client;

public FusionAuthSyncClient(string apiKey, string host, string tenantId = null) {
client = new FusionAuthClient(apiKey, host, tenantId);
public FusionAuthSyncClient(string apiKey, string host, string tenantId = null, IRESTClientBuilder clientBuilder = null) {
client = new FusionAuthClient(apiKey, host, tenantId, clientBuilder);
}

/**
Expand Down