|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection.Metadata;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Authorization;
|
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
@ -8,25 +10,19 @@ namespace Ombi
|
|
|
|
|
{
|
|
|
|
|
public class SwaggerOperationFilter : IOperationFilter
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
|
|
|
|
|
public SwaggerOperationFilter()
|
|
|
|
|
{
|
|
|
|
|
Name = "Authorization";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Apply(Operation operation, OperationFilterContext context)
|
|
|
|
|
{
|
|
|
|
|
var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
|
|
|
|
|
var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
|
|
|
|
|
var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);
|
|
|
|
|
if (isAuthorized && !allowAnonymous)
|
|
|
|
|
{
|
|
|
|
|
if (operation.Parameters == null)
|
|
|
|
|
operation.Parameters = new List<IParameter>();
|
|
|
|
|
operation.Parameters.Add(new NonBodyParameter
|
|
|
|
|
{
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
In = "header",
|
|
|
|
|
Description = "JWT token",
|
|
|
|
|
Required = true,
|
|
|
|
|
Type = "string",
|
|
|
|
|
Default = "Bearer "
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (operation.Parameters == null)
|
|
|
|
|
operation.Parameters = new List<IParameter>();
|
|
|
|
|
var tokenAuthDict = new Dictionary<string, IEnumerable<string>> {{Name, new List<string>()}};
|
|
|
|
|
operation.Security = new IDictionary<string, IEnumerable<string>>[] { tokenAuthDict };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|