-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Automatically infer OpenApiSecuritySchemes
from authentication configuration
#39761
Comments
Thanks for contacting us. We're moving this issue to the |
Who knew auth could be so hard? 🤪 I started experimenting with some of this support in the new Microsoft.AspNetCore.OpenAPI package. The problem is generally easy to solve for cookie and JWT bearer-based authentication types, but OAuth authentication types are a lot trickier for us to derive automatic annotations for because:
We've had conversations about how to make the authentication system more self-describing for annotation purposes. There's more design scenarios to reason about here but for .NET 7 we're scoping it down to auto-generating annotations for JWT-bearer based types to align with the work we've done with |
@captainsafia wouldn't it be possible to request the information from the user for the things that are hard to imply? Not having any scaffolding for AAD integration feels really unprofessional right now (in the sense that it is a solution that just doesn't work at all out of the box) and it would be really nice to have something in place for .NET7. Also, a lot of parameters used to build the auth schema for swagger can leverage the config section created by Azure AD itself: that's what I did for a few APIs on my side by creating a generic extension method that introduces a Then if the user wants to move away from some of that stuff (like handling scopes directly without the config) they can edit the initial template code to accomodate. |
On another note, I wish Azure AD's OpenIdConnect endpoint was more useful for this... if it returned app-specific scopes and URLs, it could be used directly with Swagger to automatically setup a lot of different details that have to be manually added today when using the OAuth2 As it stands today, AzureAD's OpenIdConnect discovery endpoint is completely useless IMHO. Related: |
Perhaps within reason. Although the goal of this design is to automatically generate as much of the
We had discussed this option. It dovetails nicely in some ways with the work we're doing to support loading more authentication options from config (see #42170). However, configuration isn't the source of truth for how authentication is set up in application. You'll get something that partly works for generating some schemas but not all the time. Not to mention, the approach doesn't help at all for applying the security requirements on operations.
We hadn't really explored doing this as part of the template/scaffolding infrastructure. At the moment, the plan is to do this work in the framework so that it can play well with all authentication strategies. |
OpenApiSecuritySchemes
from authentication configuration
I spent some time prototyping what this would look like and landed on a pretty solid strategy that uses With that in mind, here's some of the challenges:
|
I only got
I think building a set of intermediary types is the best choice here. It'll allow us to provide plugins for MVC + NSwag/Swashbuckle and will allow community-provided authentication providers to present their own definitions for the auth strategies they register. Although it increases the API surface area of the feature it is the most flexible. |
@captainsafia thanks for the great changes. May I ask what's the current situation for .NET 9 with this? Haven't found any doc or guidance. I'm using
Now I can plug Scalar or Swashbuckle UI and read from the OpenApi json to render a UI
But what's the situation with Authentication? My endpoints are secured with Authentication Bearer schema, but the generated OpenApi document doesn't seem to infer this. As I understood, Security Schemas like Bearer authentication are standard in OpenApi v3 I suppose this is not yet available in the NuGet or .NET 9. Is there any workaround? If I was using Swashbuckle Swagger also for OpenApi document generation, I could instruct it to generate security schema like this
Is there any "manual" way to do this in the Microsoft package? I don't see any security schema option
Maybe there is some
|
I'll answer myself in case somebody find this useful. After playing a bit with the options I managed to add open api security scheme by adding a document transformer I would like to find more examples if anyone knows, but for now this works fine.
|
An inline document transformer is also how I went about it: https://github.com/martincostello/aspnetcore-openapi/blob/d87b42a236762ac32d833e6b482500b4d97f118c/src/TodoApp/OpenApi/AspNetCore/AspNetCoreOpenApiEndpoints.cs#L35-L53 |
At the moment, when users enable authentication in their ASP.NET apps, they typically have to manually the describe the
OpenApiSecuritySchemes
in their application and the top level and configureOpenApiSecurityRequirements
for each route that requires authentication and authorization.We should infer as much of these definitions as possible so users don't need to configure auth twice, once for their application and another time for OpenAPI.
Provide metadata support for parts of the specification documented in https://swagger.io/docs/specification/authentication/.
The text was updated successfully, but these errors were encountered: