Skip to content

Commit

Permalink
Add enableAntiforgery option parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
albertwoo committed Nov 30, 2023
1 parent 5600f8f commit f54fcc3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Fun.Blazor.Server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- enableAntiforgery

## [3.3.0-beta001] - 2023-11-29

- Rename MapFunBlazorCustomElements to MapCustomElementsForSSR
Expand Down
67 changes: 39 additions & 28 deletions Fun.Blazor.Server/DIExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ type FunBlazorServerExtensions =
/// This will serve all the razor components (implement IComponent interface) for server side rendering,
/// route pattern: /fun-blazor-server-side-render-components/{componentType}
[<Extension>]
static member MapRazorComponentsForSSR(builder: IEndpointRouteBuilder, types: Type seq, ?notFoundNode: NodeRenderFragment) =
static member MapRazorComponentsForSSR(builder: IEndpointRouteBuilder, types: Type seq, ?notFoundNode: NodeRenderFragment, ?enableAntiforgery: bool) =
let enableAntiforgery = defaultArg enableAntiforgery false

types
|> Seq.iter (fun x ->
Utils.razorComponentsForSSRTypes.Value[x.FullName] <-
Expand All @@ -311,34 +313,40 @@ type FunBlazorServerExtensions =
|}
)

builder
.Map(
"/fun-blazor-server-side-render-components/{componentType}",
Func<_, _, _>(fun (componentType: string) (ctx: HttpContext) ->
match Utils.razorComponentsForSSRTypes.Value.TryGetValue(componentType) with
| true, comp -> html.blazor (comp.Type, attr = comp.CreateAttr ctx)
| _ -> defaultArg notFoundNode html.none
let builder =
builder
.Map(
"/fun-blazor-server-side-render-components/{componentType}",
Func<_, _, _>(fun (componentType: string) (ctx: HttpContext) ->
match Utils.razorComponentsForSSRTypes.Value.TryGetValue(componentType) with
| true, comp -> html.blazor (comp.Type, attr = comp.CreateAttr ctx)
| _ -> defaultArg notFoundNode html.none
)
)
)
.AddFunBlazor()
|> ignore
.AddFunBlazor()

if enableAntiforgery then
builder.WithMetadata(RequiresAntiforgeryMetadata()) |> ignore

/// This will serve all blazor components (inherit from ComponentBase) in the target assembly for server side rendering
/// route pattern: /fun-blazor-server-side-render-components/{componentType}.
/// Value can be passed by query or form for component property, form will have higher priority. Only the last value will be take when found same keys.
[<Extension>]
static member MapRazorComponentsForSSR(builder: IEndpointRouteBuilder, assembly: Assembly, ?notFoundNode: NodeRenderFragment) =
static member MapRazorComponentsForSSR(builder: IEndpointRouteBuilder, assembly: Assembly, ?notFoundNode: NodeRenderFragment, ?enableAntiforgery: bool) =
builder.MapRazorComponentsForSSR(
assembly.GetTypes() |> Seq.filter (fun x -> x.IsAssignableTo(typeof<IComponent>)),
defaultArg notFoundNode html.none
defaultArg notFoundNode html.none,
defaultArg enableAntiforgery false
)


/// This will serve all components for server side rendering with custom element enabled.
/// You should use it with: services.AddServerSideBlazor(fun options -> options.RootComponents.RegisterCustomElementForFunBlazor<YourComponent>()),
/// route pattern: /fun-blazor-custom-elements/{componentType}
[<Extension>]
static member MapCustomElementsForSSR(builder: IEndpointRouteBuilder, types: Type seq, ?notFoundNode: NodeRenderFragment) =
static member MapCustomElementsForSSR(builder: IEndpointRouteBuilder, types: Type seq, ?notFoundNode: NodeRenderFragment, ?enableAntiforgery) =
let enableAntiforgery = defaultArg enableAntiforgery false

types
|> Seq.iter (fun ty ->
Utils.funBlazorCustomElementsForSSRTypes.Value[ty.FullName] <-
Expand All @@ -348,29 +356,32 @@ type FunBlazorServerExtensions =
|}
)

builder
.Map(
"/fun-blazor-custom-elements/{componentType}",
Func<_, _, _>(fun (componentType: string) (ctx: HttpContext) ->
match Utils.funBlazorCustomElementsForSSRTypes.Value.TryGetValue(componentType) with
| true, comp ->
let attrs = comp.CreateAttr ctx
html.customElement (comp.Type, attrs = attrs)
| _ -> defaultArg notFoundNode html.none
let builder =
builder
.Map(
"/fun-blazor-custom-elements/{componentType}",
Func<_, _, _>(fun (componentType: string) (ctx: HttpContext) ->
match Utils.funBlazorCustomElementsForSSRTypes.Value.TryGetValue(componentType) with
| true, comp ->
let attrs = comp.CreateAttr ctx
html.customElement (comp.Type, attrs = attrs)
| _ -> defaultArg notFoundNode html.none
)
)
)
.AddFunBlazor()
|> ignore
.AddFunBlazor()

if enableAntiforgery then
builder.WithMetadata(RequiresAntiforgeryMetadata()) |> ignore

/// This will serve all components which is marked as FunBlazorCustomElementAttribute in the target assembly for server side rendering with custom element enabled,
/// You should use it with: services.AddServerSideBlazor(fun options -> options.RootComponents.RegisterCustomElementForFunBlazor<YourComponent>()),
/// route pattern: /fun-blazor-custom-elements/{componentType}
[<Extension>]
static member MapCustomElementsForSSR(builder: IEndpointRouteBuilder, assembly: Assembly, ?notFoundNode: NodeRenderFragment) =
static member MapCustomElementsForSSR(builder: IEndpointRouteBuilder, assembly: Assembly, ?notFoundNode: NodeRenderFragment, ?enableAntiforgery: bool) =
let types =
assembly.GetTypes()
|> Seq.filter (fun ty -> ty.GetCustomAttribute<FunBlazorCustomElementAttribute>() |> box |> isNull |> not)
builder.MapCustomElementsForSSR(types, defaultArg notFoundNode html.none)
builder.MapCustomElementsForSSR(types, defaultArg notFoundNode html.none, defaultArg enableAntiforgery false)
#endif


Expand Down

0 comments on commit f54fcc3

Please sign in to comment.