Skip to content

Commit

Permalink
General refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyApeOne authored and FrostyApeOne committed Feb 17, 2025
1 parent 6ae5a8a commit 22391df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
namespace DfE.CoreLibs.Security.Cypress
{
/// <summary>
/// Provides extension methods for registering Cypress-related antiforgery handling in the MVC pipeline.
/// Provides extension methods for registering Cypress-related AntiForgery handling in the MVC pipeline.
/// </summary>
public static class CypressAntiforgeryExtensions
public static class CypressAntiForgeryExtensions
{
/// <summary>
/// Registers the <see cref="ICypressRequestChecker"/> and <see cref="CypressAwareAntiforgeryFilter"/> services,
/// and inserts the Cypress antiforgery filter globally into the MVC pipeline.
/// Registers the <see cref="ICypressRequestChecker"/> and <see cref="CypressAwareAntiForgeryFilter"/> services,
/// and inserts the Cypress AntiForgery filter globally into the MVC pipeline.
/// </summary>
/// <remarks>
/// <para>
/// By calling this method, any incoming request recognized as a Cypress request
/// (per <see cref="ICypressRequestChecker"/>) will skip antiforgery validation.
/// Other requests will require antiforgery validation as normal.
/// (per <see cref="ICypressRequestChecker"/>) will skip AntiForgery validation.
/// Other requests will require AntiForgery validation as normal.
/// </para>
/// </remarks>
/// <returns>
/// The same <see cref="IMvcBuilder"/> so further MVC configuration can be chained.
/// </returns>
public static IMvcBuilder AddCypressAntiforgeryHandling(this IMvcBuilder mvcBuilder)
public static IMvcBuilder AddCypressAntiForgeryHandling(this IMvcBuilder mvcBuilder)
{
mvcBuilder.Services.AddScoped<ICypressRequestChecker, CypressRequestChecker>();

mvcBuilder.Services.AddScoped<CypressAwareAntiforgeryFilter>();
mvcBuilder.Services.AddScoped<CypressAwareAntiForgeryFilter>();

mvcBuilder.Services.PostConfigure<MvcOptions>(options =>
{
options.Filters.AddService<CypressAwareAntiforgeryFilter>();
options.Filters.AddService<CypressAwareAntiForgeryFilter>();
});

return mvcBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace DfE.CoreLibs.Security.Cypress
{
/// <summary>
/// An authorization filter that enforces antiforgery validation for all requests,
/// An authorization filter that enforces AntiForgery validation for all requests,
/// except for those recognized as valid Cypress requests.
/// </summary>
public class CypressAwareAntiforgeryFilter(
IAntiforgery antiforgery,
ILogger<CypressAwareAntiforgeryFilter> logger,
public class CypressAwareAntiForgeryFilter(
IAntiforgery antiForgery,
ILogger<CypressAwareAntiForgeryFilter> logger,
ICypressRequestChecker cypressChecker)
: IAsyncAuthorizationFilter
{
Expand All @@ -28,12 +28,12 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
var isCypress = cypressChecker.IsCypressRequest(context.HttpContext);
if (isCypress)
{
logger.LogInformation("Skipping antiforgery for Cypress request");
logger.LogInformation("Skipping AntiForgery for Cypress request");
return;
}

logger.LogInformation("Enforcing antiforgery for non-Cypress request");
await antiforgery.ValidateRequestAsync(context.HttpContext);
logger.LogInformation("Enforcing AntiForgery for non-Cypress request");
await antiForgery.ValidateRequestAsync(context.HttpContext);
}
}

Expand Down

0 comments on commit 22391df

Please sign in to comment.