.NET SDK Bug: CredentialsMethod.ApiToken throw duplicate authorization header error #471
Open
6 of 10 tasks
Labels
bug
Something isn't working
Checklist
Description
There is a bug in the .NET OpenFga.Sdk in the
ApiClient
class when you configure the client to useClientCredentialsMethod.ApiToken
. The setup in our application is the following:appSettings.json
Program.cs
Using the setup above, we receive the following error any time we try to use the
OpenFgaClient
in code:After debugging the source code, we found the cause of the error. When the
OpenFgaClient
is instantiated, it creates an internalOpenFgaApi
object (see here) passing in theClientConfiguration
from the DI container and theHttpClient
created by theHttpClientFactory
. TheOpenFgaApi
then creates an internalApiClient
, again passing down the configuration and http client (see here). ThisApiClient
is where the problematic code begins.The
ApiClient
starts by initializing aBaseClient
using the configuration and client (see here) and then it initializes theBaseClient
again when it detects that the credential method isApiToken
(see here). Both of these calls use the same configuration and client.These calls work fine the first time the
HttpClient
is initialized. The client default headers are added as expected in theBaseClient
class (see here). However, it fails the second time the class is initialized because theHttpClientFactory
reuses the sameHttpClient
which already has the default headers configured. When this occurs, you get the exception above when a second "Authorization" header is added to theDefaultRequestHeaders
collection.Expectation
The code above should work as configured, specifically when using the
HttpClientFactory
registration method. TheBaseClient
constructor should check to see if the default header(s) already exist before adding additional headers.Reproduction
Given you configure the
OpenFgaClient
with theservices.AddHttpClient<OpenFgaClient>()
methodWhen you configure the
OpenFgaClient
to useApiToken
authenticationThen you receive the error "Cannot add value because header 'Authorization' does not support multiple values."
SDK Checklist
OpenFGA SDK version
0.5.1
OpenFGA version
latest
SDK Configuration
Via appSettings.json configuration
Logs
No response
References
No response
The text was updated successfully, but these errors were encountered: