Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable reuse of BackOfficeSecurityRequirementsOperationFilter for cus…
…tom APIs (#15699)
- Loading branch information
Showing
2 changed files
with
60 additions
and
50 deletions.
There are no files selected for viewing
52 changes: 2 additions & 50 deletions
52
src/Umbraco.Cms.Api.Management/OpenApi/BackOfficeSecurityRequirementsOperationFilter.cs
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 |
---|---|---|
@@ -1,56 +1,8 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Umbraco.Cms.Api.Management.DependencyInjection; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Api.Management.OpenApi; | ||
|
||
internal class BackOfficeSecurityRequirementsOperationFilter : IOperationFilter | ||
internal class BackOfficeSecurityRequirementsOperationFilter : BackOfficeSecurityRequirementsOperationFilterBase | ||
{ | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
if (context.MethodInfo.HasMapToApiAttribute(ManagementApiConfiguration.ApiName) == false) | ||
{ | ||
return; | ||
} | ||
|
||
if (!context.MethodInfo.GetCustomAttributes(true).Any(x => x is AllowAnonymousAttribute) && | ||
!(context.MethodInfo.DeclaringType?.GetCustomAttributes(true).Any(x => x is AllowAnonymousAttribute) ?? false)) | ||
{ | ||
operation.Responses.Add(StatusCodes.Status401Unauthorized.ToString(), new OpenApiResponse | ||
{ | ||
Description = "The resource is protected and requires an authentication token" | ||
}); | ||
|
||
operation.Security = new List<OpenApiSecurityRequirement> | ||
{ | ||
new OpenApiSecurityRequirement | ||
{ | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = ManagementApiConfiguration.ApiSecurityName | ||
} | ||
}, new string[] { } | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
// If method/controller has an explicit AuthorizeAttribute or the controller ctor injects IAuthorizationService, then we know Forbid result is possible. | ||
if (context.MethodInfo.GetCustomAttributes(false).Any(x => x is AuthorizeAttribute | ||
|| context.MethodInfo.DeclaringType?.GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IAuthorizationService))) is true)) | ||
{ | ||
operation.Responses.Add(StatusCodes.Status403Forbidden.ToString(), new OpenApiResponse | ||
{ | ||
Description = "The authenticated user do not have access to this resource" | ||
}); | ||
} | ||
} | ||
protected override string ApiName => ManagementApiConfiguration.ApiName; | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Umbraco.Cms.Api.Management/OpenApi/BackOfficeSecurityRequirementsOperationFilterBase.cs
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,58 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Umbraco.Cms.Api.Management.DependencyInjection; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Api.Management.OpenApi; | ||
|
||
public abstract class BackOfficeSecurityRequirementsOperationFilterBase : IOperationFilter | ||
{ | ||
protected abstract string ApiName { get; } | ||
|
||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
if (context.MethodInfo.HasMapToApiAttribute(ApiName) == false) | ||
{ | ||
return; | ||
} | ||
|
||
if (!context.MethodInfo.GetCustomAttributes(true).Any(x => x is AllowAnonymousAttribute) && | ||
!(context.MethodInfo.DeclaringType?.GetCustomAttributes(true).Any(x => x is AllowAnonymousAttribute) ?? false)) | ||
{ | ||
operation.Responses.Add(StatusCodes.Status401Unauthorized.ToString(), new OpenApiResponse | ||
{ | ||
Description = "The resource is protected and requires an authentication token" | ||
}); | ||
|
||
operation.Security = new List<OpenApiSecurityRequirement> | ||
{ | ||
new OpenApiSecurityRequirement | ||
{ | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = ManagementApiConfiguration.ApiSecurityName | ||
} | ||
}, new string[] { } | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
// If method/controller has an explicit AuthorizeAttribute or the controller ctor injects IAuthorizationService, then we know Forbid result is possible. | ||
if (context.MethodInfo.GetCustomAttributes(false).Any(x => x is AuthorizeAttribute | ||
|| context.MethodInfo.DeclaringType?.GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IAuthorizationService))) is true)) | ||
{ | ||
operation.Responses.Add(StatusCodes.Status403Forbidden.ToString(), new OpenApiResponse | ||
{ | ||
Description = "The authenticated user do not have access to this resource" | ||
}); | ||
} | ||
} | ||
} |