|
|
@ -1,5 +1,5 @@ |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Swashbuckle.AspNetCore.Swagger; |
|
|
|
using Microsoft.OpenApi.Models; |
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
@ -8,7 +8,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filt |
|
|
|
{ |
|
|
|
public class AuthorizeCheckOperationFilter : IOperationFilter |
|
|
|
{ |
|
|
|
public void Apply(Operation operation, OperationFilterContext context) |
|
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context) |
|
|
|
{ |
|
|
|
// Check for authorize attribute
|
|
|
|
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() || |
|
|
@ -16,16 +16,21 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filt |
|
|
|
|
|
|
|
if (!hasAuthorize) return; |
|
|
|
|
|
|
|
operation.Responses.TryAdd("401", new Response { Description = "Unauthorized" }); |
|
|
|
operation.Responses.TryAdd("403", new Response { Description = "Forbidden" }); |
|
|
|
operation.Responses.TryAdd("401", new OpenApiResponse { Description = "Unauthorized" }); |
|
|
|
operation.Responses.TryAdd("403", new OpenApiResponse { Description = "Forbidden" }); |
|
|
|
|
|
|
|
operation.Security = new List<IDictionary<string, IEnumerable<string>>> |
|
|
|
var oAuthScheme = new OpenApiSecurityScheme |
|
|
|
{ |
|
|
|
new Dictionary<string, IEnumerable<string>> |
|
|
|
{ |
|
|
|
{ "oauth2", new [] { "marketingapi" } } |
|
|
|
} |
|
|
|
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" } |
|
|
|
}; |
|
|
|
|
|
|
|
operation.Security = new List<OpenApiSecurityRequirement> |
|
|
|
{ |
|
|
|
new OpenApiSecurityRequirement |
|
|
|
{ |
|
|
|
[ oAuthScheme ] = new [] { "marketingapi" } |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |