Skip to content

Commit

Permalink
Moving policy towards an action filter FubuMvcArchive#7
Browse files Browse the repository at this point in the history
  • Loading branch information
KevM committed Feb 19, 2014
1 parent 8dc1b53 commit 07988cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
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

0 comments on commit 07988cd

Please sign in to comment.