azure-functions-openapi-extension
azure-functions-openapi-extension copied to clipboard
Question: OpenApiOAuthFlows configuration options
I am in the process of trying to configure an OpenApiOAuthFlows
(for oauth clientcredentials) and was hoping to be able to access configuration (or maybe even other services), but it does not seem to be supported through constructor injection or in any alternative approaches. The error from attempting constructor injection is simply No parameterless constructor defined for type
.
Is there an approach I am missing, is dependency injection planned, or is there a recommended workaround? (I am guessing reading environment variables directly is a somewhat reasonably approach, if not as nice or flexible as IConfiguration)
@Skovvart Thanks for the issue!
I'm not sure what you want to achieve. OpenApiOAuthFlows
comes from OpenAPI.NET, which only has a parameterless constructor.
This extension has OpenApiOAuthSecurityFlows
that inherits OpenApiOAuthFlows
, and it also only has the parameterless constructor. If you want to use this, you should inherit it and can add as many constructors as you want.
Hi @justinyoo
Thanks for replying. Let me try to elaborate on what I am trying to achieve.
As mentioned, I am trying to describe OAuth 2 Client Credentials flow on my API.
To do this, I am using this extensions OpenApiSecurity
attribute with the Flows
property pointing to my (client credentials) configuration:
[OpenApiSecurity(MyOAuthFlows.SchemeName, SecuritySchemeType.OAuth2, Flows = typeof(MyOAuthFlows))]
In my OpenApiOAuthFlows
implementation I need to set up values, in this case for the ClientCredentials
flow:
public class MyOAuthFlows : OpenApiOAuthFlows
{
private const string MyAzureAadTenantId = "sure would be nice to configure this";
public const string SchemeName = "MyApiSecurityScheme";
public MyOAuthFlows()
{
var apiScopeEnvironment = "sure would be nice to configure this";
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri($"https://login.microsoft.com/{MyAzureAadTenantId}/oauth2/v2.0/token"),
Scopes = { { $"api://my-api-{apiScopeEnvironment}/.default", $"Access API ({apiScopeEnvironment})" } }
};
}
}
It was my hope that this extension (as it is the one pointing to the Flows
type) would instantiate my class using dependency injection so I could inject dependencies like IConfiguration
, bound configuration, etc. in the constructor, but it seems like the current implementation is a simple Activator.CreateInstance
(https://github.com/Azure/azure-functions-openapi-extension/blob/ef3819d79de6d58ca80500d7d1072522437886d3/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/DocumentHelper.cs#L309) that does not support this.
If the above is unreasonable, an alternative recommended approach would be appreciated - right now I am using System.Environment.GetEnvironmentVariable(...)
, but it is of course a little limited compared to the .NET core configuration options.
@Skovvart Thanks for the detailed explanation! Please use Environment.GetEnvironmentVariable(...)
option for the time being. I'll keep this open for further discussion.
Perhaps going on a year later, my team would also find this useful.
Just wanted to put my two cents in to see if this could go on a list of possible features for the future :)
Do I understand correctly that there is still no possibility of DI here?
It would be great if the feature would come.