question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically infer `OpenApiSecuritySchemes` from authentication configuration

See original GitHub issue

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 configure OpenApiSecurityRequirements 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/.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
captainsafiacommented, Jun 14, 2022

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 don’t store any information about what type of grant flow an OAuth implementation uses
  • We don’t make it easy for authentication scheme providers to describe what authentication type they are

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 dotnet user-jwts and the options-from-config changes.

1reaction
captainsafiacommented, Nov 22, 2022

I spent some time prototyping what this would look like and landed on a pretty solid strategy that uses PostConfigureOptions on a custom AuthenticationBuilder that would work well for this.

With that in mind, here’s some of the challenges:

  • The PostConfigureOptions needs to be added to DI as part of the AddAuthentication call. In my demo, I added this behavior to a custom WebApplicationAuthenticationBuilder that was part of WebApplication. Similar to the model that we tried out in .NET 7 with builder.Authentication. It’s not totally necessary but not having it would mean that this behavior will be enabled for all authentication scenarios in .NET 8.
  • The PostConfigureOptions would need to emit OpenApiSecuritySchemes given AuthenticationSchemeOptions but cannot take a dependency on the type since it is not in the shared framework. We could consider packaging it in the shared framework or build a set of intermediary definitions.
  • The prototype relies on a global document service existing (see https://github.com/dotnet/aspnetcore/issues/44192) so that the security schemes under components can be populated.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Authentication
OpenAPI 3.0 lets you describe APIs protected using the following security schemes: HTTP authentication schemes (they use the Authorization header):. Basic ...
Read more >
Restricting API access with API keys - Cloud Endpoints
This section configures basic authentication with an API key. ... The OpenAPI specification requires an empty list for security schemes that don't use...
Read more >
OpenAPI Specification v3.0.3 | Introduction, Definitions, & ...
The OpenAPI Specification allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model ...
Read more >
Spring Boot 3 OpenAPI Documentation with Springdoc
It examines the application at runtime to infer API semantics based on class annotations and configuration beans.
Read more >
springdoc-openapi v1.7.0
springdoc-openapi works by examining an application at runtime to infer API semantics based on spring configurations, class structure and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found