add some filter to see which endpoints require authorization and which not
This commit is contained in:
parent
d8968ce557
commit
bfcba0beb7
2 changed files with 39 additions and 14 deletions
|
|
@ -0,0 +1,36 @@
|
|||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace SurveyBackend.Filters;
|
||||
|
||||
public class EndpointAuthRequirementFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
if (!context.ApiDescription
|
||||
.ActionDescriptor
|
||||
.EndpointMetadata
|
||||
.OfType<AuthorizeAttribute>()
|
||||
.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
operation.Security = new List<OpenApiSecurityRequirement>
|
||||
{
|
||||
new OpenApiSecurityRequirement
|
||||
{
|
||||
[new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = JwtBearerDefaults.AuthenticationScheme
|
||||
}
|
||||
}] = new List<string>()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using SurveyBackend.Contexts;
|
|||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.Core.Repositories;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.Filters;
|
||||
using SurveyBackend.Infrastructure.Data;
|
||||
using SurveyBackend.Infrastructure.Repositories;
|
||||
using SurveyBackend.Middlewares;
|
||||
|
|
@ -84,20 +85,8 @@ public class Program
|
|||
Name = "Authorization",
|
||||
Type = SecuritySchemeType.ApiKey
|
||||
});
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = JwtBearerDefaults.AuthenticationScheme
|
||||
}
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
});
|
||||
|
||||
c.OperationFilter<EndpointAuthRequirementFilter>();
|
||||
|
||||
var filePath = Path.Combine(System.AppContext.BaseDirectory, "SurveyBackend.API.xml");
|
||||
c.IncludeXmlComments(filePath);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue