-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Swashbuckle.AspNetCore with Microsoft.AspNetCore.OpenApi
- Loading branch information
Showing
4 changed files
with
105 additions
and
62 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/Savings.API/OpenApi/DocumentSecuritySchemeTransformer.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,42 @@ | ||
using Microsoft.AspNetCore.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
using Savings.API.Authentication; | ||
using Savings.Model; | ||
|
||
namespace Savings.API.OpenApi | ||
{ | ||
internal sealed class DocumentSecuritySchemeTransformer(IConfiguration conf) : IOpenApiDocumentTransformer | ||
{ | ||
|
||
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
var authenticationToUse = conf["AuthenticationToUse"]; | ||
|
||
document.Info = new OpenApiInfo { Title = "Savings", Version = "v1" }; | ||
if (authenticationToUse == AuthenticationToUse.AzureAD) | ||
{ | ||
document.Components = new OpenApiComponents(); | ||
document.Components.SecuritySchemes.Add(ApiKeyAuthOptions.ApiKeySchemaName, new OpenApiSecurityScheme | ||
{ | ||
In = ParameterLocation.Header, | ||
Description = "Please insert JWT with Bearer into field", | ||
Name = "Authorization", | ||
Type = SecuritySchemeType.OAuth2 | ||
}); | ||
} | ||
else if (authenticationToUse == AuthenticationToUse.ApiKey) | ||
{ | ||
document.Components = new OpenApiComponents(); | ||
document.Components.SecuritySchemes.Add(ApiKeyAuthOptions.ApiKeySchemaName, new OpenApiSecurityScheme | ||
{ | ||
Description = "Api key needed to access the endpoints. " + ApiKeyAuthOptions.HeaderName + ": My_API_Key", | ||
In = ParameterLocation.Header, | ||
Name = ApiKeyAuthOptions.HeaderName, | ||
Type = SecuritySchemeType.ApiKey | ||
}); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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,52 @@ | ||
using Microsoft.AspNetCore.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
using Savings.API.Authentication; | ||
using Savings.Model; | ||
|
||
namespace Savings.API.OpenApi | ||
{ | ||
internal sealed class OperationSecurityTransformer(IConfiguration conf) : IOpenApiOperationTransformer | ||
{ | ||
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
var authenticationToUse = conf["AuthenticationToUse"]; | ||
|
||
if (authenticationToUse == AuthenticationToUse.AzureAD) | ||
{ | ||
operation.Security = [new OpenApiSecurityRequirement { | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = "Bearer" | ||
} | ||
}, | ||
new string[] {} | ||
} | ||
}]; | ||
} | ||
else if (authenticationToUse == AuthenticationToUse.ApiKey) | ||
{ | ||
operation.Security = [new OpenApiSecurityRequirement { | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Name = ApiKeyAuthOptions.HeaderName, | ||
Type = SecuritySchemeType.ApiKey, | ||
In = ParameterLocation.Header, | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = ApiKeyAuthOptions.ApiKeySchemaName | ||
}, | ||
}, | ||
new string[] {} | ||
} | ||
}]; | ||
} | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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