openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] C# Make it possible to handle dynamic apikey tokens

Open bme-cgm opened this issue 10 months ago • 2 comments

Is your feature request related to a problem? Please describe.

I have generated C# code that for an endpoint uses two ApiKeyTokens. My problem is that while one of them is basically static, the other one is dynamic and changes from call to call.

I have tried to implement my own TokenProvider (DynamicTokenProvider) but when I register it, I can see it it registered as a singleton. I do this:

config.UseProvider<DynamicTokenProvider, ApiKeyToken>();

and this happens:

            _services.AddSingleton<TTokenProvider>();
            _services.AddSingleton<TokenProvider<TTokenBase>>(services => services.GetRequiredService<TTokenProvider>());

This means my tokens are fixed in time. The constructor for DynamicTokenProvider is this:

        public DynamicTokenProvider(IEconomicTokenProvider tokenSource) 
        : base(new ApiKeyToken[2] { 
            new ApiKeyToken(tokenSource.AppSecretToken, Client.ClientUtils.ApiKeyHeader.X_AppSecretToken, prefix: ""),
            new ApiKeyToken(tokenSource.AgreementGrantToken.Value ?? "", Client.ClientUtils.ApiKeyHeader.X_AgreementGrantToken, prefix: "")
        })

IEconomicTokenProvider holds the two tokens and it is using that implementation I can modify the token.

Describe the solution you'd like

I need a way to modify the ApiKeyTokens on a per call basis. I have found no workable solution so far. I am open for suggestions.

Describe alternatives you've considered

The above is the closest I have gotten without it actually working.

bme-cgm avatar Jan 22 '26 15:01 bme-cgm

I just though thought about a solution that might work (seems so). I make my own custom ApiKeyToken and pass in the IEconomicTokenProvider that I mentioned above. Then override UseInHeader and UseInQuery and make them return tokens from it. I register it by using this:

                config.AddTokens(new List<ApiKeyToken>
                {
                    new ApiKeyToken(AppSecretToken, ClientUtils.ApiKeyHeader.X_AppSecretToken, prefix: ""),
                    new AgreementApiKeyToken(tokenProvider, ClientUtils.ApiKeyHeader.X_AgreementGrantToken)
                });

AgreementApiKeyToken constructor:

        public AgreementApiKeyToken(IEconomicTokenProvider tokenSource, Client.ClientUtils.ApiKeyHeader header)
        : base("", header, prefix: "")

This might work, even though the TokenContainer is registered as a singleton in the AddTokens call.

bme-cgm avatar Jan 22 '26 16:01 bme-cgm

Make your own provider and implement GetAsync

devhl-labs avatar Jan 27 '26 05:01 devhl-labs