Skip to content

Commit

Permalink
refactor: antifactory types internal with public extension for regist…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
egil committed Jan 27, 2024
1 parent ceba6bc commit 1387e7e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/HtmxBlazorSSR/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

app.UseStaticFiles();
app.UseAntiforgery();
app.UseMiddleware<HtmxAntiforgeryMiddleware>();
app.UseHtmxorAntiforgery();

app.MapGet("/contacts/count", async (ContactsRepository repo) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Htmxor/Antiforgery/HtmxAntiforgeryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmxor.Antiforgery;
/// <summary>
/// This will add a HX-XSRF-TOKEN to each response, no matter if it was initiated by HTMX or not.
/// </summary>
public sealed class HtmxAntiforgeryMiddleware(IAntiforgery antiforgery, HtmxConfig htmxConfig, RequestDelegate next)
internal sealed class HtmxAntiforgeryMiddleware(IAntiforgery antiforgery, HtmxConfig htmxConfig, RequestDelegate next)
{
private static readonly CookieOptions cookieOptions = new CookieOptions
{
Expand Down
16 changes: 14 additions & 2 deletions src/Htmxor/Antiforgery/HtmxAntiforgeryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@

namespace Htmxor.Antiforgery;

public class HtmxAntiforgeryOptions(IOptions<AntiforgeryOptions> antiforgeryOptions)
/// <summary>
/// Represents the options for Htmxor's antiforgery support.
/// </summary>
internal class HtmxAntiforgeryOptions(IOptions<AntiforgeryOptions> antiforgeryOptions)
{
/// <summary>
/// Gets the name of the form field used for antiforgery token.
/// </summary>
public string FormFieldName { get; } = antiforgeryOptions.Value.FormFieldName;

/// <summary>
/// Gets the name of the header used for antiforgery token.
/// </summary>
public string? HeaderName { get; } = antiforgeryOptions.Value.HeaderName;

/// <summary>
/// Gets the name of the cookie used for antiforgery token.
/// </summary>
public string CookieName { get; } = "HX-XSRF-TOKEN";
}
}
21 changes: 18 additions & 3 deletions src/Htmxor/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Htmxor.Configuration;
using Htmxor.Http;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -10,16 +11,17 @@
namespace Htmxor;

/// <summary>
/// This class has extension methods for <see cref="IHostApplicationBuilder"/> that enable configuration of Htmx in the application.
/// This class has extension methods for <see cref="IHostApplicationBuilder"/> and <see cref="IApplicationBuilder"/>
/// that enable configuration of Htmx in the application.
/// </summary>
public static class WebApplicationBuilderExtensions
public static class HtmxorApplicationBuilderExtensions
{
/// <summary>
/// Add and configure Htmx.
/// </summary>
/// <param name="builder"></param>
/// <param name="configBuilder"></param>
public static void AddHtmx(this IHostApplicationBuilder builder, Action<HtmxConfig>? configBuilder = null)
public static IHostApplicationBuilder AddHtmx(this IHostApplicationBuilder builder, Action<HtmxConfig>? configBuilder = null)
{
builder.Services.AddSingleton<HtmxConfig>(x =>
{
Expand All @@ -32,5 +34,18 @@ public static void AddHtmx(this IHostApplicationBuilder builder, Action<HtmxConf
});

builder.Services.AddScoped(srv => srv.GetRequiredService<IHttpContextAccessor>().HttpContext!.GetHtmxContext());

return builder;
}

/// <summary>
/// Enable Htmx to use antiforgery tokens to secure requests.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseHtmxorAntiforgery(this IApplicationBuilder builder)
{
builder.UseMiddleware<HtmxAntiforgeryMiddleware>();
return builder;
}
}

0 comments on commit 1387e7e

Please sign in to comment.