WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
|
||||
namespace Events.WebAPI.Util.Swagger;
|
||||
|
||||
public static class AddBearerTokenSchemeExtension
|
||||
{
|
||||
public static void AddBearerTokenScheme(this SwaggerGenOptions opt)
|
||||
{
|
||||
var jwtSecurityScheme = new OpenApiSecurityScheme
|
||||
{
|
||||
Description = "Paste token here",
|
||||
Name = "Authorization",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = JwtBearerDefaults.AuthenticationScheme,
|
||||
BearerFormat = "JWT",
|
||||
};
|
||||
|
||||
//Dodaj Authorize button u Swagger UI
|
||||
opt.AddSecurityDefinition(jwtSecurityScheme.Scheme, jwtSecurityScheme);
|
||||
|
||||
//opt.AddSecurityRequirement(document => new() { [new OpenApiSecuritySchemeReference(jwtSecurityScheme.Scheme, document)] = [] });
|
||||
|
||||
// nemoj ga primijeniti na sve operacije (redak iznad), nego samo na one koje imaju Authorize atribut
|
||||
opt.OperationFilter<AuthorizeOperationFilter>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace Events.WebAPI.Util.Swagger;
|
||||
|
||||
public class AuthorizeOperationFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var hasAuthorize = context.MethodInfo.DeclaringType?
|
||||
.GetCustomAttributes(true)
|
||||
.OfType<AuthorizeAttribute>().Any() == true
|
||||
||
|
||||
context.MethodInfo.GetCustomAttributes(true)
|
||||
.OfType<AuthorizeAttribute>().Any();
|
||||
|
||||
if (!hasAuthorize)
|
||||
return;
|
||||
|
||||
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
||||
|
||||
operation.Security.Add(new OpenApiSecurityRequirement
|
||||
{
|
||||
[
|
||||
new OpenApiSecuritySchemeReference(
|
||||
JwtBearerDefaults.AuthenticationScheme,
|
||||
context.Document
|
||||
)
|
||||
] = new List<string>()
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user