| | 1 | | using Microsoft.OpenApi.Models; |
| | 2 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Filters; |
| | 5 | |
|
| | 6 | | public class AuthOperationsFilter : IOperationFilter |
| | 7 | | { |
| | 8 | | public void Apply(OpenApiOperation operation, OperationFilterContext context) |
| | 9 | | { |
| 0 | 10 | | var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true) |
| 0 | 11 | | .Union(context.MethodInfo.GetCustomAttributes(true)) |
| 0 | 12 | | .OfType<AuthorizeAttribute>(); |
| | 13 | |
|
| 0 | 14 | | if (authAttributes.Any()) |
| | 15 | | { |
| 0 | 16 | | var securityRequirement = new OpenApiSecurityRequirement() |
| 0 | 17 | | { |
| 0 | 18 | | { |
| 0 | 19 | | new OpenApiSecurityScheme |
| 0 | 20 | | { |
| 0 | 21 | | Reference = new OpenApiReference |
| 0 | 22 | | { |
| 0 | 23 | | Type = ReferenceType.SecurityScheme, |
| 0 | 24 | | Id = "Bearer", |
| 0 | 25 | | } |
| 0 | 26 | | }, |
| 0 | 27 | | new string[] {} |
| 0 | 28 | | } |
| 0 | 29 | | }; |
| 0 | 30 | | operation.Security = [securityRequirement]; |
| 0 | 31 | | operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" }); |
| | 32 | | } |
| 0 | 33 | | } |
| | 34 | | } |