-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from AdastralGroup/feature/swagger-auth
Implement Swagger Authentication
- Loading branch information
Showing
4 changed files
with
129 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Adastral.Cockatoo.Services.WebApi; | ||
|
||
public class FileUploadFilter : IOperationFilter | ||
{ | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
var formParameters = context.ApiDescription.ParameterDescriptions | ||
.Where(paramDesc => paramDesc.IsFromForm()); | ||
if(formParameters.Any()) | ||
{ | ||
// already taken care by swashbuckle. no need to add explicitly. | ||
return; | ||
} | ||
if(operation.RequestBody != null) | ||
{ | ||
// NOT required for form type | ||
return; | ||
} | ||
if (context.ApiDescription.HttpMethod == HttpMethod.Post.Method) | ||
{ | ||
var uploadFileMediaType = new OpenApiMediaType() | ||
{ | ||
Schema = new OpenApiSchema() | ||
{ | ||
Type = "object", | ||
Properties = | ||
{ | ||
["files"] = new OpenApiSchema() | ||
{ | ||
Type = "array", | ||
Items = new OpenApiSchema() | ||
{ | ||
Type = "string", | ||
Format = "binary", | ||
Description = "Form File Upload" | ||
}, | ||
Description = "Form File Upload" | ||
} | ||
}, | ||
Required = new HashSet<string>() { "files" }, | ||
Description = "Form File Upload" | ||
} | ||
}; | ||
var uploadStreamFileType = new OpenApiMediaType() | ||
{ | ||
Schema = new OpenApiSchema() | ||
{ | ||
Type = "string", | ||
Format = "binary", | ||
Description = "Binary File Upload" | ||
} | ||
}; | ||
|
||
operation.RequestBody = new OpenApiRequestBody | ||
{ | ||
Content = new Dictionary<string, OpenApiMediaType>(){ {"multipart/form-data", uploadFileMediaType}, | ||
{"application/octet-stream", uploadStreamFileType} }, | ||
Required = true | ||
}; | ||
} | ||
} | ||
} | ||
internal static class MvcApiExplorerExtensions | ||
{ | ||
internal static bool IsFromForm(this ApiParameterDescription apiParameter) | ||
{ | ||
var source = apiParameter.Source; | ||
var elementType = apiParameter.ModelMetadata?.ElementType; | ||
|
||
return (source == BindingSource.Form || source == BindingSource.FormFile) | ||
|| (elementType != null && typeof(IFormFile).IsAssignableFrom(elementType)); | ||
} | ||
} |
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