Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at a move to a ActionFilter based policy #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using FubuMVC.Core;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.StructureMap;
using FubuTestingSupport;
using HtmlTags;
Expand All @@ -21,9 +22,9 @@ public void the_order_of_the_configuration_action_was_wrong()
{
var graph = runtime.Factory.Get<BehaviorGraph>();

graph.BehaviorFor<TestEndpoint>(x => x.post_csrf(null))
.OfType<AntiForgeryNode>().Any()
.ShouldBeTrue();
graph.BehaviorFor<TestEndpoint>(x => x.post_csrf(null))
.OfType<ActionFilter>().Any()
.ShouldBeTrue();
}


Expand Down
30 changes: 30 additions & 0 deletions src/FubuMVC.AntiForgery/AntiForgeryFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Net;
using FubuMVC.Core.Continuations;

namespace FubuMVC.AntiForgery
{
public interface IAntiForgeryFilter
{
FubuContinuation Filter(string salt);
}

public class AntiForgeryFilter : IAntiForgeryFilter
{
private readonly IAntiForgeryValidator _validator;

public AntiForgeryFilter(IAntiForgeryValidator validator)
{
_validator = validator;
}

public FubuContinuation Filter(string salt)
{
if (_validator.Validate(salt))
{
return FubuContinuation.NextBehavior();
}

return FubuContinuation.EndWithStatusCode(HttpStatusCode.InternalServerError);
}
}
}
25 changes: 13 additions & 12 deletions src/FubuMVC.AntiForgery/AntiForgeryPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using FubuMVC.Core;
using FubuMVC.Core.Registration;

using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;

namespace FubuMVC.AntiForgery
{
[ConfigurationType(ConfigurationType.InjectNodes)]
public class AntiForgeryPolicy : IConfigurationAction
{
public void Configure(BehaviorGraph graph)
{
var antiForgerySettings = graph.Settings.Get<AntiForgerySettings>();
graph.Behaviors.Where(antiForgerySettings.AppliesTo)
.Each(x => x.Prepend(new AntiForgeryNode(x.InputType().FullName)));
}
}
[ConfigurationType(ConfigurationType.InjectNodes)]
public class AntiForgeryPolicy : IConfigurationAction
{
public void Configure(BehaviorGraph graph)
{
var antiForgerySettings = graph.Settings.Get<AntiForgerySettings>();
graph.Behaviors.Where(antiForgerySettings.AppliesTo)
.Each(x => x.Prepend(ActionFilter.For<IAntiForgeryFilter>(f => f.Filter(x.InputType().FullName))));
}
}
}
1 change: 1 addition & 0 deletions src/FubuMVC.AntiForgery/FubuMVC.AntiForgery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="AntiForgeryData.cs" />
<Compile Include="AntiForgeryExtensions.cs" />
<Compile Include="AntiForgeryNode.cs" />
<Compile Include="AntiForgeryFilter.cs" />
<Compile Include="AntiForgeryPolicy.cs" />
<Compile Include="AntiForgeryService.cs" />
<Compile Include="AntiForgeryServiceRegistry.cs" />
Expand Down