2023-09-26 09:22:45 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2025-11-24 12:06:03 +01:00
|
|
|
|
using Microsoft.OpenApi;
|
2023-09-26 09:22:45 +02:00
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
using Umbraco.Cms.Api.Common.Security;
|
|
|
|
|
|
using Umbraco.Cms.Api.Delivery.Controllers.Content;
|
|
|
|
|
|
using Umbraco.Cms.Api.Delivery.Filters;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Api.Delivery.Configuration;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This configures member authentication for the Delivery API in Swagger. Consult the docs for
|
|
|
|
|
|
/// member authentication within the Delivery API for instructions on how to use this.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This class is not used by the core CMS due to the required installation dependencies (local login page among other things).
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public class ConfigureUmbracoMemberAuthenticationDeliveryApiSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
|
|
|
|
|
|
{
|
2024-11-04 14:07:18 +01:00
|
|
|
|
private const string AuthSchemeName = "UmbracoMember";
|
2023-09-26 09:22:45 +02:00
|
|
|
|
|
|
|
|
|
|
public void Configure(SwaggerGenOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
// add security requirements for content API operations
|
2024-11-04 14:07:18 +01:00
|
|
|
|
options.DocumentFilter<DeliveryApiSecurityFilter>();
|
2023-09-26 09:22:45 +02:00
|
|
|
|
options.OperationFilter<DeliveryApiSecurityFilter>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 08:32:54 +02:00
|
|
|
|
private sealed class DeliveryApiSecurityFilter : SwaggerFilterBase<ContentApiControllerBase>, IOperationFilter, IDocumentFilter
|
2023-09-26 09:22:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (CanApply(context) is false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 12:06:03 +01:00
|
|
|
|
var schemaRef = new OpenApiSecuritySchemeReference(AuthSchemeName, context.Document);
|
|
|
|
|
|
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
|
|
|
|
|
operation.Security.Add(new OpenApiSecurityRequirement { [schemaRef] = [] });
|
2023-09-26 09:22:45 +02:00
|
|
|
|
}
|
2024-11-04 14:07:18 +01:00
|
|
|
|
|
|
|
|
|
|
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (context.DocumentName != DeliveryApiConfiguration.ApiName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 17:24:28 +01:00
|
|
|
|
swaggerDoc.AddComponent(
|
2024-11-04 14:07:18 +01:00
|
|
|
|
AuthSchemeName,
|
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
|
{
|
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
|
Name = AuthSchemeName,
|
|
|
|
|
|
Type = SecuritySchemeType.OAuth2,
|
|
|
|
|
|
Description = "Umbraco Member Authentication",
|
|
|
|
|
|
Flows = new OpenApiOAuthFlows
|
|
|
|
|
|
{
|
|
|
|
|
|
AuthorizationCode = new OpenApiOAuthFlow
|
|
|
|
|
|
{
|
|
|
|
|
|
AuthorizationUrl = new Uri(Paths.MemberApi.AuthorizationEndpoint, UriKind.Relative),
|
2025-11-24 12:06:03 +01:00
|
|
|
|
TokenUrl = new Uri(Paths.MemberApi.TokenEndpoint, UriKind.Relative),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2024-11-04 14:07:18 +01:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2023-09-26 09:22:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|