-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor OpenAPI/Swagger auth integration
Refactored and added new functionality for OpenAPI and Swagger integration related to authentication in a .NET project. Removed `Helpers` class and moved its functionality to a new `OpenApiHelpers` class. Updated `AuthenticationOperationFilter` and `AuthenticationOperationTransformer` to use `OpenApiHelpers`. Updated `SwaggerExtensions` and `OpenApiExtensions` to support OAuth2 authentication, including new methods for adding OAuth2 schemes and security requirements. Introduced `OAuth2AuthenticationDocumentTransformer` for transforming OpenAPI documents to include OAuth2. Ensured compatibility with .NET 9.0+ using conditional compilation. Defined `OpenApiHelpers` in both `SimpleAuthentication.Swagger` and `SimpleAuthentication.OpenApi` namespaces for different contexts. #127 #128
- Loading branch information
1 parent
5f6ab87
commit 3fd85e6
Showing
8 changed files
with
136 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/SimpleAuthentication/OpenApi/OAuth2AuthenticationDocumentTransformer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#if NET9_0_OR_GREATER | ||
|
||
using Microsoft.AspNetCore.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace SimpleAuthentication.OpenApi; | ||
|
||
internal class OAuth2AuthenticationDocumentTransformer(string name, OpenApiOAuthFlow authFlow) : IOpenApiDocumentTransformer | ||
{ | ||
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
document.Components ??= new(); | ||
document.Components.SecuritySchemes ??= new Dictionary<string, OpenApiSecurityScheme>(); | ||
|
||
document.Components.SecuritySchemes.Add(name, new() | ||
{ | ||
Type = SecuritySchemeType.OAuth2, | ||
Flows = new() | ||
{ | ||
Implicit = authFlow | ||
} | ||
}); | ||
|
||
AuthenticationDocumentTransformer.AddSecurityRequirement(document, name); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters